Posted by & filed under Programming & Sysadmin.

Please, use the 5th argument

Php’s mail() function takes a 5th argument that is usually ignored. Using it will save you some headaches and let you see what is really happening with your website forms. Some web hosts will configure your outgoing email this way by default, so it may be unnecessary, but not all hosts do this, so it makes sense to be sure that your email is being sent with an envelope sender address that is a real address and not something like apache@localhost or worse.. Otherwise all mail that isn’t deliverable will be bounced back to an undeliverable address, and most likely be deleted. So, if you want to keep track of what mail is refused, bounced, or undeliverable for other reasons, you should set this parameter.

Example:

$to='recipient@example.com';
$subject='test';
$message='blah blah blah';
$headers = "From: Our Company \n";
$headers .= "Reply-To: Our Company \n";
$env_from='sender@example.com';
$mail_sent = @mail( $to, $subject, $message, $headers , "-f $env_from");
echo $mail_sent ? "Mail sent" : "Mail failed";