I am a JS developer so PHP is really not my expertise. I have a page that sends an email when submitted, sometimes the page will timeout when gmail servers are slow to respond.
I am wondering if there is a way to either extend the timeout of a page (I am using LAMP setup, Apache, PHP) or maybe run Mail::factory in aysnc non blocking?
or any other ideas to make sure the page does not timeout to users (I don't mind if they need to wait 10 more seconds as they see loading bar...) when gmail is slow to reply back?
this is the function
function sendMail($from, $to, $subject, $body, $type = "HTML", $attach = "", $attachType = "'image/jpg'") {
$crlf = "
";
$mime = new Mail_mime(array('eol' => $crlf));
#if ($type == "text") {
# $mime->setTXTBody($body);
#} else {
$mime->setHTMLBody($body);
#}
$headers = array(
'From' => "<from.gmail.com>",
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'que@aaa.com',
'password' => 'xxxx'
));
//$to = 'Recipient Name <my_user@gmail.com>';
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
//echo("<p>" . $mail->getMessage() . "</p>");
} else {
//echo("<p>Message successfully sent!</p>");
}
}
Thanks,
Sean.
You can remove execution timeout completely limit by adding
set_time_limit(0);
At the very top of your script.
Here is the link to PHP documentation on this function.
Why send your emails asynchronously? Use a queue service to send emails in the background instead.
Install a local mail server, send using SMTP to localhost. It will complete very quickly and the mail server will deal with waiting, queuing, retrying etc without you having to implement anything complicated. SMTP is not good during page loads because it can be very slow. You can configure the mail server to relay onwards via your gmail account. I recommend postfix.