PHPMailer邮件错误 - > SMTP连接()

时间:2016-09-12 07:31:31

标签: php email gmail phpmailer

我不知道为什么会收到此错误PHPMailer Mail Error - >SMTP connect()。我该如何解决?我不知道我该怎么办我需要完全解释,我是PHP的新手

<?php
    require '../plugins/phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->CharSet = "utf-8";
    $mail->IsSMTP();
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth = true;
    $mail->Username = "myemail@gmail.com";
    $mail->Password = "mypass";
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = "587";

    $mail->setFrom('your_gmail@gmail.com', 'your name');
    $mail->AddAddress('to_mail@mail.com', 'receivers name');

    $mail->Subject = 'using PHPMailer';
    $mail->IsHTML(true);
    $mail->Body = 'Hi there ,
                            <br />
                            this mail was sent using PHPMailer...
                            <br />
                            cheers... :)';

    if ($mail->Send()) {
        echo "Message was Successfully Send :)";
    } else {
        echo "Mail Error - >" . $mail->ErrorInfo;
    }
    ?>

2 个答案:

答案 0 :(得分:1)

由于SMTP连接失败导致错误。所以,首先检查您的配置,您可以通过注释行检查$ mail-&gt; IsSMTP();

// $mail->IsSMTP();

见以下是工作演示:

  <?php
   require 'phpmailer.php';
   require 'smtp.php';
   $mail = new PHPMailer;
  //$mail->IsSMTP(); // telling the class to use SMTP
   $mail->Host       = "smtp.gmail.com"; // SMTP server
   $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
   $mail->SMTPAuth   = true;                  // enable SMTP authentication
   $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
   $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
   $mail->Username   = GMAIL EMAIL ID; // SMTP account username
   $mail->Password   = GMAIL PASSWORD;        // SMTP account password
   $mail->SMTPSecure = 'ssl';


   $mail->From = 'from@example.com';
   $mail->FromName = 'Mailer';
   $mail->addAddress('MAIL ID to whom you eant to send');               // Name is optional

  $mail->addCC('CC EMAIL ID');
  $mail->addBCC('BCC EMAIL ID');
  $mail->WordWrap = 50;                                 // Set word wrap to    50 characters

  $mail->Subject = 'Here is the subject';
  $mail->Body    = 'MESSAGE';
  $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';
 }
    ?>

答案 1 :(得分:0)

更改行:

$mail->SMTPSecure = "ssl";

要:

$mail->SMTPSecure = "tls";

许多邮件服务器不再允许使用SSL,因为它存在一些安全问题。