【问题标题】:Class SMTP not found in phpmailer在 phpmailer 中找不到类 SMTP
【发布时间】:2015-04-14 10:41:54
【问题描述】:

我的 php 邮件功能遇到了这个问题。我尝试更改所需的文件,但仍然相同。谁能帮我解决这个错误。

这是我的 phpmailer 代码:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "testnoreply@gmail.com";
$mail->Password = "test";
$mail->SetFrom("testnoreply@gmail.com");
$mail->Subject = "Membership expire notice";
$mail->Body = "Dear ICONIS member your membership is going to expire please renew it";
$mail->AddAddress($em);

if (!$mail->Send()) {
  echo "Mailer Error: " .$mail->ErrorInfo;
} else {
  echo "Mail has been sent";
}

【问题讨论】:

  • 你是如何加载库的?
  • require_once("PHPMailer-master/class.phpmailer.php");
  • 你应该包含它的自动加载器而不是require 'PHPMailerAutoload.php';,如github.com/PHPMailer/PHPMailer的示例所示
  • 还是一样..

标签: php email gmail


【解决方案1】:

您确定您的文件夹中同时拥有 class.phpmailer.php 和 class.smtp.php 吗?

在包含class.phpmailer.php之前尝试包含class.smtp.php,如下:

<?php
include 'class.smtp.php';
include 'class.phpmailer.php';
// your code goes here

然后告诉你是否有错误。

【讨论】:

    【解决方案2】:

    只需下载所有文件,将文件解压缩到一个文件夹中并从该文件夹中包含 PHPMailerAutoload.php。

    <?php
    /**
     * This example shows settings to use when sending via Google's Gmail servers.
     */
    //SMTP needs accurate times, and the PHP time zone MUST be set
    //This should be done in your php.ini, but this is how to do it if you don't have access to that
    date_default_timezone_set('Etc/UTC');
    
    /*======================================*/
    /*======================================*/
    require 'YOURFOLDERNAME/PHPMailerAutoload.php';
    /*======================================*/
    /*======================================*/
    
    
    //Create a new PHPMailer instance
    $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';
    // use
    // $mail->Host = gethostbyname('smtp.gmail.com');
    // if your network does not support SMTP over IPv6
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "username@gmail.com";
    //Password to use for SMTP authentication
    $mail->Password = "yourpassword";
    //Set who the message is to be sent from
    $mail->setFrom('from@example.com', 'First Last');
    //Set an alternative reply-to address
    $mail->addReplyTo('replyto@example.com', 'First Last');
    //Set who the message is to be sent to
    $mail->addAddress('whoto@example.com', 'John Doe');
    //Set the subject line
    $mail->Subject = 'PHPMailer GMail SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';
    //Attach an image file
    $mail->addAttachment('images/phpmailer_mini.png');
    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    

    【讨论】:

      【解决方案3】:

      试试

      require 'PHPMailerAutoload.php';
      $mail = new PHPMailer;
      

      而不是

      require 'class.phpmailer.class.php';
      $mail = new PHPMailer;
      

      【讨论】:

        猜你喜欢
        • 2022-12-15
        • 1970-01-01
        • 1970-01-01
        • 2017-08-19
        • 2018-12-19
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多