向Google的SMTP服务器发送电子邮件的问题(使用自己的php脚本) [英] Problems sending an email to google's SMTP server (with own php script)

查看:226
本文介绍了向Google的SMTP服务器发送电子邮件的问题(使用自己的php脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的php脚本,连接到一个SMTP服务器并发送一个电子邮件。特别是连接到谷歌smtp服务器。显然从下面显示的SMTPD集合出现问题。

  [data2] => 501 5.5.2无法解码响应o47sm22737144eem.11 

我的电子邮件正文中有错误,即。格式错误?
这里是PHP脚本:

  // SMTP设置
define('SMTP_SERVER','ssl ://smtp.gmail.com);
define('SMTP_PORT','465');
define('SMTP_USER','XXX@gmail.com'); //我的Gmail帐户
define('SMTP_PWD','XXX'); //我的gmail密码


类邮件
{
public static function send($ smtpServer,$ smtpPort,$ smtpUser,$ smtpPwd,
$ $,$ subject,$ message)
{
//建立与SMTP服务器的连接
$ smtpConn = fsockopen($ smtpServer,$ smtpPort);
$ smtpLog ['connect'] = fgets($ smtpConn);

//如果成功启动SMTP通信
if($ smtpConn!== false)
{
//向hello发送SMTP
fputs ($ smtpConn,'EHLO'。$ _SERVER ['SERVER_ADDR']。NL);
$ smtpLog ['hello'] = fgets($ smtpConn);

//需要身份验证
fputs($ smtpConn,'AUTH LOGIN'。NL);
$ smtpLog ['auth'] = fgets($ smtpConn);

//发送用户名
fputs($ smtpConn,base64_encode($ smtpUser)。NL);
$ smtpLog ['user'] = fgets($ smtpConn);

//发送密码
fputs($ smtpConn,base64_encode($ smtpPwd)。NL);
$ smtpLog ['pwd'] = fgets($ smtpConn);

//发送电子邮件从
fputs($ smtpConn,MAIL FROM:< $ from>NL);
$ smtpLog ['from'] = fgets($ smtpConn);

//发送电子邮件到
fputs($ smtpConn,RCPT TO:< $ to>NL);
$ smtpLog ['to'] = fgets($ smtpConn);

//发送电子邮件
fputs($ smtpConn,'DATA'。NL);
$ smtpLog ['data1'] = fgets($ smtpConn);

fputs($ smtpConn,
To:< $ to> \r\\\
From:< $ from> \r\\\
Subject:$ subject\r\\ \\ n $ message\r\\\
.\r\\\
);
$ 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','主题','讯息');

print_r($ log);

而来自谷歌的SMTP服务器的完整的响应日志:

  [connect] => 220 mx.google.com ESMTP o47sm22737144eem.11 
[hello] => 250-mx.google.com在您的服务,[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无法解码响应o47sm22737144eem.11
[quit] => 530-5.5.1需要验证。了解更多在


解决方案

男人,你读单行回复有多行。



MULTILINE REPLY

  250-LINE#1 //行已经有###继续下一个
250-LINE#2 //行已经有###继续下一个
250 LINE#3 // last line has space after ###

SINGLELINE REPLIES

  220 LINE#1 //一个回复代码220 
250 LINE#2 //另一个回复与代码250
//否###代码后面跟着 - 这意味着:不是最后一行!

您的命令不同步。所以修复如下:如果($ smtpLog ['hello'] = fgets($ smtpConn)){
//处理多行回复$ b,则

  $ b while($ smtpLog ['hello'] {4}!==''){
$ smtpLog ['hello']。= PHP_EOL.fgets($ smtpConn);
}
}

这适用于您收到的所有回复,命令在 EHLO 中断同步。


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

解决方案

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.

这篇关于向Google的SMTP服务器发送电子邮件的问题(使用自己的php脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