使用PHPMailer通过G Suite发送邮件

时间:2017-12-04 01:33:36

标签: php email phpmailer google-apps gsuite

我正在尝试通过G Suite管理的电子邮件帐户发送电子邮件。我一直收到连接错误。我已经尝试更改各种设置并使用tls或ssl,但它似乎没有任何区别。我已经验证了登录凭据,所以我知道这些不是问题。通过此帐户发送的电子邮件工作正常,因此MX记录也已正确配置。

require "PHPMailer-master/PHPMailerAutoload.php";
include "info/user.inc";

$name = trim(htmlspecialchars($_POST['name']));
$email = trim(htmlspecialchars($_POST['email']));
$subject = trim(htmlspecialchars($_POST['subject']));
$phone = trim(htmlspecialchars($_POST['phone']));
$extension = "Ext. ".trim(htmlspecialchars($_POST['extension']))."";
if($etension == null) {
    $extension = "";
}

$message = nl2br(trim(htmlspecialchars($_POST["message"])));

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //ssl or tls
$mail->Host = "smtp.gmail.com";
$mail->Port = "587"; //465 or 587
$mail->Username = $user;
$mail->Password = $pass;

$mail->SetFrom($email, $name);
$mail->AddAddress($address);
$mail->AddReplyTo($email);

$mail->WordWrap = 50;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;

if(!$mail->Send()) {   
    echo "Message could not be sent. Please try again later.";
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
    header("Location: ../success.html");
}

调试设置为2时,出现以下错误:

2017-12-04 01:24:36 SERVER -> CLIENT: 220-a2ss55.a2hosting.com ESMTP Exim 4.89 #1 Sun, 03 Dec 2017 20:24:36 -0500 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. 
2017-12-04 01:24:36 CLIENT -> SERVER: EHLO www.tigertorch.ca 
2017-12-04 01:24:36 SERVER -> CLIENT: 250-a2ss55.a2hosting.com Hello www.tigertorch.ca [68.66.216.5] 250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP 
2017-12-04 01:24:36 CLIENT -> SERVER: STARTTLS 
2017-12-04 01:24:36 SERVER -> CLIENT: 220 TLS go ahead 
2017-12-04 01:24:36 SMTP Error: Could not connect to SMTP host. 
2017-12-04 01:24:36 CLIENT -> SERVER: QUIT 
2017-12-04 01:24:36 SERVER -> CLIENT: 221 a2ss55.a2hosting.com closing connection 
2017-12-04 01:24:36 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent. Please try again later.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

0 个答案:

没有答案