Problems sending an email to google's SMTP ser

2019-08-11 10:51发布

问题:

I wrote a small php script which connects to a SMTP server and sends an email. In particular connecting to googles smtp server. Obviously from the SMTPD set shown below something goes wrong.

[data2] => 501 5.5.2 Cannot Decode response o47sm22737144eem.11

Did I do a mistake in my email body, ie. wrong formatting? Here is the php script:

// SMTP settings
define( 'SMTP_SERVER', "ssl://smtp.gmail.com" );
define( 'SMTP_PORT', '465' );
define( 'SMTP_USER', 'XXX@gmail.com' ); // my gmail account
define( 'SMTP_PWD', 'XXX' ); // my gmail password


class Mail
{
    public static function send( $smtpServer, $smtpPort, $smtpUser, $smtpPwd,
        $from, $to, $subject, $message )
    {
        // Establish connection to SMTP server
        $smtpConn = fsockopen( $smtpServer, $smtpPort );
        $smtpLog['connect'] = fgets( $smtpConn );

        // In case of success start SMTP communication
        if ( $smtpConn !== false )
        {
            // Say hello to SMTP
            fputs( $smtpConn, 'EHLO ' . $_SERVER['SERVER_ADDR'] . NL );
            $smtpLog['hello'] = fgets( $smtpConn );

            // Require authentication
            fputs( $smtpConn, 'AUTH LOGIN' . NL );
            $smtpLog['auth'] = fgets( $smtpConn );

            // Send username
            fputs( $smtpConn, base64_encode( $smtpUser ) . NL );
            $smtpLog['user'] = fgets( $smtpConn );

            // Send password
            fputs( $smtpConn, base64_encode( $smtpPwd ) . NL );
            $smtpLog['pwd'] = fgets( $smtpConn );

            // Send email from
            fputs( $smtpConn, "MAIL FROM: <$from>" . NL );
            $smtpLog['from'] = fgets( $smtpConn );

            // Send email to
            fputs( $smtpConn, "RCPT TO: <$to>". NL );
            $smtpLog['to'] = fgets( $smtpConn );

            // Send "the email"
            fputs( $smtpConn, 'DATA' . NL );
            $smtpLog['data1'] =  fgets( $smtpConn );

            fputs( $smtpConn,
                "To: <$to>\r\nFrom: <$from>\r\nSubject: $subject\r\n$message\r\n.\r\n" );
            $smtpLog['data2'] =  fgets( $smtpConn );

            fputs( $smtpConn, 'QUIT' . NL );
            $smtpLog['quit'] =  fgets( $smtpConn );

            fclose( $smtpConn );
        }

        return $smtpLog;
    }
}


$log = Mail::send( SMTP_SERVER, SMTP_PORT, SMTP_USER, SMTP_PWD,
   'XXX@gmail.com', 'XXX@yahoo.com', 'Subject', 'Message' );

print_r( $log );

And the full response log from google's SMTP server:

[connect] => 220 mx.google.com ESMTP o47sm22737144eem.11
[hello] => 250-mx.google.com at your service, [XX.XX.XX.XX]
[auth] => 250-SIZE 35882577
[user] => 250-8BITMIME
[pwd] => 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
[from] => 250 ENHANCEDSTATUSCODES
[to] => 334 XXX
[data1] => 334 XXX
[data2] => 501 5.5.2 Cannot Decode response o47sm22737144eem.11
[quit] => 530-5.5.1 Authentication Required. Learn more at

回答1:

Man, you read single line replies where there are multiple lines.

MULTILINE REPLY:

250-LINE #1 // lines that have - after ### continue on the next
250-LINE #2 // lines that have - after ### continue on the next
250 LINE #3 // last line has space after ###

SINGLELINE REPLIES:

220 LINE #1 // one reply with code 220
250 LINE #2 // another reply with code 250
// no ### code is followed by - as that means: not the last line!

Your commands are out of sync. So fix like this:

if($smtpLog['hello'] = fgets( $smtpConn )){
    // handle multiline reply
    while($smtpLog['hello']{4} !== ' '){
        $smtpLog['hello'] .= PHP_EOL.fgets( $smtpConn );
    }
}

This applies to every response you get but in your case, commands break sync at EHLO.



回答2:

Try to use nice and tested php-class phpmailer, it's cool thing



标签: php email smtp