PHP Mailer需要30秒才能发送欢迎电子邮件

时间:2016-04-30 03:35:49

标签: php email phpmailer

所以我在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-&gt; assignMailer(new PHPMailer());

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

为什么需要这么长时间?应该花这么久......

2 个答案:

答案 0 :(得分:1)

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

你可以经常逃脱直接交付并不意味着它是一种可靠的方法。

要查看SMTP会话的哪些部分需要很长时间,请设置$mailer->SMTPDebug = 2;并观看输出(但不要在您的实际网站上执行此操作!)。

答案 1 :(得分:0)

不知道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);

    }