1

First, I've seen this question: Send mail without blocking 'execution'

But, I need to know why PHP is not a multi-threading process but has no problem if two people try to connect at the same time.

I am calling the mail script by an Ajax call actually :

 $.ajax({
     url: 'sendMailTemplate.php', 
     type: 'POST',
     data: formData,
     processData: false,
     contentType: false,
     success: function (data) {}
...

My problem with PHP mailer is that, when I'm sending an email if this takes 10 seconds, then for 10 seconds i can't use any feature of my website ( and the website is like down for everyone for 10 seconds). Should I try to use cron jobs? Is there any tutorial for PHP mailer + cron jobs? What's the best way other than cron jobs?

Towkir
  • 3,889
  • 2
  • 22
  • 41
axel axel
  • 253
  • 1
  • 11
  • It's likely that this is not due to the sending (though that's what is taking the time), but due to session file locking, where nobody can do anything because the session is locked. Try calling [`session_write_close()`](https://php.net/session_write_close) before you send the email. – Synchro Jun 11 '19 at 10:25
  • As you say though, it's also possible that you only have a single process configured for PHP that everyone shares, but you don't normally notice because other operations don't take long enough for it to matter. If you're using PHP-FPM (which you should be), make sure that the `pm.max_children` setting (and other related settings - search for them) in your FPM pool config isn't too low. – Synchro Jun 11 '19 at 10:36
  • @Synchro With a session locking problem everyone else can use the site just fine, it's _just you_ that is blocked while the email sends. – Sammitch Jun 11 '19 at 15:37
  • That’s true. Seems likely to be some similar kind of lock though. – Synchro Jun 11 '19 at 16:12
  • @Synchro pm.max_children and other settings doesn't change anything, i'll try cron jobs i guess.. – axel axel Jun 12 '19 at 08:49
  • You could configure a local mail server to act as a relay - that's the fastest way to get email submitted anyway, and it's a lot simpler than configuring cron. On Debian-like Linuxes, simply: `apt install postfix` and then change your script to `$mail->Host = 'localhost'` and disable encryption (you can't encrypt to localhost). – Synchro Jun 12 '19 at 08:52

2 Answers2

1

Should I try to use cron jobs ?
Yes

Is there any tutorial for PHP mailer + cron jobs ?
I tried my own logic by using multiple reference :

Below logic execute like queue and also sending time base on your cronjob & you hrly mailing limit.

  1. Mailing : when you click on send email that time you have store all emails into "temp_table" of your database with their "to,cc,bcc,suject,body" columns.
  2. Write cron job for hrly(your time) basic which will execute in certain time span.
  3. In cron script you have to write code which will take emails (set query limit to take no of records) from "temp_tables" & send to the recipient. After success full sending, delete those mails from "temp_table" of you database.

Whats the best way other than cron jobs ?
You have contact to your service provider to increase hrly mailing limit, increase your server speed, php.ini : change loading time (this option not works every time), refer different language like python

Ashu
  • 1,320
  • 2
  • 10
  • 24
0

Just switch to a smtpd that allow placing mails in queue and sending them asynchronously.

Victor Langat
  • 51
  • 1
  • 7