通过 PhpMailer 异步发送电子邮件

2024-02-05

我正在使用 PHPMailer 发送电子邮件,效果很好。但问题是,由于它同步发送电子邮件,因此后续页面加载需要很长时间。

我正在使用 PhpMailer,如本示例所示https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

我想知道是否有办法使电子邮件发送异步。我对此进行了研究,发现 sendmail 有一个选项可以将 DeliveryMode 设置为“后台模式” - 来源http://php.net/manual/en/function.mail.php http://php.net/manual/en/function.mail.php

mail($to, $subject, $message, $headers, 'O DeliveryMode=b');

我想知道 PhpMailer 是否可以做类似的事情?有人在这方面取得过成功吗?

编辑:-(附加信息)看来 PhpMailer 可以配置为使用 sendmail -https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php因此我想知道是否可以以某种方式利用它来实现后台交付。

/**
 * Which method to use to send mail.
 * Options: "mail", "sendmail", or "smtp".
 * @type string
 */
public $Mailer = 'mail';

/**
 * The path to the sendmail program.
 * @type string
 */
public $Sendmail = '/usr/sbin/sendmail';
/**
 * Whether mail() uses a fully sendmail-compatible MTA.
 * One which supports sendmail's "-oi -f" options.
 * @type boolean
 */
public $UseSendmailOptions = true;

/**
 * Send messages using $Sendmail.
 * @return void
 */
public function isSendmail()
{
    $ini_sendmail_path = ini_get('sendmail_path');
    if (!stristr($ini_sendmail_path, 'sendmail')) {
        $this->Sendmail = '/usr/sbin/sendmail';
    } else {
        $this->Sendmail = $ini_sendmail_path;
    }
    $this->Mailer = 'sendmail';
}

另外 - 显然有办法通过 php.ini 设置 sendmail 选项http://blog.oneiroi.co.uk/linux/php/php-mail-making-it-not-suck-using-sendmail/ http://blog.oneiroi.co.uk/linux/php/php-mail-making-it-not-suck-using-sendmail/

我更愿意将其作为 api 调用与 php.ini 的内联参数来执行,这样这不是全局更改。有人试过这个吗?


错误的做法。

PHPMailer 不是邮件服务器,这正是您所要求的。 SMTP 是一种冗长、繁琐的协议,容易出现延迟和吞吐量缓慢,并且绝对不适合在典型的网页提交期间以交互方式发送(这就是 BlackHatSamurai 链接到的问题可能正在做的事情)。许多人确实这么做了,但不要误以为这是一个好的解决方案,也绝对不要尝试自己实施 MTA。

您链接到的 gmail 示例使用 SMTP 发送到远程服务器,这总是比本地提交慢。如果您通过 sendmail 提交(或mail()- 这基本上是相同的事情)到本地服务器并且花费了大约 0.1 秒以上,你正在做一些非常错误的事情。即使 SMTP 到本地主机也不会花费太长时间,并且发送到附近的智能主机也不太可能太慢。

尝试使用线程进行后台处理是一大堆蠕虫,这完全不是解决此问题的方法 - 无论您以这种方式实现什么,与适当的邮件服务器相比都会很糟糕。只是不要这样做。

正确的方法是安装本地邮件服务器,并使用 PHPMailer 向其提交邮件。这种方式非常快(每秒数百条消息),而且你必须精确地做nothing让它工作,因为这就是 PHPMailer 默认的工作方式。

然后,邮件服务器将执行其应该执行的操作 - 对您的邮件进行排队、处理连接问题、延迟投递、退回邮件以及您未考虑到的所有其他操作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 PhpMailer 异步发送电子邮件 的相关文章

随机推荐