【问题标题】:PHP mailer not working on cpanelPHP邮件程序无法在cpanel上运行
【发布时间】:2016-08-14 07:56:50
【问题描述】:

我的 php 邮件程序在 localhost 上运行良好,但是当我在 cpanel 上运行相同的代码时,我收到错误消息:SMTP connect() failed

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                     
$mail->Host = "tls://smtp.gmail.com"; 
$mail->SMTPAuth = true;                               
$mail->Username = '********@gmail.com';                 
$mail->Password = '********';                           

$mail->Port = 587;                                   

$mail->setFrom('sender@gmail.com', 'Mailer');
$mail->AddAddress('receiver@gmail.com', 'Joe User');    
$mail->addReplyTo('sender@gmail.com', 'Information');

$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';
}
?>

请帮助我,我哪里出错了?

【问题讨论】:

  • 如果您注释掉对addAddress 的调用,则该消息不会发送给任何人,那么您为什么希望它起作用?!您可以尝试与您收到的错误消息相关联的reading the docs
  • 但是在取消注释 $mail->AddAddress() 时出现错误:SMTP connect() failed
  • 不设置收件人就无法发送,不是设置一个导致连接错误!
  • 您确认您的 Cpanel 机器已为 OUT 开放端口 587 吗? “SMTP 错误:无法连接到 SMTP 主机。”这也可能显示为 SMTP connect() failed 或 Called Mail() without being connected 在调试输出中。这通常被报告为 PHPMailer 问题,但它几乎总是归结为本地 DNS 故障、防火墙阻止或本地网络上的其他问题。这意味着 PHPMailer 无法联系您在 Host 属性中指定的 SMTP 服务器,但没有说明具体原因。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

标签: php email localhost phpmailer cpanel


【解决方案1】:

您需要从 ISP 公司购买 Cpanel/WHM 才能访问外部 SMTP 电子邮件,例如 Gmail。首先,禁用:WHM > 安全中心 > SMTP 限制已禁用。
并确保启用 CSF 防火墙“SMTP_BLOCK”设置。 如果您在安全设置中使用 Gmail,请启用较少的安全性,您可以使用 smtp 身份验证。 PHPMailer 建议对所有防火墙和安全问题使用 SSL。

【讨论】:

    【解决方案2】:

    CPanel 默认阻止访问外部 SMTP 服务器。

    在 whm > 安全中心 > SMTP 限制禁用此限制

    这行得通

    <?php
    require_once('./class.phpmailer.php');
    $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.mail.yahoo.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "xxxxxx@ymail.com";
    $mail->Password = "xxxxxx";
    $mail->SetFrom("xxxxx@ymail.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("xxxxx@ymail.com");
    
    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }?>
    

    【讨论】:

      【解决方案3】:

      感谢您的建议,非常感谢:-)

      我得到的解决方法如下。

      1>give Absolute path for PHPMailerAutoload.php
      2>host name as "localhost"
      3>create dummy emailId on server
      
      
      
      <?php
      require'/home/username/public_html/phpmailertesting/PHPMailer/PHPMailerAutoload.php';
      $mail = new PHPMailer;
      $mail->SMTPDebug =3;                               // Enable verbose debug output
      $mail->isSMTP();                                      // Set mailer to use SMTP
      $mail->Host = 'localhost';  // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Username = 'info@hostname.com';                 // SMTP username
      $mail->Password = '*****';                           // SMTP password
      $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                                    // TCP port to connect to
      $mail->setFrom('info@hostname.com', 'Mailer');
      $mail->addAddress('*******@gmail.com');               // Name is optional
       //$mail->addReplyTo('info@hostname.com', 'Information');
       //$mail->addCC('*******@gmail.com');
       //$mail->addBCC('bcc@example.com');
      //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
      //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
      $mail->isHTML(true);                                  // Set email format to HTML
      $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;
      exit();
      } else {
      echo 'Message has been sent';
      }
      ?>
      

      【讨论】:

        【解决方案4】:

        您可能忘记启用对您的 GMail 帐户的 SMTP 访问(它是设置中 IMAP 访问的一部分)。

        此外,“tls://smtp.gmail.com”不是有效的 SMTP 服务器地址。如果要使用 TLS,请使用 $mail-&gt;SMTPSecure = "tls";

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        • 2013-10-16
        • 1970-01-01
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多