PHP邮件GMAIL SMTP错误


PHP MAILER GMAIL SMTP ERROR

我目前正处于项目的测试模式,已经设置了PHPILER(5.2.9版)库,并希望使用Gmail SMTP生成电子邮件。

我已经在本地系统上设置了脚本,但当脚本执行时,它会一直等待localhost响应,即使我已经指定了smtp服务器地址。

我目前正在使用以下内容:

PHP 5.4.7版examplep 1.8.1版本(我知道它已经过时了)phpmailer版本-5.2.9

下面是脚本:

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxx@gmail.com';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'xxx@gmail.com';
$mail->FromName = 'test';
$mail->addAddress('xxx@outlook.com');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

感谢

您的代码应该是:

require 'phpmailer/PHPMailerAutoload.php';
  $mail = new PHPMailer;
  //Tell PHPMailer to use SMTP
  $mail->isSMTP();
  //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail->SMTPDebug = 2;
  //Ask for HTML-friendly debug output
  $mail->Debugoutput = 'html';
  //Set the hostname of the mail server
  $mail->Host = 'smtp.gmail.com';
  //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  $mail->Port = 587;
  $mail->SMTPSecure = 'tls';
  $mail->SMTPAuth = true;
  $mail->Username = "username@gmail.com";
  $mail->Password = "yourpassword";
  //Set who the message is to be sent from
  $mail->setFrom('from@example.com', 'First Last');
  $mail->addReplyTo('replyto@example.com', 'First Last');
  $mail->addAddress('whoto@example.com', 'John Doe');
  $mail->WordWrap = 50;                                 
  $mail->isHTML(true);                                  
  $mail->Subject = 'Here is the subject';
  $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  if(!$mail->send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail->ErrorInfo;
  } else {
      echo 'Message has been sent';
  } 

$mail->SMTPDebug = 3; 没有选项

所以问题似乎出在php.ini文件中的一个设置上。未发表评论;extension=php_opensl.dll,删除前面的;它奏效了。

确保您的phpmailer脚本运行正常,如果mailer没有问题,则使用您的gmail帐户和goto->gmail设置登录。现在检查gmail设置中的允许不太安全选项。默认情况下其关闭

允许不太安全的应用程序:关闭

确保此设置为ON。

完成后,现在从浏览器运行您的脚本。

如果您没有找到链接,请单击此处转到https://myaccount.google.com/security#activity

Try if SMTP issues + using TSL+ Gmail
<?php
include'PHPMailer/PHPMailerAutoload.php';
include "PHPMailer/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
//$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "mygmail@gmail.com"; 
$mail->Password = "mypassword";
$mail->addReplyTo("mygmail@gmail.com","user");
$mail->SetFrom("mygmail@gmail.com","My Site");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("your@gmail.com");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>