Set senders address in PHP scripts

When a user send out an email using php script (sendmail function), they will notice that the email is sent as "nobody@servername" or "anonymous@servername".

If the email bounces, it will not bounce back to the sender's email address specified in the PHP mail() but to "nobody@servername" or "anonymous@servername"
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:


$to = '';
$from = '';
$subject = 'hello';

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

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


Properties ID: 000212   Views: 4697   Updated: 13 years ago
Filed under: