PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999





How To Generate and Send Mail From PHP

Adding Extra Header Information

As I mentioned, the mail() function allows you to specify additional header fields to be added to the message header. One use for the extra headers to identify the message as having been generated programmatically. Any script that generates mail can be thought of as a "remailer." Often, it is useful for a form to send mail that appears to come from the person submitting the form. One such use is when setting up a form that subscribes a user to a mailing list. The following code rewrites the From: field to the user's email address and adds a X-Header identifying the mail as being sent from your custom remailer script. Identifying mail as coming from your script gives administrators a heads up that mail is coming from an automated source, if there should be a problem with mail generated by your script. I used the phpversion() function to identify the message as coming from a php script (as the php manual shows).

<?php

  mail 
($list_request"Subscribe""Subscribe""From: $email_address X-Mailer: PHP/" phpversion());

?>
 

Some list servers require that the user's first and last names be available in the From: field. To satisfy them, add form variables for the names into the From: field in the extra headers. It's necessary to "escape" the double quotes within a double quote context. This is accomplished by prefixing (or "escaping", which is the fancy term for protecting a character from misinterpretation) the quotes surrounding the first and last names with the backslash character. The email address is enclosed by angle brackets to help differentiate it from the surrounding text, as is usual practice in net email.

<?php

  $from 
"From: "$first_name $last_name" <$email_address> X-Mailer: PHP/" phpversion();
  
mail ($list_request"Subscribe""Subscribe"$from);

?>
 
Pages:  1   2   3   4   5Next:  When to Use the Mail Function »




Copyright © 2017 PHPHelp.com