1

I got question regarding sendmail, I already searched for similar questions but couldn't find anything which was helpful to me.

I am currently using a server (Debian with LAMP Stack) with sendmail, to send verification mail for user registrations on the website. This usually works fine, gmail e.g. receives the mails and everything works perfectly. BUT I recently discovered that with yahoo it doesn't work at all, mails seemed to be not received by yahoo at all. So I set up a yahoo account myself for testing and observed the following behaviour, which I try to describe as detailed as possible:

I use the following PHP-Code-Snippet to send mails:

$toEmail = '...';
$subject = '...';
$message = '...';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=utf-8";
$headers[] = "From: noreply@MYSERVER.de";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

if(mail($toEmail, $subject, $message, implode("\r\n",$headers))) {
    return TRUE;
}

I got an installation of sendmail working on the server, processing the mail-command from PHP.

As said, this works completely fine for nearly all adresses as recipient except for yahoo (and maybe others which I don't know of yet). Using the form on the website which triggers the sending of the mail, I receive the mail within seconds. The following is the log shown in /var/log/mail.log

May  3 14:19:12 btfmx5 sendmail[544]: u43CJCtW000544: from=www-data, size=1174, class=0, nrcpts=1, msgid=<201605031219.u43CJCtW000544@MYSERVER.de>, relay=www-data@localhost
May  3 14:19:13 btfmx5 sm-mta[545]: u43CJDBO000545: from=<www-data@MYSERVER.de>, size=1419, class=0, nrcpts=1, msgid=<201605031219.u43CJCtW000544@MYSERVER.de>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
May  3 14:19:13 btfmx5 sendmail[544]: u43CJCtW000544: to=TESTADDRESS@gmail.com, ctladdr=www-data (33/33), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=31174, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (u43CJDBO000545 Message accepted for delivery)
May  3 14:19:13 btfmx5 sm-mta[547]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=ECDHE-RSA-AES128-GCM-SHA256, bits=128/128
May  3 14:19:13 btfmx5 sm-mta[547]: u43CJDBO000545: to=<TESTADDRESS@gmail.com>, ctladdr=<www-data@MYSERVER.de> (33/33), delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=121419, relay=gmail-smtp-in.l.google.com. [64.233.184.26], dsn=2.0.0, stat=Sent (OK 1462278313 o16si26784998wme.6 - gsmtp)

However, when I try sending to a yahoo address, the PHP-Code above returns TRUE but no mail is received in the yahoo account at all. The following is the log shown in this case:

May  3 14:26:50 btfmx5 sendmail[571]: u43CQoiH000571: from=www-data, size=1174, class=0, nrcpts=1, msgid=<201605031226.u43CQoiH000571@MYSERVER.de>, relay=www-data@localhost
May  3 14:26:50 btfmx5 sm-mta[572]: u43CQogB000572: from=<www-data@MYSERVER.de>, size=1419, class=0, nrcpts=1, msgid=<201605031226.u43CQoiH000571@MYSERVER.de>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
May  3 14:26:50 btfmx5 sendmail[571]: u43CQoiH000571: to=TESTADDRESS@yahoo.com, ctladdr=www-data (33/33), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=31174, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (u43CQogB000572 Message accepted for delivery)
May  3 14:26:51 btfmx5 sm-mta[574]: STARTTLS=client, relay=mta5.am0.yahoodns.net., version=TLSv1/SSLv3, verify=FAIL, cipher=ECDHE-RSA-AES128-GCM-SHA256, bits=128/128
May  3 14:26:53 btfmx5 sm-mta[574]: u43CQogB000572: to=<TESTADDRESS@yahoo.com>, ctladdr=<www-data@MYSERVER.de> (33/33), delay=00:00:03, xdelay=00:00:03, mailer=esmtp, pri=121419, relay=mta5.am0.yahoodns.net. [66.196.118.37], dsn=5.0.0, stat=Service unavailable
May  3 14:26:53 btfmx5 sm-mta[574]: u43CQogB000572: u43CQrgB000574: DSN: Service unavailable
May  3 14:26:53 btfmx5 sm-mta[574]: u43CQrgB000574: to=<www-data@MYSERVER.de>, delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30000, dsn=2.0.0, stat=Sent

I now tried to send an email to the yahoo adress using sendmail from the command line, to get additional informaiton from verbose mode.

But to my surprise, this email was received by yahoo (in the spam folder, but who cares)! Now I'm completely puzzled, how the hell does it work from command line, but not when using PHP?

The command I used for sending the mail:

echo "Subject: testmail" | sendmail -v TESTADDRESS@yahoo.com

And the corresponding log entries in mail.log:

May  3 14:34:35 btfmx5 sendmail[581]: u43CYZp5000581: from=alumpi, size=18, class=0, nrcpts=1, msgid=<201605031234.u43CYZp5000581@MYSERVER.de>, relay=root@localhost
May  3 14:34:35 btfmx5 sm-mta[582]: u43CYZx0000582: from=<alumpi@MYSERVER.de>, size=340, class=0, nrcpts=1, msgid=<201605031234.u43CYZp5000581@MYSERVER.de>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
May  3 14:34:37 btfmx5 sm-mta[582]: STARTTLS=client, relay=mta7.am0.yahoodns.net., version=TLSv1/SSLv3, verify=FAIL, cipher=ECDHE-RSA-AES128-GCM-SHA256, bits=128/128
May  3 14:34:39 btfmx5 sm-mta[582]: u43CYZx0000582: to=<TESTADRESS@yahoo.com>, ctladdr=<alumpi@MYSERVER.de> (1000/1000), delay=00:00:04, xdelay=00:00:04, mailer=esmtp, pri=30340, relay=mta7.am0.yahoodns.net. [63.250.192.45], dsn=2.0.0, stat=Sent (ok dirdel)
May  3 14:34:39 btfmx5 sendmail[581]: u43CYZp5000581: to=TESTADRESS@yahoo.com, ctladdr=alumpi (1000/1000), delay=00:00:04, xdelay=00:00:04, mailer=relay, pri=30018, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (u43CYZx0000582 Message accepted for delivery)

Verbose output of the sendmail command:

TESTADDRESS@yahoo.com... Connecting to [127.0.0.1] via relay...
220 MYSERVER.de ESMTP Sendmail 8.14.4/8.14.4/Debian-8; Tue, 3 May 2016 14:34:35 +0200; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
>>> EHLO MYSERVER.de
250-MYSERVER.de Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-EXPN
250-VERB
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250-DELIVERBY
250 HELP
>>> VERB
250 2.0.0 Verbose mode
>>> MAIL From:<alumpi@MYSERVER.de> SIZE=18 AUTH=alumpi@MYSERVER.de
250 2.1.0 <alumpi@MYSERVER.de>... Sender ok
>>> RCPT To:<TESTADDRESS@yahoo.com>
>>> DATA
250 2.1.5 <TESTADDRESS@yahoo.com>... Recipient ok
354 Enter mail, end with "." on a line by itself
>>> .
050 <TESTADDRESS@yahoo.com>... Connecting to mta7.am0.yahoodns.net. via esmtp...
050 220 mta1523.mail.gq1.yahoo.com ESMTP ready
050 >>> EHLO MYSERVER.de
050 250-mta1523.mail.gq1.yahoo.com
050 250-PIPELINING
050 250-SIZE 41943040
050 250-8BITMIME
050 250 STARTTLS
050 >>> STARTTLS
050 220 Start TLS
050 >>> EHLO MYSERVER.de
050 250-mta1523.mail.gq1.yahoo.com
050 250-PIPELINING
050 250-SIZE 41943040
050 250 8BITMIME
050 >>> MAIL From:<alumpi@MYSERVER.de> SIZE=340
050 250 sender <alumpi@MYSERVER.de> ok
050 >>> RCPT To:<TESTADDRESS@yahoo.com>
050 >>> DATA
050 250 recipient <TESTADDRESS@yahoo.com> ok
050 354 go ahead
050 >>> .
050 250 ok dirdel
050 <TESTADDRESS@yahoo.com>... Sent (ok dirdel)
250 2.0.0 u43CYZx0000582 Message accepted for delivery
TESTADDRESS@yahoo.com... Sent (u43CYZx0000582 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 MYSERVER.de closing connection

So the main question is: Why does sending to for example gmail work, but sending to yahoo fails?

The secondary question: Why does sending to yahoo via command line work, but fail via PHP?

I hope I provided all necessary information, thank you in advance for your help!

Beztix
  • 29
  • 1
  • 1
  • 5
  • What sender address is used when sending via echo "Subject: testmail" | sendmail -v sebastian.luetzow@yahoo.com ? – Maciej Asembler May 03 '16 at 13:46
  • 1) Receiving server "may dislike" `www-data` as envelope sender. 2) Your php executed sendmail which successfully passed the email to local sedmail daemon at 127.0.0.1:25 – AnFi May 03 '16 at 14:58
  • Thanks for the comments, helped by pushing me in the right direction to solve the problem :) – Beztix May 04 '16 at 10:07
  • Voting to close since this was a programming issue, not a mailserver issue. – Jenny D Aug 19 '17 at 15:52

