born2net born2net - 9 months ago 40
PHP Question

Sending mail in PHP via Mail::factory sometimes will timeout my page

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 = "\n";
$mime = new Mail_mime(array('eol' => $crlf));

#if ($type == "text") {
# $mime->setTXTBody($body);
#} else {

$headers = array(
'From' => "<>",
'To' => $to,
'Subject' => $subject

$smtp = Mail::factory('smtp', array(
'host' => 'ssl://',
'port' => '465',
'auth' => true,
'username' => '',
'password' => 'xxxx'

//$to = 'Recipient Name <>';

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




You can remove execution timeout completely limit by adding


At the very top of your script.

Here is the link to PHP documentation on this function.