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:

<?php

$to = 'xyz@hers.com';
$from = 'test@his.com';
$subject = 'hello';

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

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

?>


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