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!
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
There are many background process software like Beanstalkd, GearMan etc...
I suggest beanstalkd because it's very light weight and simple. Easy to create job and pass to queue (Tube in their terms).
One additional worker required who continues keeping eye on Tube and process if any job came.
Giving you some links that might help you,
Actually there are many more, but i don't remember name right now.
Regards
If you are interested in sending email via async PHP, you can have a look at this answer. https://stackoverflow.com/a/22627769/829533
it is using Thread of php
PECL pthreads
libraryPlease note that setting up
PECL
for your Apache environment is the most tricky partPlus in the very same post people suggested about setting corn jobs for such requirements but it totally depends on your requirement. you can
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:
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:
-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:
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.
You could use an AJAX request to post data to PHP script, which will then send the email.
You can use
exec
to tell the PHP CLI interpreter to run your script in the background.if you are on linux:
if you are on windows:
This requires that your server have PHP's CLI interpreter installed and requires that you 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 using
get_cwd()
.