PHPMailer 说无法连接到 SMTP 主机

DrD*_*DrD 1 php email connect phpmailer swiftmailer

在过去的 2 天里,我一直在尝试使用 PHP 脚本来发送电子邮件,但它似乎不起作用。首先我尝试了普通的 php 邮件功能,然后是 PHPMailer,然后是 Swiftmailer。

我尝试同时使用 gmail 的帐户和 SMTP 以及我的互联网提供商的电子邮件帐户和 SMTP,但都没有奏效。我尝试 ping 两个 SMTP 服务器,它们工作正常,telnet 也正确回复。在 telnet smtp.gmail.com 587 上,它说 220 smtp.gmail.com ESMTP q125sm9630397wmd.19 - gsmtp。我已经尝试了端口 465 和 587 以及与 tls 和 ssl 的组合。对于 gmail,我还编辑了我的 gmail 帐户,以确保它允许用于不太安全的设备并允许 imap 等。

我在 Windows 7 上的我自己的电脑上运行 apache。我已经转发了端口 465 和 587。我已经浏览了 PHPMailer 的故障排除页面。我还禁用了防火墙进行测试,但这没有帮助。

我已经正确编辑了 php.ini 设置,并确保了 ; 已从 extension=php_openssl.dll 中删除。

这是我的 PHPMailer 代码,基于为 gmail 提供的示例 PHPMailer(我编辑了 SMTP、帐户和密码):

require '/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.x.x';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'x@x.x';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('x@x.x', 'Mailer');
$mail->addAddress('x@x.x');     // Add a recipient

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
$checkup = 'Something went wrong';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$checkup = 'Success';
}
Run Code Online (Sandbox Code Playgroud)

当我在我的互联网提供商的 SMTP 上使用我的互联网提供商电子邮件地址时,我收到此错误:

