How to specify a return-path for generated mail using PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to specify a return-path for generated mail using PHP

Post by Tony » Sun Nov 29, 2009 3:57 am

You may want to specify a return-path for your website generated mail. This is the address to which rejected mail will be returned. This is very different from a return address. You can specify the return-path by using the -f parameter in the mail() function or specifying the -f parameter as a default in a php.ini file.

You can put the -f parameter in the mail() function, as follows :

Code: Select all

mail($toEmailAddress,$subject,$message,$headers,"-f$returnAddress"); 
Is is a fifth parameter, and note there is no space between the -f and the email address.

You can combine the example above with these two lines (put them first) to cover all the bases (Return Path, Reply To, and From).

Code: Select all

$headers = "From: <$returnAddress>\n"; 
$headers .= "Reply-To: <$returnAddress>\n";  
You can specify a default return-path in your php.ini file using this line:

Code: Select all

sendmail_path = /usr/sbin/sendmail -t -i [email protected]
Ensure the sendmail path is correct for your host, and again note there is no space between the -f and the email address.
Post Reply

Return to “PHP & MySQL”