我正在尝试通过 PHPMailer 库在 PHP 脚本中发送邮件。以下代码过去曾成功发送电子邮件且没有出现警告:
function mail_attachment($filename, $path, $mailto, $subject, $message) {
include("PHPMailer_5.2.3/class.phpmailer.php");
include("PHPMailer_5.2.3/class.smtp.php");
$email_config = parse_ini_file('email.cfg');
$from = $email_config['sender'];
$alias = $email_config['alias' ];
$smtp = $email_config['host' ];
$port = $email_config['port' ];
$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->IsHTML(false); // send as HTML?
$mail->WordWrap = 50; // set word wrap
$mail->IsSMTP();
$mail->Host = $smtp; // set the host SMTP server
$mail->Port = $port; // set the host SMTP port
$mail->Subject = $subject;
$mail->Body = $message;
$mail->SetFrom( $from, $alias);
$mail->AddReplyTo($from, $alias);
$mail->AddAddress($mailto);
$mail->AddAttachment($path, $filename);
var_dump( $email_config );
$sent = $mail->Send();
if(!$sent)
echo "Mailer Error: " . $mail->ErrorInfo ."\n";
else
echo "Message has been sent\n";
return $sent;
}
突然间,自动报告电子邮件不再出现在人们的收件箱中,并生成以下消息:
SMTP -> FROM SERVER:220 relay-8.dlfw.twtelecom.net ESMTP Postfix
<br />SMTP -> FROM SERVER: 250-relay-8.dlfw.twtelecom.net
250-PIPELINING
250-SIZE 25600000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
<br />SMTP -> FROM SERVER:250 2.1.0 Ok
<br />SMTP -> FROM SERVER:554 5.7.1 <localhost.localdomain>: Helo command rejected: Impossible, I am localhost
<br />SMTP -> ERROR: RCPT not accepted from server: 554 5.7.1 <localhost.localdomain>: Helo command rejected: Impossible, I am localhost
<br />SMTP Error: The following recipients failed: [email protected]
Mailer Error: SMTP Error: The following recipients failed: [email protected]<p>SMTP server error: 5.7.1 <localhost.localdomain>: Helo command rejected: Impossible, I am localhost
</p>
Mail Was NOT Successfully Sent!
您对代码停止工作的原因有什么想法吗?
问题出在我这边还是远程邮件服务器上?
我有以下主机名配置:
/etc/主机名
Dev07
/etc/hosts
127.0.0.1 localhost
127.0.1.1 lamp
#Required for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
答案1
看起来远程服务器不喜欢您的服务器报告它位于您正在发送的localhost.localdomain
内容中这一事实。HELO
这可能是由于远程服务器将其 SMTP 服务配置为拒绝无效的HELO
s。也可能是由于您的本地服务器上发生了某些变化,导致主机名配置不正确。
我会推荐设置主机名更改为某些内容,这样sendmail
就不会报告为localhost.localdomain
。在此过程中,使用/etc/hosts
服务器的 IP 地址、FQDN 和主机名进行更新通常也是一个好主意。
有教程这里如何去做。
/etc/hostname:
hostname.domain.tld
/etc/hosts:
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
1.2.3.4 hostname.domain.tld hostname
如果您有基于 RedHat/CentOS 的系统,请编辑/etc/sysconfig/network
并设置:
HOSTNAME=hostname.domain.tld