PhpMailer 发送时延迟 ~30 秒



好的,所以我对PhpMailer几乎没有问题。

  1. 在执行$mail>发送之前,有大约 30 秒的延迟。

2019-07-2313:43:55 连接:打开到 smtp.gmail.com:587, 超时=300, 选项=数组()
2019-07-2313:44:16 连接:已打开
2019-07-23 13:44:16 服务器 等。。。(一些参数列表)。

    用于
  1. 注册用户的代码。我在模型方法中实例化了 Mail 类,然后在控制器中调用该方法,后跟成功登录后重定向的函数。我还收到标题已发送错误。

2019-07-23 13:44:17 连接:已关闭
消息有 已发送
警告:无法修改标头信息 - 标头 已发送者(输出开始于 C:\xampp\htdocs\log\vendor\phpmailer\phpmailer\src\SMTP.php:257) in C:\xampp\htdocs\log\App\Core\Controller.php 在第 43 行

我的代码:

邮件类:

class Mail
{
public function sendMail($to, $subject, $text, $html)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'wuwu5431@gmail.com';
$mail->Password = 'Password';                       
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('wuwu5431@gmail.com', 'John Smith');
$mail->addAddress($to, '');
$mail->addReplyTo('wuwu5431@gmail.com', 'Information($mail->addReplyTo)');
//Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body    = $text;
$mail->AltBody = $html . ' $html . This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}//end of method
}// end of class

模型(方法):

public function sendActivationEmail($email)
{
$url = 'url';
$text = 'text';
$html = 'html';
$mail = new AppMail;
$mail->sendMail($email, 'Account activation', $text, $html);
}

控制器(方法):

public function register()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
foreach($_POST as $key => $value){
$data[$key] = htmlspecialchars(strip_tags(trim($value)));
}
$this->userModel = new AppModelsUserM;
if ($this->userModel->Register($data)) {
Flash::addMessage('Thank you for Registering with us');
$this->userModel->sendActivationEmail($data['email']);
$this->redirect('User/UserC/success');
}
}
}

环境:赢7,xampp,本地主机。

这是完全正常的 - 欢迎来到SMTP的世界。

SMTP可以在事务中的多个点施加长达10分钟的延迟,因此在页面提交处理期间不太适合使用,尽管这样做仍然很常见。

处理它的方法是使其异步(就您的页面而言),并且有多种方法可以做到这一点,例如将您的消息存储在稍后由单独进程拾取和发送的队列中(即不阻止您的页面提交处理),或将其直接提交到本地邮件服务器, 通常会在几毫秒内接受提交,并为您处理后续交付。

除此之外,这些行可能是错误的:

$mail->setFrom('wuwu5431@gmail.com', 'John Smith');
$mail->addAddress($to, '');
$mail->addReplyTo('wuwu5431@gmail.com', 'Information($mail->addReplyTo)');

如果您的发件人地址和回复地址相同,则回复将被忽略。如果您没有与收件人地址一起使用的名称,请将其关闭。单引号字符串在 PHP 中不执行变量插值。您可能只想:

$mail->setFrom('wuwu5431@gmail.com', 'John Smith');
$mail->addAddress($to);

尝试使用 IP 作为邮件服务器。遇到同样的问题,从域交换到 IP 将时间减少到 ~1 秒

最新更新