我想使用SMTP
和PHP
发送邮件,我正在使用PHPMailer
。
这是我的代码:
<?php
require 'class.phpmailer.php';
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$body = 'Test Mail';
echo $body + '\r\n';
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls'; // sets the prefix to the server
$mail->Host = 'smtp.gmail.com'; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = 'mail@gmail.com'; // GMAIL username
$mail->Password = '<password>'; // GMAIL password
$mail->SetFrom('mail@gmail.com', '<Name>');
$mail->AddReplyTo("replytomail@mail.com","<Name>");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->MsgHTML($body);
$address = "mailto@mail.com";
$mail->AddAddress($address, "<Name>");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
错误是:
2016-01-28 04:37:27 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 11sm12390018pfq.87 - gsmtp
2016-01-28 04:37:27 CLIENT -> SERVER: EHLO ebusinessservices.in
2016-01-28 04:37:27 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [118.67.244.60] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2016-01-28 04:37:27 CLIENT -> SERVER: STARTTLS
2016-01-28 04:37:28 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2016-01-28 04:37:56 SMTP Error: Could not connect to SMTP host.
2016-01-28 04:37:56 SMTP NOTICE: EOF caught while checking if connected
2016-01-28 04:37:56 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
我尝试应用StackOverflow的答案,检查了凭据,尝试使用SSL与端口465但是得到相同的错误。
在我的Gmail帐户中,Less Secure Apps
被允许,并且禁用了两步验证。我尝试将谷歌提供的所有设置应用于此工作,但仍然出现相同的错误。
即使根据一些答案,我也从我的密码中删除了特殊字符,以确保它不能用作转义字符,但仍无法正常工作。
请让我知道对此的建议。
我正在尝试从服务器运行脚本,即文件已上传到Web服务器。
更新1:使用我的Google凭据尝试使用内置脚本编写PHPMailer后,这是一个错误日志。
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP b28sm13608414pfd.24 - gsmtp
CLIENT -> SERVER: EHLO <name>
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [118.67.244.60]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
SMTP NOTICE: EOF caught while checking if connected
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting