Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send emails using PHPMailer in the background?

PHPMailers is doing a fine job in sending emails from a gmail account. But it takes quite a bit of time, and the page won't show the response until the email has been sent. Any ways to send the email in the background so that I can provide a better user experience to the user? Thanks!

like image 254
user2335065 Avatar asked Apr 21 '14 05:04

user2335065


People also ask

How can I send multiple emails in PHPMailer?

SendMail() code tar. gz"); // add attachments $mail->AddAttachment("/tmp/image. jpg", "new. jpg"); // optional name $mail->IsHTML(true); // set email format to HTML $mail->Subject = "Here is the subject"; $mail->Body = "This is the HTML message body <b>in bold!

Can we send email through PHP script?

Using the PHP mail() function. PHP's built-in mail() function is one of the simplest ways to send emails directly from the web server itself. It just takes three mandatory parameters: the email address, email subject and message body—and sends it to the recipient.

Why it is advantages to use PHPMailer for sending and receiving email?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.

Can I send email from localhost PHP?

The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.


1 Answers

The use of an email queue and php exec() is one of the best ways.

It will trigger when needed (avoiding the use of CRONs), it's fast because it is called backgrounded, and immediate.

1. Email queue. Take all fields in a table's MySQL with an insert, something like:

$queryIN="INSERT INTO email_queue (date,subject,body,destination,idle) values (...)";
mysql_query($queryIN);

That's important because you will need an independent background process, so also it's a good idea for registering and auditing all outgoing emails.

2. PHP exec(). After inserting in MySQL is time to call as external execution:

exec("wget -qO- http://example.com/index.php?process_email_queue=1 &> /dev/null &");
  • Take note, options from wget -q0- and &> ... /dev/null & are needed to suppress output and call as a background process.

3. Same script file index.php or other for processing call of a queue:

This way, it will call our index.php (you can use other name file), and process outgoing:

if ($_GET['process_email_queue']==1) { ...code for sending idle emails queue...  }

Perhaps you have to touch some php.ini options for exec(), is not a big deal.

Once everything is running correctly, you will offer a better web navigation and email handling for fast response and zero waits.

In some cases you will pass from waiting from a direct email 2.60 secs to queue-exec-background 0.024 secs, which is a speed improvement of 11 times faster.

like image 58
Rafa Avatar answered Oct 08 '22 17:10

Rafa