codiy Posted December 19, 2016 Share Posted December 19, 2016 I have a user registration as well as a password recovery setup in php7 that I have got to work as I wanted. my problem is on registering or recovering a password I have it coded to send a message to the user ... it sends the email to user and registers no problem but the execution of the mail is extremely slow. I am trying to figure out a faster way of sending this.. So when the user fills out the register form and then click register it just sits and spends for a few sec then finally says registration complete. if i remove the code below which sends the email the site registers blazing fast. the below code sends the email with no problem other than the execution of it is slow not in receiving the email slow as in slows the execution on the site $from = "Mysite <noreply@mysite.com>";$to = $email; $subject = "Registration"; $body_message = "Hello!\n\n\n\nThank you for registering. \n\nYou must verify the email address associated with your account. \n\n\nThis message was sent from an unmonitored account. Any responses will not be read.\nIf you have any questions or concerns, please contact support@mysite.net"; $headers = "From: $from\nReply-To: $from\nContent-Type: text/plain"; mb_send_mail($email, $subject, $body_message, $headers); Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/ Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Do you know it's the mb_send_mail() and underlying mail() that's slow? Could it be the machine's MTA that's being slow - perhaps trying to send the email immediately instead of queueing it up locally to send later? Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540494 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 yes it seems as though it wants to send it instantly so it waits for the email to send before it displays the successful message is there anyway to prevent this? or trouble shoot? Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540501 Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Not sure you understood what I said... What is installed on your system that is sending the email? It's probably not PHP, rather PHP is telling the thing to send the email. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540510 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 not sure I my host is media temple Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540512 Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Oh, not your own hosting? Perfect. Complain to them that sending emails is slow and they can look into it. But first, be sure that it's not your code. Try a simple mail script <?php function udate() { list($us, $s) = explode(" ", microtime()); return date("Y-m-d H:i:s" . ltrim($us, "0"), $s); } header("Content-type: text/plain"); echo "Time before sending mail is: ", udate(), "\n"; mail("youremail@example.com", "Test", "The time is " . udate(), "From: noreply@mysite.com"); echo "Time after sending mail is: ", udate(), "\n"; and see how long it takes. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540515 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 here is what it gave me after click register Time before sending mail is: 1482172764.5671Time after sending mail is: 1482172770.7037The time is Mon, 19 Dec 2016 10:39:24 -0800 Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540518 Share on other sites More sharing options...
DeX Posted December 19, 2016 Share Posted December 19, 2016 Based on those numbers I would say the actual sending of the mail is done in a timely manner. I would investigate the mb_send_mail function. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540520 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 if i remove the mail part from my code the site register and displays message at the snap... Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540522 Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Based on those numbers (which you got from code that I revised... but not fast enough) it took six seconds to send the email. That's slow. Talk to your hosting provider. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540523 Share on other sites More sharing options...
DeX Posted December 19, 2016 Share Posted December 19, 2016 Based on those numbers (which you got from code that I revised... but not fast enough) it took six seconds to send the email. That's slow. Talk to your hosting provider. Oops, I thought it was in milliseconds. You're right. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540524 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 I just notice that when i click register the title in the browser says connecting then it says register complete Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540525 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 ok the support guy asked me what time format that was? Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540527 Share on other sites More sharing options...
codiy Posted December 19, 2016 Author Share Posted December 19, 2016 this was a response I got Well, six seconds is around the high end of the usual threshold for PHP mail or mail sent via SMTP that is being sent out as a result of a contact form or script on the (gs) Grid-Service. As I previously mentioned, the Grid is a clustered type of server where resources from each server in the cluster are shared among customers. When PHP mail it sent out it is sent from any number of web nodes and not necessarily only from your (gs) Grid-Service itself, depending on load.If you need instantaneous sending capability it is recommended that you either further optimize your script, use a dedicated mailing service provider (such as MailChimp), or consider hosting on a VPS server where resources are not shared. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540530 Share on other sites More sharing options...
DeX Posted December 19, 2016 Share Posted December 19, 2016 Makes sense. If you're getting cheaper hosting by using shared hosting then your trade-off is that you're sharing it with others and your times are going to be affected. If you want faster mailing then you can pay more for dedicated hosting or you can offload your mailing to a third party script like MailChimp. The choice is really yours, if your client wants faster mail but doesn't want to pay up for it then they can't have it both ways. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540532 Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Can you ask whether the mail is being sent immediately or being queued through a sendmail service? If immediately, is there a way you can make your emails be queued instead? Queuing will be much faster: sending requires connecting to the remote mail server, sending the contents of the mail, waiting for the server to validate everything and check you against spam services and whatnot, while queueing is just the amount of time it takes to save the email to the server (so it can go through the sending process later without impacting your script). Otherwise, short of moving to dedicated hosting where you could manage all this yourself (for better or worse), something like MailChimp will probably be your safest bet. Unless you wanted to implement the mail queue yourself, but I don't think I would recommend that in this case - if it's even possible given what you have access to. Quote Link to comment https://forums.phpfreaks.com/topic/302775-php-mail-slow-execution/#findComment-1540533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.