2016-05-06 12:27:04 Connection: opening to smtp.x.x:587, timeout=300, options=array ( ) 2016-05-06 12:27:04 
Connection: opened 2016-05-06 12:27:04  
SERVER -> CLIENT: 220-smtp6.mnd.mail.iss.as9143.net ESMTP Fri, 06 May 2016 14:27:07 +0200 220-Sending spam or unsolicited commercial e-mail to this server is strictly 220-prohibited by our NO UBE / NO UCE policy. Abuse will be prosecuted and/or 220 charged per attempted recipient at international postal rates. 2016-05-06 12:27:04 
CLIENT -> SERVER: EHLO localhost 2016-05-06 12:27:04    
SERVER -> CLIENT: 250-smtp6.mnd.mail.iss.as9143.net Hello (MY IP ADDRESS, edited out for security) 250-SIZE 31457280 250-8BITMIME 250-PIPELINING 250-STARTTLS 250 HELP 2016-05-06 12:27:04
CLIENT -> SERVER: STARTTLS 2016-05-06 12:27:04  SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 12:27:04  SMTP Error: Could not connect to SMTP host. 2016-05-06 12:27:04 CLIENT -> SERVER: QUIT 2016-05-06 12:27:04  
SERVER -> CLIENT: MIA?x?????dqi7??=???&J??3??????S?_??Z?$??NH???|??m>[z@?f?g??G????.pAGD?8Q?:??J???'L?a8%??v?(*YC??u??W?s????l?#6F?w???? ???9?|?\%?!?????B=:??/U??m??bc\?c???4??d?-U??seEZ?PC??Q?Ai`J?K?U?]h???H?v???i???cF M??x ???}???V?HR?c?!??,`?   *)?$O???EM??&B?+???u9A?R??j'[7??y????a ????N???? 2016-05-06 12:27:04    
SMTP ERROR: QUIT command failed: MIA?x?????dqi7??=???&J??3??????S?_??Z?$??NH???|??m>[z@?f?g??G????.pAGD?8Q?:??J???'L?a8%??v?(*YC??u??W?s????l?#6F?w???? ???9?|?\%?!?????B=:??/U??m??bc\?c???4??d?-U??seEZ?PC??Q?Ai`J?K?U?]h???H?v???i???cF  M??x ???}???V?HR?c?!??,`?   *)?$O???EM??&B?+???u9A?R??j'[7??y????a ????N???? 2016-05-06 12:27:04    
Connection: closed 2016-05-06 12:27:04  
SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,SMTP 服务器似乎通过 IP 识别出我并说“继续”,然后它突然失败了。我一直在到处寻找,但我完全不知道如何解决它。

当我使用 gmail 的 SMTP + gmail 帐户时,它说:

2016-05-06 17:42:40 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2016-05-06 17:42:40   
Connection: opened 2016-05-06 17:42:40  
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 8sm9564071wms.14 - gsmtp 2016-05-06 17:42:40 CLIENT -> SERVER: EHLO localhost 2016-05-06 17:42:40    
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [MY IP ADDRESS] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250 SMTPUTF8 2016-05-06 17:42:40   
CLIENT -> SERVER: STARTTLS 2016-05-06 17:42:40  
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2016-05-06 17:42:40  SMTP Error: Could not connect to SMTP host. 2016-05-06 17:42:40
CLIENT -> SERVER: QUIT 2016-05-06 17:42:40  
SERVER -> CLIENT: MIA??b8   ??4}?5KO?q0$&??B.r??Q???P??=???r$?%?,??X?"?c?5??D?C?b???B??@??T??b?_?*?????X1`T%_??? ?QQ????y}]OO?????8d?R|?$?????b?????[? ,??5?[*?H????5?l???B????a????f??f???Pj??c@>????vk+Cq?r*??-?4???`W?A``A?S ,?w:?q?1?????.????*?',???iH?T0?E?/1W??  |?i4?{?Gk?J?n,US?F?|???|L????n}Qp? 2016-05-06 17:42:40  
SMTP ERROR: QUIT command failed: MIA??b8    ??4}?5KO?q0$&??B.r??Q???P??=???r$?%?,??X?"?c?5??D?C?b???B??@??T??b?_?*?????X1`T%_??? ?QQ????y}]OO?????8d?R|?$?????b?????[? ,??5?[*?H????5?l???B????a????f??f???Pj??c@>????vk+Cq?r*??-?4???`W?A``A?S ,?w:?q?1?????.????*?',???iH?T0?E?/1W??  |?i4?{?Gk?J?n,US?F?|???|L????n}Qp? 2016-05-06 17:42:40  
Connection: closed 2016-05-06 17:42:40  SMTP connect() failed.  Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Run Code Online (Sandbox Code Playgroud)

在调试模式 4 上,它说的是相同的(这仅显示了您当时获得的巨大文本墙的一部分):

CLIENT -> SERVER: STARTTLS 2016-05-06 17:55:10 
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10    
SMTP -> get_lines(): $str is "220 TLS go ahead " 2016-05-06 17:55:10    
SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 17:55:10  SMTP Error: Could not connect to SMTP host. 2016-05-06 17:55:10 
CLIENT -> SERVER: QUIT 2016-05-06 17:55:10  
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10    
SMTP -> get_lines(): $str is "MIA " 2016-05-06 17:55:10 
SMTP -> get_lines(): $data is "MIA " 2016-05-06 17:55:10
Run Code Online (Sandbox Code Playgroud)

它说准备启动 TLS,然后它说连接再次失败。我在 stackoverflow 上阅读了很多关于此的主题,但到目前为止,那里提供的解决方案和选项都没有奏效。有谁有想法吗?

Dhr*_*ruv 6

@David Kariuki 解决方案中的 SMTPOptions 行解决了我的问题。由于 TLS 握手失败,SMTP 连接失败。

$mail->SMTPOptions = array(
                  'ssl' => array(
                      'verify_peer' => false,
                      'verify_peer_name' => false,
                      'allow_self_signed' => true
                  )
              );
Run Code Online (Sandbox Code Playgroud)