Jump to content

php mail slow execution


codiy

Recommended Posts

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);

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

:confused: 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.