使用PHPMailer通过smtp.com发送邮件失败求助
问题背景
我无法通过smtp.com的SMTP服务器发送邮件。最初遇到getaddrinfo failed: name or service not known错误,现已修复,但仍存在连接后无法完成EHLO/HELO、AUTH验证的问题,最终连接被远程服务器重置。
当前配置信息
Server: send.smtp.com Port: 25 Use encrypted protocol: No Authentication: requires username/password
已尝试的操作
- 与smtp.com技术支持确认配置无误
- 尝试切换端口(80、2525)
- 临时关闭RHEL 7服务器上的Apache防火墙
- 使用IP地址替代域名作为服务器地址
测试邮件发送错误日志
SMTP -> FROM SERVER: SMTP -> FROM SERVER: SMTP -> ERROR: EHLO not accepted from server: SMTP -> FROM SERVER: SMTP -> ERROR: HELO not accepted from server: SMTP -> ERROR: AUTH not accepted from server: object(SMTP)#35 (7) { ["SMTP_PORT"]=> int(25) ["CRLF"]=> string(2) " " ["do_debug"]=> bool(true) ["do_verp"]=> string(2) "on" ["smtp_conn":"SMTP":private]=> resource(474) of type (stream) ["error":"SMTP":private]=> array(3) { ["error"]=> string(29) "AUTH not accepted from server" ["smtp_code"]=> bool(false) ["smtp_msg"]=> bool(false) } ["helo_rply":"SMTP":private]=> NULL } SMTP -> NOTICE: EOF caught while checking if connected SMTP -> FROM SERVER: SMTP -> FROM SERVER: SMTP -> ERROR: EHLO not accepted from server: SMTP -> FROM SERVER: SMTP -> ERROR: HELO not accepted from server: SMTP -> ERROR: AUTH not accepted from server: object(SMTP)#35 (7) { ["SMTP_PORT"]=> int(25) ["CRLF"]=> string(2) " " ["do_debug"]=> bool(true) ["do_verp"]=> string(2) "on" ["smtp_conn":"SMTP":private]=> resource(476) of type (stream) ["error":"SMTP":private]=> array(3) { ["error"]=> string(29) "AUTH not accepted from server" ["smtp_code"]=> bool(false) ["smtp_msg"]=> bool(false) } ["helo_rply":"SMTP":private]=> NULL } SMTP -> NOTICE: EOF caught while checking if connected
Telnet测试结果
# telnet send.smtp.com 25 Trying 192.40.165.68 Connected to send.smtp.com Escape character is '^]'
/var/log/maillog记录
May 23 08:20:38 txapone sendmail[68217]: w4MHtNJF053686: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=18:25:15, xdelay=00:00:40, mailer=relay, pri=1740402, relay=send.smtp.com [192.40.165.69], dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com May 23 08:20:38 txapone sendmail[68217]: w4MHvv5c053871: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=18:22:40, xdelay=00:00:00, mailer=relay, pri=1740402, relay=send.smtp.com, dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com May 23 08:20:38 txapone sendmail[68217]: w4MHakO3053279: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=18:43:52, xdelay=00:00:00, mailer=relay, pri=1830402, relay=send.smtp.com, dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com May 23 08:20:38 txapone sendmail[68217]: w4MHcf5U053351: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=18:41:57, xdelay=00:00:00, mailer=relay, pri=1830402, relay=send.smtp.com, dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com May 23 08:20:38 txapone sendmail[68217]: w4MHmc4h053590: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=18:32:00, xdelay=00:00:00, mailer=relay, pri=1830402, relay=send.smtp.com, dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com May 23 08:22:46 txapone sendmail[68288]: w4NCMkVH068288: from=apache, size=121, class=0, nrcpts=1, msgid=<201805231222.w4NCMkVH068288@txapone.tframes.local>, relay=apache@localhost May 23 08:22:46 txapone sendmail[68289]: w4NCMk5M068289: from=<apache@server.domain.local>, size=402, class=0, nrcpts=1, msgid=<201805231222.w4NCMkVH068288@txapone.tframes.local>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1] May 23 08:22:46 txapone sendmail[68288]: w4NCMkVH068288: to=me@mydomain.com, ctladdr=apache (48/48), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30121, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (w4NCMk5M068289 Message accepted for delivery) May 23 08:23:27 txapone sendmail[68291]: w4NCMk5M068289: to=<me@mydomain.com>, ctladdr=<apache@server.domain.local> (48/48), delay=00:00:41, xdelay=00:00:41, mailer=relay, pri=120402, relay=send.smtp.com [192.40.165.68], dsn=4.0.0, stat=Deferred: Connection reset by send.smtp.com
使用的PHP脚本片段
<?php require("src/PHPMailer.php"); require("src/SMTP.php"); require("src/Exception.php"); $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->IsSMTP(); $mail->Host = "send.smtp.com"; $mail->SMTPAuth = true; $mail->Username = "webmaster@domain.com"; $mail->
排查与解决方案建议
从错误日志和maillog来看,核心问题是连接到send.smtp.com后被远程服务器重置,同时EHLO/HELO、AUTH步骤都无法完成,结合你已经做的操作,建议尝试以下几点:
强制使用加密连接
虽然你当前配置的是不使用加密,但很多SMTP服务商现在强制要求加密传输(即使端口25也可能需要)。尝试在PHPMailer中开启SSL或TLS:$mail->SMTPSecure = 'tls'; // 或者'ssl',对应端口587或465 $mail->Port = 587; // TLS常用端口注意:smtp.com的标准加密端口是465(SSL)和587(TLS),优先尝试这两个端口而非25/80/2525。
修复CRLF配置错误
从错误日志里看到["CRLF"]=> string(2) " ",这明显有问题——CRLF应该是"\r\n",而不是空格。这会导致SMTP命令格式错误,服务器无法识别EHLO/HELO指令。修复PHPMailer的CRLF配置:$mail->CRLF = "\r\n"; $mail->LE = "\r\n";这个配置错误是导致EHLO/HELO被拒绝的关键原因之一。
再次验证SMTP账户信息
虽然你和技术支持确认过配置,但建议再次核对用户名(通常是你的smtp.com账户邮箱或授权用户名)和密码,确保没有拼写错误、特殊字符转义问题。可以开启更详细的调试日志排查交互细节:$mail->SMTPDebug = 2; // 输出详细的SMTP交互日志检查服务器IP是否被拉黑
maillog里多次出现Connection reset by send.smtp.com,有可能是你的服务器IP被smtp.com的反垃圾邮件系统标记了。可以联系smtp.com技术支持查询IP信誉,或者尝试从其他IP测试发送,确认是否是IP的问题。绕过本地sendmail代理
从maillog来看,你的PHP脚本发送的邮件先经过了本地sendmail转发,而不是直接连接smtp.com。可以修改PHPMailer配置,确保直接连接SMTP服务器:$mail->isSMTP(); // 确保启用SMTP模式,不使用sendmail $mail->SMTPAutoTLS = true; // 自动启用TLS
内容的提问来源于stack exchange,提问作者AllisonC

