I was digging around my server tidying things up, and I found I had a huge www-data mail file. I took a look into the file and found it was full of bounces from emails sent from sites I host to users, whose email address did not exist or there were issues.
I had thought that php would send the email to the sender, but as it was sent from a user (www-data i.e. the native apache user) it was sent back to the user. Maybe it’s a quirk of my server but digging deeper on the net this scenario is not uncommon.
To get the email to be sent back to the sender, rather than the apache user, you need to set the hidden ’5th’ option when using the php mail() function.
So instead of:
$to = "[email protected]";
$subject = "Website contact";
$message = "This is a message from our website to say thank you";
$headers = "From: [email protected]";
mail($to, $subject, $message, $headers);
you use:
$to = "[email protected]";
$subject = "Website contact";
$message = "This is a message from our website to say thank you";
$headers = "From: [email protected] \r\nReply-To: [email protected]";
$additional = "[email protected]";
mail($to, $subject, $message, $headers, $additional);
The $additional is a direct interaction with sendmail – telling it to ‘force’ the return path to be whatever you tell it to be (in this case my email address).
By Colin Harris