$mail = new PHPMailer(); // create a new object $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = ‘ssl’; // secure transfer enabled required for Gmail $mail->Host = 'smtp.gmail.com”; $mail->Port = 465; // or 587 $mail->IsHTML(true); $mail->Username = 'email@gmail.com”; $mail->Password = “password”; $mail->SetFrom('example@gmail.com”); $mail->Subject = “Test”; $mail->Body = “hello”; $mail->AddAddress('email@gmail.com”);
if(!$mail->Send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }
上面的代码已经过测试并为我工作。
可能是您需要 $mail->SMTPSecure = ’ssl’;
另外,请确保您没有为该帐户启用两步验证,因为这也会引起问题。
更新
您可以尝试将$ mail-> SMTP更改为:
$mail->SMTPSecure = ’tls’;
值得注意的是,某些SMTP服务器会阻止连接。某些SMTP服务器不支持SSL(或TLS)连接。
解决方法我想通过 PHP Mailer* 使用 Gmail SMTP 服务器发送电子邮件。 *
这是我的代码
<?phprequire_once(’class.phpmailer.php’);$mail = new PHPMailer();$mail->IsSMTP();$mail->CharSet='UTF-8';$mail->SMTPSecure = ’tls’;$mail->Host = ’smtp.gmail.com’;$mail->Port = 587;$mail->Username = ’MyUsername@gmail.com’;$mail->Password = ’valid password’;$mail->SMTPAuth = true;$mail->From = ’MyUsername@gmail.com’;$mail->FromName = ’Mohammad Masoudian’;$mail->AddAddress(’anotherValidGmail@gmail.com’);$mail->AddReplyTo(’phoenixd110@gmail.com’,’Information’);$mail->IsHTML(true);$mail->Subject = 'PHPMailer Test Subject via Sendmail,basic';$mail->AltBody = 'To view the message,please use an HTML compatible email viewer!';$mail->Body = 'Hello';if(!$mail->Send()){ echo 'Mailer Error: ' . $mail->ErrorInfo;}else{ echo 'Message sent!';}?>
但我收到以下错误
Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.comSMTP server error: SMTP AUTH is required for message submission on port 587