PHP Programming/Mailing
The mail function is used to send E-mail Messages through the SMTP server specified in the php.ini Configuration file.
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
The returned boolean will show whether the E-mail has been sent successfully or not.
This example will send message "message" with the subject "subject" to email address "example@domain.tld". Also, the receiver will see that the eMail was sent from "Example2 <example2@domain.tld>" and the receiver should reply to "Example3 <example3@domain.tld>"
mail(
"example@domain.tld", // E-Mail address
"subject", // Subject
"message", // Message
"From: Example2 <example2@domain.tld>\r\nReply-to: Example3 <example3@domain.tld>" // Additional Headers
);
There is no requirement to write E-mail addresses in format "Name <email>", you can just write "email".
This will send the same message as the first example but includes From: and Reply-To: headers in the message. This is required if you want the person you sent the E-mail to be able to reply to you. Also, some E-mail providers will assume mail is spam if certain headers are missing so unless you include the correct headers your mail will end up in the junk mail folder.
Important notes
[edit | edit source]- PHP by default does not have any mail sending ability itself. It needs to pass the mail to a local mail transfer agent, such as sendmail. This means you cannot just run PHP by itself and expect it to send mail; you must have a mail transfer agent installed.
- Make sure you do not have any newline characters in the to or subject, or the mail may not be sent properly.
- However, the additional headers field -- which should always include the From: header -- may also include other headers. On PHP for Windows, each header should be followed by \r\n but on Unix versions, you should only include \n between header lines. Don't put \n or \r\n after the final additional header line.
- The to parameter should not be an address in the form of "Name <someone@example.com>". The mail command may not parse this properly while talking with the MTA (Particularly under Windows).
Error Detection
[edit | edit source]Especially when sending multiple emails, such as for a newsletter script, error detection is important.
Use this script to send mail while warning for errors:
$result = @mail($to, $subject, $message, $headers);
if ($result) {
echo "Email sent successfully.";
} else {
echo "Email was not sent, as an error occurred.";
}
Sending To Multiple Addresses Using Arrays
[edit | edit source]In the case below the script has already got a list of emails, we simply use the same procedure for using a loop in PHP with mysql results. The script below will attempt to send an email to every single email address in the array until it runs out.
while ($row = mysql_fetch_assoc($result)) {
mail($row['email'], $subject, $message, null, "-f$fromaddr");
}
Then if we integrate the error checking into the multiple email script we get the following
$errors = 0
$sent = 0
while ($row = mysql_fetch_assoc($result)) {
$result = "";
$result = @mail($row['email'], $subject, $message, null, "-f$fromaddr");
if (!$result) {
$errors = $errors + 1;
}
$sent = $sent + 1;
}
echo "You have sent $sent messages";
echo "However there were $errors errors";
For More Information
[edit | edit source]