Reputation: 1
I've been writing a number of PHP pages that involve using mail(). For the most part, it works well. However, occasionally (I'd say about 10-20% of the time), the mail() function causes the page to load exceptionally slowly, if at all.
I haven't been able to find a similar problem on forums anywere. Just to reiterate, the mail() function works fine and sends mail, but when calling scripts with the mail() function in it, it occasionally causes slow page load times.
Here's the important bits of what the pages look like. And for the record, we are using Microsoft Exchange Server 2007.
<html>
<head>
<?php
if ($_POST['submit'] == 'submit'){
//execute some php code.
mail($to, $subj, $body, $headers, "O DeliveryMode=b");
}
?>
<meta http-equiv="refresh" content="0">
<?php
}
</head>
<body>
<form action=<?php echo $_SERVER['PHP-SELF']?>>
<!--Form Data-->
<input type='submit' name='submit' value='submit'/>
</form>
</body>
</html>
Upvotes: 0
Views: 1818
Reputation: 4467
As johndavidjohn points out, the slowness is caused by the communication with the mail server. The page won't finish loading until the email has been sent and the connection is closed.
I would suggest saving the message in a database and then using a cronjob to pull the message from the database and send the email. This will offload the work of sending the email to a background process.
If you can't run a cronjob, you might be able to getting better response times using a mail delivery service like Postmark, Sendgrid, or Amazon SES.
Upvotes: 1
Reputation: 2247
There are several options of doing this: using ajax, or putting emais in a queue and run a cron to run your script to send them asynchronously.
Upvotes: 1