【问题标题】:SMTP Error: Could not connect to SMTP host. i tried all result found on stackoverflow and githubsSMTP 错误:无法连接到 SMTP 主机。我尝试了在 stackoverflow 和 githubs 上找到的所有结果
【发布时间】:2017-10-21 14:43:21
【问题描述】:

我是 php 新手,我正在尝试使用 phpmailer 使用 gmail 发送电子邮件
这是我写的代码

$mail = new PHPMailer(true);       
try {
    $mail->SMTPDebug = 2;          
    $mail->isSMTP();               
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;                            
    $mail->Username = 'sendermail@gmail.com';      
    $mail->Password = '<some password>';                   
    $mail->SMTPSecure = 'tls';                         
    $mail->Port = 587;                                  
    $mail->setFrom('anadresse@gmail.com', 'GestionStock');
    $mail->addAddress('anadresse@gmail.com', 'hamza');  
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} ?>

我得到的结果是 SMTP 错误:无法连接到 SMTP 主机。

SMTP 错误:无法连接到 SMTP 主机。 无法发送邮件。Mailer 错误:SMTP 错误:无法连接到 SMTP 主机。

谁能告诉我我做错了什么!!

【问题讨论】:

  • 您可以手动连接到端口587 上的smtp.google.com 吗?你身边的防火墙能阻止这种情况吗?
  • 好吧,那么提供的 SMTP 配置显然有问题。顺便说一句,我希望这不是您的真实密码。否则你应该立即改变它!尝试改成$mail-&gt;SMTPSecure = 'ssl';$mail-&gt;Port = 465;
  • @GillesGouaillardet,我该如何手动完成??
  • @ Connum ,我尝试了你所说的,我得到了 2017-10-21 14:49:06 SMTP 错误:无法连接到服务器:(0) SMTP connect() 失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 无法发送邮件。邮件程序错误:SMTP 连接()失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  • 我认为您必须在 gmail 帐户中激活使用 SMTP 的选项,否则将不起作用。例如Gmail Webmail - Remote SMTP Server - Setup Guide(使用谷歌快速搜索)

标签: php phpmailer


【解决方案1】:

我在使用 Gmail 时遇到了同样的问题,我通过从站点 https://curl.haxx.se/docs/caextract.html 下载 cacert.pem 证书解决了它 您还应该编写 php.ini 文件,如下所示:extension = php_openssl.dll openssl.cafile = C: \xampp\php\extras\ssl\cacert.pem 它必须在 GMail 帐户中激活标签:访问和安全选项: 允许不太安全的应用访问选项:开启

这个解决方案要感谢 stackoverflow 的 matteobin 用户贡献

【讨论】:

    【解决方案2】:

    我发现问题不是来自代码,而是来自 gmail 帐户,idk 到底是什么,但我有不同的帐户,有些工作,有些不工作,我尝试创建新的 gmail 帐户并启用 lesssecureapps 但没有工作 它仅适用于具有禁用的 lesssecureapps 的特定 gmail

    【讨论】:

      【解决方案3】:

      以下代码对我有用。

                          require('./phpmailer/PHPMailerAutoload.php');
                          require('./phpmailer/class.phpmailer.php');
                          $mail=new PHPMailer();
                          $mail->SMTPOptions = array(
                              'ssl' => array(
                              'verify_peer' => false,
                              'verify_peer_name' => false,
                              'allow_self_signed' => true
                              )
                              );
      
                          $mail->CharSet =  "utf-8";
                          $mail->Host = "smtp.gmail.com"; 
                          $mail->SMTPSecure = 'ssl';  
                          $mail->Port = 465; 
                          $mail->SMTPAuth=true;
                         
                          $mail->isSMTP();
      
                          $mail->Username="myemail@gmail.com";
                          $mail->Password="mypassword";
      
                          $mail->setFrom('myemail@gmail.com','Some text here');
                          $mail->addAddress($email);
                          $mail->addReplyTo('myemail@gmail.com');
      
                          $mail->isHTML(true);
                          $mail->Subject="Confirmation email";
                          $mail->Body="<h2 style='text-align='center';'>Confirmation email</h2></br>
                          <p>Your message has been received. One of our team members will contact you shortly.</br></br>Thank you for contacting us.</p>";
      
                          if(!$mail->send())
                          {
                              echo "Message could not be sent!";
                              echo $mail->ErrorInfo;
                          }
      
                          else
                          {
                              echo "Message sent successfully!";
                          }
      

      我遇到了同样的问题。我添加了以下代码:

                              $mail->SMTPOptions = array(
                              'ssl' => array(
                              'verify_peer' => false,
                              'verify_peer_name' => false,
                              'allow_self_signed' => true
                              )
                              );
      

      这是因为,在较新的版本中,PHP 实施了更严格的 SSL 行为导致了这个问题。希望这也能解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多