【问题标题】:PHPMailer keeps loading pagePHPMailer不断加载页面
【发布时间】:2022-01-06 13:27:40
【问题描述】:

我一直在尝试使用 PHPMailer 从我的 localhost(XAMPP) 发送邮件,但页面一直在加载。一段时间后显示

致命错误:C:\xampp\htdocs\test\vendor\phpmailer\phpmailer\src\SMTP.php 第 1227 行中的最大执行时间超过 120 秒

use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->SMTPDebug = 2;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'my_email';                     //SMTP username
    $mail->Password   = 'my_password';                               //SMTP password
    // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->SMTPSecure = 'tls';            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    // $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('my_email', 'Elexis');
    $mail->addAddress('recipient email.com');     //Add a recipient
    // $mail->addAddress('ellen@example.com');               //Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $body= '<strong>This is my first email with php mailer</strong>';
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'This Is My First Email';
    $mail->Body    = $body;
    $mail->AltBody = strip_tags($body);

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

我该如何解决?

【问题讨论】:

  • 你打开了调试器,那么你在错误日志中得到了什么

标签: php server phpmailer


【解决方案1】:

尝试将php.ini 中的max_execution_time 增加到300(5 分钟)。

我认为 2 分钟的执行时间不足以让 SMTP 响应。

【讨论】:

    【解决方案2】:

    一般来说,SMTP 不是一个很好的交互使用协议(即在网页提交处理期间),因为它有一些非常长的超时,您可能会在与外部邮件服务器通信时遇到这些超时。

    避免这种情况的最佳方法是将本地邮件服务器作为中继运行,并让该服务器为您处理后续递送。

    您可以安装类似 postfix 和 configure it to relay through your gmail account 的东西。

    这将比提交到外部服务器快得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      相关资源
      最近更新 更多