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

When to Use the Mail Function

Having such a simple and uncomplicated function for sending mail is useful when a script must send several messages. It takes only one line of code (unless you create your own function for sending mail), so helps keep code readable if the mail() functions must appear in a conditional statement. It's also nice to be able to communicate with the mail program without worrying about the command syntax or security measures required to execute a Unix mail program. Although the interface to sendmail or qmail is standardized, using the mail function protects your script to a degree from any changes to sendmail an administrator might make. Using the mail() function means you do not have to create your own function for sending mail or duplicate code inline. The following script gives an example of using more than one mail() function in a script to notify the administrator when a form submission is made and at the same time, respond to the user by email. (Please keep in mind this is just a sketch showing what is possible, not a working example).


// notify admin
$mailTo "";
$msgSubject "Downloading MediaKit";
$msgBody "$name$company$address$email_address$demographics";
$xHeaders "From: $email_address X-Mailer: PHP/" phpversion();

mail ($mailTo$msgSubject$msgBody$xHeaders);

// thank customer
$mailTo $email_address;
$msgSubject "Thank You for Downloading our MediaKit";
$msgBody "Please feel free to contact us if you have any questions or desire a quote. Thank You.";
$xHeaders "From: X-Mailer: PHP/" phpversion();

mail ($mailTo$msgSubject$msgBody$xHeaders);

Pages:  1   2   3   4   5Next:  Talking to Sendmail »

Copyright © 2017