Sending Email with PHP

PHP mail() function can automatically send email thru the internet. This is how it’s done.

Example 1. Sending mail.

mail("joecool@example.com", "My Subject", "Line 1\nLine 2\nLine 3");
Example 2. Sending mail with extra headers.

mail("nobody@example.com", "the subject", $message,
     "From: webmaster@$SERVER_NAME\r\n"
    ."Reply-To: webmaster@$SERVER_NAME\r\n"
    ."X-Mailer: PHP/" . phpversion());
Example 3. Sending mail with extra headers and setting an additional command line parameter.

mail("nobody@example.com", "the subject", $message,
     "From: webmaster@$SERVER_NAME", "-fwebmaster@$SERVER_NAME");

You can also use simple string building techniques to build complex email messages.

Example 4. Sending complex email.

/* recipients */
$to  = "Mary " . ", " ; // note the comma
$to .= "Kelly ";

/* subject */
$subject = "Birthday Reminders for August";

/* message */
$message = '

Here are the birthdays upcoming in August!
Person Day Month Year
Joe 3rd August 1970
Sally 17th August 1973
'; /* To send HTML mail, you can set the Content-type header. */ $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; /* additional headers */ $headers .= "From: Birthday Reminder \r\n"; $headers .= "Cc: birthdayarchive@example.com\r\n"; $headers .= "Bcc: birthdaycheck@example.com\r\n"; /* and now mail it */ mail($to, $subject, $message, $headers);

====================

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Next Post » »