PHP mail issue with www-data
Asked Answered
C

6

28

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?

Circuitous answered 14/4, 2011 at 16:18 Comment(2)
do you have this file: /etc/postfix/main.cf, also run postconf -n in your terminal, let us know the output.Defensive
I am using sendmail not postfixCircuitous
K
29

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 =
Kilowatt answered 14/4, 2011 at 16:49 Comment(21)
I added the -r option in sendmail_path and now I got: sendmail[2719]: p3EGvC1r002719: from=www-data, size=259, class=0, nrcpts=0, msgid=<201104141657.p3EGvC1r002719@DynusT>, relay=www-data@localhostCircuitous
If I specify -f [email protected] in my sendmail_path it works, but I don't want to do thisCircuitous
Just for testing can you try removing From: and Reply-To: from your code.Kilowatt
May I know your reservations against providing -f [email protected] but you are okay to use the same in From: and Reply-To: headers.Kilowatt
because it is possible to have different From address. That is like hardcoding the sender's addressCircuitous
Inside your php you can call mail function like this: mail('[email protected]', 'the subject', 'the message', $headers, '[email protected]'); to supply runtime value to mail function.Kilowatt
I know that, my curiosity is why doesn't it take directly from the header?Circuitous
That explanation is covered in the first paragraph of my answer above. In short From: in header is different from what goes in -f.Kilowatt
also I just tried doing the -f from PHP and it didn't send the mail, I got the following in my log Apr 14 17:25:45 DynusT sendmail[2812]: p3EHPjBi002812: Authentication-Warning: DynusT: www-data set sender to [email protected] using -f Apr 14 17:25:45 DynusT sendmail[2812]: p3EHPjBi002812: [email protected], size=269, class=0, nrcpts=0, msgid=<201104141725.p3EHPjBi002812@DynusT>, relay=www-data@localhostCircuitous
One more attempt: can you try this call please: mail('[email protected]', 'the subject', 'the message', $headers, '[email protected] [email protected]');Kilowatt
I've got nothing in my log now, but the email is no where to be found in my inbox nor spam folderCircuitous
Can you check the return value from above mail function call? And also try sending null in place of $headers above.Kilowatt
It just beats me. I just copy pasted above mail function call in my php script on Ubuntu and executed that from a browser. Not only mail function returned TRUE I also received my email on the to-email address.Kilowatt
hmmm...super weird, then it has something to do with my configuration of my php/server/mail server. How do you set up so that when you actually invoke sendmail from the CL it sends from [email protected]? as of now when I send something from the CL it sends it from root@machinehostnameCircuitous
I pasted my php.ini above in my answer as well.Kilowatt
hmm..everything seems to be commented thereCircuitous
what do you have in your var/hosts? aside from that my php.ini config looks somewhat similar to yours except I have sendmail_path uncommented and points to sendmail_path = /usr/sbin/sendmail -t -iCircuitous
All my /etc/hosts files are empty pretty much. Try commenting all the sendmail_ lines in your php.iniKilowatt
@Kilowatt Excellent answer! Thanks for breaking this down so clearly, this helped me solve a similar problem I was having regarding the sender envelope not being authenticated by SES. I did notice now that when sending test emails from my WP site that they end up in my spam folder in gmail. Could it be the sender envelope not matching the from address causing this?Mudskipper
Yes that seems to be reason why gmail marks it as spam. I am sure there would be some WP plugin to customize this but I am not a WP expert.Kilowatt
@Kilowatt You were a big help from this question in order to help me on this question.Trimorphism
D
10

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.

Dendroid answered 20/12, 2012 at 22:23 Comment(0)
C
6

In my case, I've got a hosted server so I needed to edit this file :

/etc/ssmtp/ssmtp.conf

Then uncomment this line :

FromLineOverride=YES

Once done, personals headers are working.

Coverage answered 18/11, 2014 at 8:30 Comment(0)
D
2

I was having similar problem with www-data when all my mails were sent and received with this header:

From: www-data <[email protected]>

I used the -f [email protected] flag as 5th argument with the PHP email() function (as mentioned in accepted answer), but i was still receiving my emails as:

From: www-data <[email protected]>

So i added one more flag -f [email protected] -F info to set the full name of the email and finally i was getting emails as i wanted:

From: info <[email protected]>

I'm posting this answer because nobody mentions it here and i got a little stuck on it.

Dick answered 15/2, 2019 at 8:40 Comment(0)
D
0

This worked for me:

$mail->Sendmail = $mail->Sendmail.' -f '.$mail_errorsto; 
Denson answered 15/7, 2014 at 9:19 Comment(0)
T
0

I had this issue using exim4 with smarthost. The mails were sent with

Return-path: <www-data@servername>

which was rejected by the ISP. I needed to change it to at least [email protected] (assuming 'example.com' is the servers public domain name). I could achieve that by changing /etc/mailname from

servername

to

example.com

This has already worked for me, having [email protected] as Return-path.

However - If you want to completely change the email address, you can configure it in /etc/email-addresses as

www-data: [email protected]

After that the emails have been sent by default with

Return-path: <[email protected]>
Tufa answered 28/2, 2020 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.