【问题标题】:How to configure PHPmailer to sendmail and Gmail for localhost?如何配置 PHPmailer 为本地主机发送邮件和 Gmail?
【发布时间】:2015-01-27 14:34:53
【问题描述】:

一个多星期以来,我一直在寻找答案。我被这个问题困住了。我已经下载了 sendmail 文件夹并进行了配置。是的,它说发送了真实的消息,但实际上它并没有发送到邮件中。所以我决定把 PHPmailer 放在我在一些网站上读过的地方。我下载了 PHPmailer 并对其进行了配置。当我运行它时出现错误。致命错误:在第 1197 行的 C:\wamp\www\Project\PHPMailer\class.phpmailer.php 中找不到类“SMTP”。 而且我还阅读了一些将代码粘贴到 104 #connect 到 smtp 服务器之前的解决方案。但我的第 104 行看起来像这样。 公共 $CRLF = "\r\n";

/**
 * Debug output level.

 * Options:
 * * self::DEBUG_OFF (`0`) No debug output, default
 * * self::DEBUG_CLIENT (`1`) Client commands
 * * self::DEBUG_SERVER (`2`) Client commands and server responses
 * * self::DEBUG_CONNECTION (`3`) As DEBUG_SERVER plus connection status
 * * self::DEBUG_LOWLEVEL (`4`) Low-level data output, all messages
 * @type integer
 */
public $do_debug = self::DEBUG_OFF;

你们能帮帮我吗?我卡在这里了。

【问题讨论】:

  • 调试代码参考什么? PHPMailer 有一个调试功能,能提供更多线索吗?
  • 您只需要read the docs,按照提供的任何示例进行操作即可;您需要使用自动加载器或自己加载 SMTP 类以避免此问题。

标签: php sendmail phpmailer


【解决方案1】:

你有最新的带有自动加载器的 PHPMailer 吗?

请参阅https://github.com/PHPMailer/PHPMailer,因为邮件程序不再从 PHPMailer 类中调用 SMTP 类。当我更新为直接调用邮件程序产生此错误时,这很痛苦。

同时打开 PHPMailers 调试反馈,看看它说了什么。

【讨论】:

    【解决方案2】:

    这是我在很多项目中使用的 sendMail() 方法。它使用 PHPMailer。

        function sendMail($toAddress, $subject, $body, $AttachmentFilePath) {
        $mail = new PHPMailer();
    
        $mail->IsSMTP ();
        $mail->CharSet = 'UTF-8';
        // nable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = 0; // To prevent any outpur
        // Ask for HTML-friendly debug output
        $mail->Debugoutput = 'html';
        // Get the hostname of the mail server
        $mail->Host = 'smtp.gmail.com';
        // Get the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 587;
        // Get 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 = "your.email@gmail.com";
        // Password to use for SMTP authentication. Specific to the Lead Qualifier Tool
        $mail->Password = "YourPassWordHere";
    
        // Set who the message is to be sent from
        $mail->SetFrom ( 'your.email@gmail.com', 'Your Name' );
    
        // To Address
        $mail->AddAddress ( $toAddress, $toAddress );
    
        $mail->Subject = $subject;
    
        $mail->Body = $body;
        $mail->AltBody = $body;
    
        // Add attachment
        if ($AttachmentFilePath != NULL)
            $mail->AddAttachment ( $AttachmentFilePath );
    
        if (! $mail->Send ()) {
            echo "<br />Error while sending e-mail: " . $mail->ErrorInfo;
        } else {
            // echo "Message sent!";
        }
    }
    

    希望对您有所帮助! :-)

    另外,由于您使用的是 GMail,请确保您创建一个 ASP(应用程序特定密码)并在此处使用它,而不是您的真实密码。

    【讨论】:

    • @DaniloEscapa 你能告诉我你面临的错误是什么吗?
    • 尝试在 $body 参数中传递一些东西。您应该会在收件人的收件箱中看到它。
    • 邮件未发送 PHPMailer 错误:以下发件人地址失败:verbz95@gmail.com :调用 Mail() 时未连接
    • 让我们在聊天中继续。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2021-02-12
    • 2015-04-15
    • 1970-01-01
    • 2018-05-20
    • 2020-02-07
    • 2015-05-29
    相关资源
    最近更新 更多