Sendmail dsn=5.0.0 Service unavailable when sending mails by PHP to yahoo

13,348

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 [email protected], when using the command line it was [email protected], 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: [email protected]";
$headers[] = "X-Mailer: PHP/".phpversion();

if(mail($toEmail, $subject, $message, implode("\r\n",$headers), '-f [email protected]')) {
    return TRUE;
}
Share:
13,348

Related videos on Youtube

Beztix
Author by

Beztix

Updated on September 18, 2022

Comments

  • Beztix
    Beztix over 1 year

    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: [email protected]";
    $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=<[email protected]>, relay=www-data@localhost
    May  3 14:19:13 btfmx5 sm-mta[545]: u43CJDBO000545: from=<[email protected]>, size=1419, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
    May  3 14:19:13 btfmx5 sendmail[544]: u43CJCtW000544: [email protected], 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=<[email protected]>, ctladdr=<[email protected]> (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=<[email protected]>, relay=www-data@localhost
    May  3 14:26:50 btfmx5 sm-mta[572]: u43CQogB000572: from=<[email protected]>, size=1419, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
    May  3 14:26:50 btfmx5 sendmail[571]: u43CQoiH000571: [email protected], 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=<[email protected]>, ctladdr=<[email protected]> (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=<[email protected]>, 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 [email protected]
    

    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=<[email protected]>, relay=root@localhost
    May  3 14:34:35 btfmx5 sm-mta[582]: u43CYZx0000582: from=<[email protected]>, size=340, class=0, nrcpts=1, msgid=<[email protected]>, 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=<[email protected]>, ctladdr=<[email protected]> (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: [email protected], 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:

    [email protected]... 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:<[email protected]> SIZE=18 [email protected]
    250 2.1.0 <[email protected]>... Sender ok
    >>> RCPT To:<[email protected]>
    >>> DATA
    250 2.1.5 <[email protected]>... Recipient ok
    354 Enter mail, end with "." on a line by itself
    >>> .
    050 <[email protected]>... 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:<[email protected]> SIZE=340
    050 250 sender <[email protected]> ok
    050 >>> RCPT To:<[email protected]>
    050 >>> DATA
    050 250 recipient <[email protected]> ok
    050 354 go ahead
    050 >>> .
    050 250 ok dirdel
    050 <[email protected]>... Sent (ok dirdel)
    250 2.0.0 u43CYZx0000582 Message accepted for delivery
    [email protected]... 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!

    • Maciej Asembler
      Maciej Asembler about 8 years
      What sender address is used when sending via echo "Subject: testmail" | sendmail -v [email protected] ?
    • AnFi
      AnFi about 8 years
      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
    • Beztix
      Beztix about 8 years
      Thanks for the comments, helped by pushing me in the right direction to solve the problem :)
    • Jenny D
      Jenny D almost 7 years
      Voting to close since this was a programming issue, not a mailserver issue.