9

Is it possible to send asynchronous emails with phpmailer?

Regular mail sending code snippet is as follows:

$mail->Send();

PHP waits for the Send() to return the result before moving on. Is it possible to have phpmailer to return a result instantly without waiting for the real email sending routine to complete.

Haluk
  • 2,091
  • 2
  • 27
  • 35

2 Answers2

8

Update May 2016

As mentioned by user @Sinak Salek PHP does support multithreading. It is available using the pthreads extension.

Original

PHP does not support multithreading natively (which you need to do this beautifully). You can do it though by saving the emails in a database and then process them later using another script (e.g. using a cron job). In this way you don't have to wait for the underlying email framework.

Another thing, if phpmailer is slow it can be due to the underlying mail program (sendmail, postfix etc.) is setup incorrectly.

Community
  • 1
  • 1
CodeTower
  • 6,293
  • 5
  • 30
  • 54
  • 1
    Thank you. In our case we implemented the following async curl request to a php script whose only duty is to send emails. http://stackoverflow.com/questions/962915/how-do-i-make-an-asynchronous-get-request-in-php – Haluk Nov 14 '12 at 15:29
  • Your welcome. Thanks for link to your solution, thats a neat way to solve it :) – CodeTower Nov 14 '12 at 17:07
  • 1
    PHP does support multi-threading and also multi-processing via well known extensions. I suppose that's what you meant by natively, if that's so then connecting to database also needs proper extension and many other things. Note that pthreads extension is actually well known and very straight forward to use – Sina Salek May 21 '16 at 18:45
  • @Sina Thank you for the input. At the time of writting the answer i was not aware of these extensions (if they were even well known then, this answer is more than 3 years old). I will update the answer to reflect this. – CodeTower May 23 '16 at 08:03
0

If you're on linux, you can put your php script in an exec command and launch it in background (put an & at the end of the command) and silent mode (2>&1 >/dev/null)

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
5p1r10n
  • 11
  • 2