PHP Mailer发送欢迎邮件需要30多秒


PHP Mailer taking over 30 seconds to send a welcome email

因此,当我的用户注册时,我使用PHP中的PHPMailer库发送欢迎电子邮件,这需要很长时间。

点击注册提交后,实际加载主页大约需要30-50秒。它基本上将页面置于重新加载状态超过30秒。

我使用的代码如下。。。

if ($config['user']['welcome_email_enabled'])
    $autoLoader->getLibrary('mail')->sendWelcomeEmail($email, $username);

我的邮件库在这里。

<?php
/**
 * MangoCMS, content management system.
 *
 * @info         Handles the mail functions.
 * @author       Liam Digital <liamatzubbo@outlook.com>
 * @version      1.0 BETA
 * @package      MangoCMS_Master
 *
 */
defined("CAN_VIEW") or die('You do not have permission to view this file.');
class mangoMail {
    private $phpMailer;
    public function assignMailer($phpMailer) {
        $this->phpMailer = $phpMailer;
    }
    public function setupMail($username, $password, $eHost) {
        if ($this->phpMailer == null)
            return;
        $this->phpMailer->isSMTP();
        $this->phpMailer->Host = $eHost;
        $this->phpMailer->SMTPAuth = true;
        $this->phpMailer->Username = $username;
        $this->phpMailer->Password = $password;
        $this->phpMailer->SMTPSecure = 'tls';
        $this->phpMailer->Port = 587;
    }
    public function sendMail() {
        if ($this->phpMailer == null)
            return;
        if (!$this->phpMailer->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $this->phpMailer->ErrorInfo;
            exit();
        }
        else {
            echo 'Email has been sent.';
        }
    }
    public function setFrom($from, $fromTitle) {
        $this->phpMailer->setFrom($from, $fromTitle);
    }
    public function addAddress($address) {
        $this->phpMailer->addAddress($address);
    }
    public function setSubject($subject) {
        $this->phpMailer->Subject = $subject;
    }
    public function setBody($body) {
        $this->phpMailer->Body = $body;
    }
    public function setAltBody($altBody) {
        $this->phpMailer->AltBody = $altBody;
    }
    public function setHTML($html) {
        $this->phpMailer->isHTML($html);
    }
    public function addReply($email, $name = '') {
        $this->phpMailer->addReplyTo($email, $name);
    }
    public function sendWelcomeEmail($email, $username) {
        global $config;
        $mailer = $this->phpMailer;
        $mailer->setFrom($config['website']['email'], $config['website']['owner']);
        $mailer->addAddress($email, $username);
        $mailer->addReplyTo($config['website']['email'], 'Reply Here');
        $mailer->isHTML(true);
        $mailer->Subject = 'Welcome to ' . $config['website']['name'] . ' (' . $config['website']['link'] . ')';
        $mailer->Body = '<div style="background-color:#1a8cff;padding:24px;color:#fff;border-radius:3px;">
        <h2>Welcome to Zubbo ' . $username . '!</h2>Thank you for joining the Zubbo community, we offer spectacular events, opportunities, and entertainment.<br><br>When you join Zubbo you will recieve <b>250,000 credits</b>, <b>100,000 duckets</b>, and <b>5 diamonds</b>. One way to earn more is by being online and active, the more you are active the more you will earn, other ways are competitions, events, and games :)<br><br>We strive to keep the community safe and secure, so if you have any questions or concerns or have found a bug please reply to this email or contact us using in-game support.<br><br>Thank you for joining Zubbo Hotel!<br>- Zubbo Staff Team
        </div>';
        $mailer->AltBody = 'Here is a alt body...';
        if (!$mailer->send()) {
            exit('FAILED TO SEND WELCOME EMAIL!! ' . $mailer->ErrorInfo);
        }
    }
}
?>

所以我首先调用这些,然后在我想实际发送电子邮件时调用sendWelcomeEmail()。

$mailer->assignMailer(new PHPMailer());

$mailer->setupMail(
    "********@gmail.com",
    "**************",
    "smtp.gmail.com");

为什么要花这么长时间?应该花这么长时间吗。。

在页面提交过程中使用远程SMTP并不是一件好事——正如您所看到的,它通常非常慢(有时是故意的,用于问候延迟检查)。解决方法是始终提交到本地(快速)邮件服务器,让它处理等待,还可以处理PHPMailer无法处理的延迟送达等问题。在走这条路线时,你还需要正确处理反弹,因为你不会立即得到反馈。

你经常可以直接送货,但这并不意味着这是一种可靠的方法。

要查看SMTP会话的哪个部分需要很长时间,请设置$mailer->SMTPDebug = 2;并观察输出(但不要在您的实时站点上这样做!)。

不知道PHPMailer是否对您是强制性的,但如果不是,我建议使用SwiftMailer。

根据我的个人经验,它非常快速可靠。

谢谢。

include_once "inc/swift_required.php";
$subject = 'Hello from Jeet Patel, PHP!'; //this will Subject
$from = array('jeet@mydomain.com' =>'mydomain.com'); //you can use variable
$text = "This is TEXT PART";
$html = "<em>This IS <strong>HTML</strong></em>";
$transport = Swift_SmtpTransport::newInstance('abc.xyz.com', 465, 'ssl');
$transport->setUsername('MYUSERNAME@MYDOMAIN.COM');
$transport->setPassword('*********');
$swift = Swift_Mailer::newInstance($transport);

    $to = array($row['email']  => $row['cname']);
    $message = new Swift_Message($subject);
    $message->setFrom($from);
    $message->setBody($html, 'text/html');
    $message->setTo($to);
    $message->addPart($text, 'text/plain');
    if ($swift->send($message, $failures))
    {
        echo "Send successfulllyy";
    } else {
        print_r($failures);
    }