Php send mail with specify sender address rather than nobody

When a user send out an email using php script (sendmail function), they will encounter that the email will not be bounce back to their own email account instead it bounces back the email to nobody@servername.com even though that they already specify the from, return-path, reply-to.

To make sure that the mail server uses the email address specify rather than to nobody user, must add "-f $from" at the mail() to bypass the nobody user. You may see the example at below:

<?php

$to = 'xyz@yahoo1.com1';
$from = 'test@abc.com';
$subject = 'hello';

$headers = 'From: test@abc.com' . "\r\n".
'Reply-To: test@abc.com'. "\r\n".
'Return-Path: test@abc.com' . "\r\n".
'X-Mailer: PHP/' . phpversion();
$message = "test123";

mail($to, $subject, $message, $headers, "-f $from");

?>

The above example, will send the bounce back email to the sender that send out the email instead of nobody@server_name.com.

Why is it good to implement the above script:

1) You are able to keep track on which of your recipient is not a valid user so that you can discard the name from your list
2) You can avoid your script being mistaken as sending out spam email to your client.

Add Feedback