Reputation: 251
I am using PHPMailer to send email which works great. The issue however is that since it sends the email synchronously, the subsequent page load takes a long time.
I am using PhpMailer as shown in this example https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
I wonder if there is a way to make email delivery asynchronous. I researched this and found that sendmail has an option to set DeliveryMode to "background mode" - source http://php.net/manual/en/function.mail.php
mail($to, $subject, $message, $headers, 'O DeliveryMode=b');
I want to know if something similar can be done in PhpMailer? Has anyone had any success with this?
EDIT:- (Additional info) It seems like PhpMailer can be configured to use sendmail - https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php Therefore I wonder if this can be somehow leveraged to enable background delivery.
/**
* 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';
}
Also - apparently there is way to set sendmail options via php.ini http://blog.oneiroi.co.uk/linux/php/php-mail-making-it-not-suck-using-sendmail/
I would prefer to do this as an inline argument to the api call vs php.ini so that isnt a global change. Has anyone tried this?
Upvotes: 6
Views: 11519
Reputation: 37710
Wrong approach.
PHPMailer is not a mail server, which is what you're asking it to be. SMTP is a verbose, chatty protocol that's prone to delays and slow throughput, and is absolutely unsuited to sending interactively during a typical web page submission (which is what the question BlackHatSamurai linked to is probably doing). Many get away with doing precisely that, but don't be fooled into thinking it's a good solution, and definitely don't try to implement an MTA yourself.
The gmail example you linked to is using SMTP to a remote server, which will always be slower than submitting locally. If you're submitting via sendmail (or mail()
- it's basically the same thing) to a local server and it's taking more than about 0.1 sec, you're doing something very wrong. Even SMTP to localhost won't take much longer, and sending to a nearby smarthost is unlikely to be too slow either.
Trying to background things using threads is a massive can of worms that is exactly not the way to go about this - whatever you achieve that way would be terrible compared to a proper mail server. Just don't do it.
The right way to do this is to install a local mail server, and submit your messages to it with PHPMailer. This way is very fast (hundreds of messages per second), and you have to do precisely nothing to make it work because that's how PHPMailer works by default.
The mail server will then do what it's supposed to do - queue your message, deal with connection problems, delivery deferrals, bounces and everything else you've not considered.
Upvotes: 22
Reputation: 23483
According to this phpMailer
doesn't support this type of call. You would have to write your own threaded class in order to do make an async call. See pThreads and the Thread class. One other solution was found here.
Upvotes: 0