PHP MAILER GMAIL SMTP错误

时间:2014-10-11 05:56:09

标签: php email smtp phpmailer

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

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

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

PHP版本5.4.7 xampp版本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';
}

感谢

4 个答案:

答案 0 :(得分:0)

您的代码应为:

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;

没有选项

答案 1 :(得分:0)

所以似乎问题出在php.ini文件中的设置。未注释; extension = php_openssl.dll删除前面的;它起作用了。

答案 2 :(得分:0)

确保您的phpmailer脚本运行正常,如果邮件程序没有问题,请使用您的Gmail帐户登录并转到 - > gmail设置。 现在检查gmail设置中的allow less secure选项。 默认为关闭

允许安全性较低的应用:关闭

确保此设置为开。

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

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

答案 3 :(得分:0)

&#13;
&#13;
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";
}
?>
&#13;
&#13;
&#13;