Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postfix Ignores PHP mail() function

I'm successfully sending mail using PHP using the mail() function but the sending is showing up as the server address and not what I have configured in the script. Postfix is installed on the apache server. On ServerFault answer I read to use the -f and -r flags but that does not work either like this:

mail($to, $subject, $message, $headers,'From: ' . $fromname . ' <'.$from.'>', "-f $from -r [email protected]");

The best I've been able to do is change the Postfix myorigin which is the local machine name hostname to the parent domain of the machine name.

This leads me to believe Postfix is ignoring or stripping the From: element?

like image 960
Rocco The Taco Avatar asked Oct 07 '15 20:10

Rocco The Taco


4 Answers

mail($to, $subject, $message, $headers,'From: ' . $fromname . ' <'.$from.'>', "-f $from -r [email protected]");

Cannot work as mail() only accepts 5 parameters: bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

For me both

mail($to, $subject, $message, null, "-r [email protected]");
mail($to, $subject, $message, 'Name <[email protected]>', "-r [email protected]");

work (I don't have any additional restrictions in postfix which might rewrite senders). Also make sure that the sender is properly formatted (no spaces or whatsoever).

However, I recommend to adjust the sendmail_path setting in your php.ini to include the "-r [email protected]" parameter.

If it doesn't work for you, you should post the mail headers here and check your log files for warnigns/errors.

PS: Please make sure you don't pass user entered data directly to mail(), because if users can insert newlines (such as \nCC: [email protected] in subject, to mail or from mail) your server might be used for spamming.

like image 141
MrTux Avatar answered Nov 17 '22 11:11

MrTux


You could let the Postfix do its job rewriting headers and adding proper From: addresses. For this, Postfix has sender_canonical_maps.

Let's say your web server (or a process manager) runs under user named username, and you want to have [email protected] in the From: header of your emails instead of unclear [email protected]. Then, canonical map would contain:

#/etc/postfix/canonical
username [email protected]

Configuring Postfix to use it can't be easier:

postmap /etc/postfix/canonical
postconf -e sender_canonical_maps=hash:/etc/postfix/canonical

Other option is to tell postfix to mark all emails send from you server as send from [email protected] by means of luser_relay:

postconf -ev [email protected]

If you can't do it, quite possible someone already did this for you, but did that wrong. That could be a reason why you can't change From: address since it is being forcedly overwritten by Postfix.

like image 35
sanmai Avatar answered Nov 17 '22 09:11

sanmai


You've got the header argument in your mail() call twice and thus, your $additional_parameters argument isn't being used because it's the 6th argument in a function that only accepts 5. You should instead move the header to be included with the rest of your headers:

//Be mindful to protect from email injection attacks.
$fromname = str_replace("\n", "", $fromname);
$from = str_replace("\n", "", $from);

//This assumes that $headers is already set, containing your other headers
$headers .= 'From: ' . $fromname . ' <'.$from.">\n";

mail($to, $subject, $message, $headers, "-f $from -r [email protected]");

Using this, I would expect that your 5th argument is now redundant and could be simplified to:

$fromname = str_replace("\n", "", $fromname);
$from = str_replace("\n", "", $from);

$headers .= 'From: ' . $fromname . ' <'.$from.">\n";

mail($to, $subject, $message, $headers);
like image 4
HPierce Avatar answered Nov 17 '22 09:11

HPierce


In PHP, the syntax for mail function is as follows:

mail($to, $subject, $message, $headers, $parameters);

So in your case, if you want to specify a different "From" address and a different "Return path" your code should be as follows:

$to = "[email protected]";
$subject = "Your reset email";
$message = "<message text / html>";
$from = "[email protected]";
$bounceEmail = "[email protected]";
$headers = array(
    "From: " . $from
);
$headers = implode("\r\n", $headers);
$parameters = "-r" . $bounceEmail;

// Send it!
mail($to, $subject, $message, $headers, $parameters);

Hope this helps!

like image 4
Jamesking56 Avatar answered Nov 17 '22 11:11

Jamesking56