View Full Version : mail() problem

07-13-2007, 12:58 PM

I have written a send to a friend script in php. The script appears to work fine...I can receive it, but hotmail, aol and bt connect email accounts don't receive anything.

$friend_from_email = "myemail@myaddress.co.uk";
$friend_subject = "Take a look at this";

$filename = "send_to_friend.txt";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
$contents = str_replace("Xyour_nameX", $your_name, $contents);
$contents = str_replace("Xyour_emailX", $your_email, $contents);
$contents = str_replace("Xfriend_nameX", $friend_name, $contents);
$contents = str_replace("Xfriend_emailX", $friend_email, $contents);
$contents = str_replace("XmessageX", $message, $contents);
$contents = str_replace("XpageURLX", $pageURL, $contents);
$contents = str_replace("Xfriend_from_emailX", $friend_from_email, $contents);
mail("$friend_email","$friend_subject","$contents","From: $your_name <$friend_from_email>");
fclose ($handle);

I've tried adding header info like this:

mail("$friend_email", "$friend_subject", "$contents",
"From: $your_name <$email>\n" .
"MIME-Version: 1.0\n " .
"Content-type: text/html; charset=iso-8859-1" .
"X-Mailer: PHP/" . phpversion());

but it doesn't seem to make any difference.

Any help is really appreciated!



07-13-2007, 04:37 PM
Does $your_name need to be in quotes?

mail("$friend_email","$friend_subject","$contents","From: \"$your_name\" <$friend_from_email>");

Also have you checked their spam folders?

07-14-2007, 09:36 AM
It's not going into their spam folders.

It works fine...$your_name appears in the From field of the e-mail, so I'm not sure the quotes are needed but I'll have a go.

Like I said..the code I have works fine..I just think I need something more, possibly more header information for it to be accepted by some e-mail accounts.

Thanks for your help.


07-14-2007, 10:04 AM
If it helps these are the headers I use for my site

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: PR Designz<me@domain.com>\r\n" .
"Reply-To: ".$Email."";

07-14-2007, 04:14 PM
There are several possibilities. Either your mail server is not sending the mail out or the receiving mail servers are not accepting it.

When you send it to yourself, the To: address $friend_email is to a mail box on your mail server and your mail server just thinks it is receiving an email and it places it into your mail box. When you send it to an external mail address, your mail serve then thinks that you are relaying an email.

Normally, to send an email to an external address, if the mail box in the From: address (ignoring the alias name portion of it, which does not require additional quotes BTW) is a mail box on your mail server, your mail server should accept the mail and re-send it. However, if your mail server is setup to require SMTP authentication to relay mail/send to a third party address, it won't accept and re-send this email, in which case there should be a failure message. Either check your web server log for errors or turn on full PHP error reporting at the start of your script. If your mail server is setup to require authentication, the PHP mail(...) function cannot be used and you would need to use a script like the phpmailer class - http://phpmailer.sourceforge.net/

The header in your second example only has \n between the lines and it is possible that either your mail server or the receiving mail servers can not process the values. These need to be \r\n. Once you follow the code in Aerospace_Eng's post, the \n problem will be corrected. His code also supplies a proper From: address for a mail box on your mail server (see the next paragraph for why) and allows a separate reply to address that contains a mail box not on your mail server.

For external mail servers to accept mail from your mail server, your mail server needs to have a reverse DNS record that resolves back to the domain name in the From: address and and you need to have an SPF record (an incorrect or non-matching SPF record will get your mail rejected, a missing SPF record will get your mail voted as junk/spam) that says that your mail server is allowed to send mail for the domain in the From: address. Check for these type of problems by entering your domain at www.dnsreport.com

In the case where the From: address is the entered address from the form (the second header example in your first post), it would normally be an address that does not match your sending mail server and if the domain in that From: address has an SPF record, mail from your mail server would get rejected because the SPF record at that domain's DNS server would indicate that your mail server is not allowed to send mail for that From: domain.