How to send email using PHPMailer in the background?

PHPMailers does a great job of sending emails from a gmail account. But this takes quite some time, and the page will not display the response until the message has been sent. Any ways to send email in the background so that I can provide the user with a more convenient user experience? Thanks!

+6
source share
5 answers

Using email queue and php exec () is one of the best ways.

It will start when necessary (avoiding the use of CRONs), quickly, because it is caused by the background and immediate.

1. Email queue. Take all the fields in the MySQL table with the insert, something like:

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

This is important because you will need an independent background process, so it is also a good idea to register and audit all outgoing emails.

2. PHP exec (). After pasting into MySQL, the time to call as an external execution is:

 exec("wget -qO- http://domain.com/index.php?process_email_queue=1 &> /dev/null &"); 
  • Please note: for output and invocation as a background process, parameters from wget -q0- and &> ... / dev / null & are required.

3. The same script file index.php or another to handle the call to the queue:

Thus, it will call our index.php (you can use a different name file) and process the outgoing ones:

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

Perhaps you need to touch on some php.ini options for exec (), this is not a big problem.

Once everything works correctly, you will offer the best site navigation and email for quick response and zero waiting.

In some cases, you will go from waiting from direct mail 2.60 seconds to queue-exec-background 0.024 secs, i.e., x11 improvements are faster.

+9
source

You can use an AJAX request to send data to a PHP script, which will then send an email.

0
source

If you are interested in sending emails via async PHP, you can look at this answer. fooobar.com/questions/968035 / ...

 <?php class AsyncEmail extends Thread { public function __construct($arg){ $this->arg = $arg; } public function run() { /** Add your email sending code here. **/ } } // and call this following lines in loop $aEmail = new AsyncEmail( $arg ); var_dump($aEmail->start()); ?> 

uses Thread library php PECL pthreads

Note that configuring PECL for your Apache environment is the hardest part

Plus, in the same message, people suggested setting corn assignments for such requirements, but it completely depends on your requirement. You can

  • Create a new spreadsheet for sending emails.
  • write a php script to send letters using data from the new creation table
  • configure the corn job to run this script at some interval
0
source

You can use exec to tell the PHP CLI interpreter to run the script in the background.

if you are using linux:

 exec("/path/to/php /path/to/your/mailer/script \"arg1\" \"arg2\" \"arg3\" \"arg4\" > /dev/null 2> /dev/null &"); 

if you are in windows:

  pclose(popen("start /B /path/to/php /path/to/php/script \"arg1\" \"arg2\" \"arg3\" \"arg4\"","r")); 

This requires a PHP CLI interpreter to be installed on your server, and you need to know the path to the PHP binary (ask your host if you don't know). You also need to know the absolute path to your PHP script, which you can find with get_cwd() .

0
source

There are many background programs like Beanstalkd, GearMan, etc.

I suggest beanstalkd because it is very light and simple. Easily create tasks and go into the queue (Tube in their terms).

One additional worker demanded that he continue to monitor the pipe and process if any work came.

Providing you with some links that may help you,

Actually there are many more, but I don’t remember the name right now.

Hello

0
source

All Articles