I am making a contact form. I've gotten a rudimentary version to work (gathering form info and sending an email from my site to my personal email) but I cannot seem to get the 'additional headers' to work. It works fine if I have the following headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
But if I try and add additional Mail headers such as:
$headers .= 'To: Jack <Jack Johnson>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
$headers .= "\r\nX-Mailer: PHP/" ;
I get a 'fail' on the mail function. I'm using PHP version 5.3.8. To make sure the mail function is working I am doing this:
$sendmail = mail($email_to, $email_subject, $email_message, $headers);
if ($sendmail) {
echo '<div>Thanks for submitting!</div>';
} else {
echo '<div>Fail</div>';
}
Am I formatting this incorrectly?
$headers .= 'To: Jack <Jack Johnson>' . "\r\n";
As mentioned in comments, this doesn't need to be there. But it will almost certainly cause a problem because it doesn't contain an email address
Also as mentioned in the comments, you have a double \r\n
by including it at the start of
$headers .= "\r\nX-Mailer: PHP/" ;
Finally, this shouldn't cause the problem, but you really shouldn't do it:
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
Bcc lines do not belong in the header. They will appear in the header for all recipients, undermining the point of Bcc. You will find this is handled intermittently in different mail clients and services. Some will display it, some will just keep it, some will "kindly" hide it from the headers.
PHP's mail() isn't really designed to handle Bcc, so you will probably need to call the mail() function and separately send to the Bcc recipient