Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail issue with www-data

Tags:

php

sendmail

I am trying to invoke sendmail via PHP's mail function by the following code:

$to      = '[email protected]';     $subject = 'test';     $message = 'test';     $headers = 'From: [email protected]' . "\r\n" .                'Reply-To: [email protected]' . "\r\n" .                 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); 

However in my mail.log I am getting a message that the from is not the address I specified in the header:

<www-data@Name>: Sender address rejected: Domain not found 

Why is this?? I am running PHP's fast-cgi on ubuntu Why doesn't sendmail use the header that I have specified via the PHP code?

like image 494
aherlambang Avatar asked Apr 14 '11 16:04

aherlambang


People also ask

Why my mail function is not working in PHP?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

Does PHP mail need SMTP?

On a *nix machine, the PHP mail() function does not support SMTP, but instead uses the sendmail() or other configured mail script on the server. This script can send through an SMTP, but this isn't the easiest way within PHP (unless you already have the script). To use SMTP, I would recommend PHPMailer.

How do I prevent mails sent through PHP mail () from going to spam?

Try changing your headers to this: $headers = "MIME-Version: 1.0" . "\r\n"; $headers . = "Content-type: text/html; charset=iso-8859-1" .

Why does PHP mail take so long?

It is the SMTP mail delivery (which PHP hands off the message to) which is taking time. Possibly, the delay you see is greylisting on the receiving server, meaning that the receiving mail server refuses to accept the message until the sending server (which your PHP script handed it to) tries a few times.


2 Answers

It looks like www-data@Name is your envelope "from" address. The envelope "from" address is different from the address that appears in your "From:" header of the email. It is what sendmail uses in its "MAIL FROM/RCPT TO" exchange with the receiving mail server.The main reason it is called an "envelope" address is that appears outside of the message header and body, in the raw SMTP exchange between mail servers.

The default envelope "from" address on unix depends on what sendmail implementation you are using. But typically it will be set to the username of the running process followed by "@" and the hostname of the machine. In a typical configuration this will look something like [email protected].

If your emails are being rejected by receiving mail servers, or if you need to change what address bounce emails are sent to, you can change the envelope "from" address to solve your problems.

To change the envelope "from" address on unix, you specify an "-r" option to your sendmail binary. You can do this globally in php.ini by adding the "-r" option to the "sendmail_path" command line. You can also do it programmatically from within PHP by passing -r [email protected] as the additional parameter argument to the mail() function (the 5th argument). If you specify an address in both places, the sendmail binary will be called with two "-r" options, which may have undefined behavior depending on your sendmail implementation. With the Postfix MTA, later "-r" options silently override earlier options, making it possible to set a global default and still get sensible behavior when you try to override it locally.

EDIT

About optional flags that can be passed to sendmail: -f will set the From address, -r will override the default Return-path that sendmail generates (typically the From address gets used). If you want your bounce-backs to go to a different address than the from address, try using both flags at once: -f [email protected] -r [email protected]

my php.ini

[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25  ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = [email protected]  ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path ;sendmail_path =  ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters =  ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = On  ; Log all mail() calls including the full path of the script, line #, to address and headers ;mail.log = 
like image 89
anubhava Avatar answered Sep 21 '22 13:09

anubhava


Although this is an old question, I'm adding this answer in case it is of help to someone:

I had the same problem with the From: header being re-written to www-data@host... I eventually tracked it down to the ssmtp bridge service that was piping mail from our web server into our mailserver. I added the line FromLineOverride=YES in the file /etc/ssmtp/ssmtp.conf and the problem disappeared.

like image 33
David G Avatar answered Sep 21 '22 13:09

David G