1 Answers1

1

Thanks to the comments I was able to figure it out (I am rather new to this kind of admin stuff, didn't know well about envelope sender and so on...):

As seen from the logs, the envelope sender used by sendmail when using PHP was www-data@MYSERVER.de, when using the command line it was alumpi@MYSERVER.de, so I tried changing them around. BUT this wasn't a problem and no reason for the different behaviour of the two cases.

In fact dumb me only then realized that I would get a bounce-message with probably useful information, if I use a proper mail-address as envelope sender/return-path. After doing so, I received a bounce message to this mail-address that contained:

554 Message not allowed - Headers are not RFC compliant[291]

So yeah, the initial error is quite unspectecular:

As seen in the PHP-Code posted in the question, I'm sending the subject-header twice. Once in the headers-array and once directly using the subject-parameter of the PHP mail()-function. It seems most mail providers don't give a sh** about that, but yahoo does.

So, I cleaned up my PHP code to send a single subject and use a useful return-path, now it works perfectly (and doesn't even go to the spam-folder):

$toEmail = '...';
$subject = '...';
$message = '...';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=utf-8";
$headers[] = "From: noreply@MYSERVER.de";
$headers[] = "X-Mailer: PHP/".phpversion();

if(mail($toEmail, $subject, $message, implode("\r\n",$headers), '-f ME@MYADDRESS.de')) {
    return TRUE;
}
Beztix
  • 29
  • 1
  • 1
  • 5