首页 > 解决方案 > PHPMailer 挂起邮件发送

问题描述

我尝试向用户发送电子邮件,但一旦$mail->send()被执行,什么也没有发生,网页卡住并挂了 5 分钟。

这是$mail设置:

<?php

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

require_once __DIR__ . '/../PHPMailer/src/Exception.php';
require_once __DIR__ . '/../PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/../PHPMailer/src/SMTP.php';

class Email {
  private $mail;

  function __construct() {
    $config = parse_ini_file(__DIR__ . '/../../private/config.ini');
    $this->mail = new PHPMailer(true);
    $this->mail->IsSMTP();
    $this->mail->SMTPDebug = 4;   
    $this->mail->SMTPAuth = true;
    $this->mail->CharSet = 'utf-8';
    $this->mail->Secure = 'ssl';
    $this->mail->Host = $config['smtphost'];
    $this->mail->Port = 465;
    $this->mail->Username = $config['smtpusername'];
    $this->mail->Password = $config['smtppassword'];
  }
}

5 分钟后,此消息显示在浏览器中:

2020-04-01 22:53:58 Connection: opening to ##mysmtphost##:465, timeout=300, options=array()
2020-04-01 22:53:58 Connection: opened

我已经检查了任何类似的问题及其解决方案,但这些似乎都对我的问题没有帮助。

发送:

$mail = new Email();
$mail->AddAddress($_POST['forgot_password']['email'], $foundEmail['username']);
$fromEmailAddress = ###MyHostEmailAddress###;
$fromName = ###name...###;
$mail->SetFrom($fromEmailAddress, $fromName);
$subject = ###subject..###;
$msgBody = $url;
$mail->Subject($subject);
$mail->Body($msgBody);
if($mail->Send())
  $validationMessage = 'TEST'

标签: phpsmtpphpmailer

解决方案


尝试在发送函数之后返回一些东西,因为我认为(所以我不确定 100%,但我有这样的问题)调用发送函数时的 phpmailer 使用线程,所以它使用线程发送邮件,所以如果你的代码结束或死亡如果没有返回,线程也将停止或死亡。你也可以转储 send() 的结果,如果它是真的,这意味着你的配置是好的,否则意味着你有问题


推荐阅读