Jump to content

[SOLVED] mail() command takes a long time to finish


rtadams89

Recommended Posts

I'm hosting my site on a shared Linux hosting package at 1&1.

 

I'm using the following code as part of a larger project to email all my users when a new event gets added to my site's calendar. The $recipients variable is a comma separated string of about 90 email addresses.

//Email members on addition of new event.	

$sql2 = "SELECT * FROM phpbb_users WHERE user_id = ".$uid."";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
$name = preg_replace('/(\w+)([A-Z])/U', '\\1.\\2', $row2['username'], 1);
$name = explode(".", $name, 2);
$name = $name[0] . " " . $name[1];
$recipients = calendar_mail();

$subject = 'New ASURFC Event';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ASURFC <no-reply@asurfc.com>' . "\r\n";
$message = '
<html>
<head>
  <title>New ASURFC Event</title>
</head>
<body>
  <p>A new event has been posted on the <a href="http://www.asurfc.com/schedule.php">ASURFC.com calendar</a>.</p>
  <br>Event Title: '.$title.'
  <br>Event Date: '.$month . "/" . $day . "/" . $year.'
  <br>Added by: '.$name.'
  <br>Event Details: '.$text.'
  <br><br>If you want to stop receiving notices when new events are added, you can change your notification preferences here: <a href="http://www.asurfc.com/user_cp.php">http://www.asurfc.com/user_cp.php</a>.
  
</body>
</html>
';

mail($recipients, $subject, $message, $headers);	

 

When this code is run, it takes about a minute to complete. It does work, it just takes sooooooooooooo long. I'm worried that as I add more recipients, I will begin to experience errors dues to PHP's max execution time.

 

Should the mail() function be this slow? Is there any way to make it faster?

Link to comment
Share on other sites

Firstly, do you really want to give out all your users addresses to each user? Bad practice IMO.

 

And yeah, sending mail can take some time. Its not php's mail function thats the problem, but the actual mail server itself.

 

Id usually write a shell script to do this, then execute it via at. Do you have shell access to the server?

Link to comment
Share on other sites

if you have access to the mail server, be sure that it isn't trying to filter spam and check for viruses on mail sent from you server (since you know/hope that you won't be sending either). That can dramatically slow it down for each address you are sending to.

Link to comment
Share on other sites

I'm not to worried about everyone seeing the other user's emails. We are a small organization, and all the members already have access to each other's emails.

 

Unfortunately I do not have shell access. This is one of 1&1's $4/month linux hosting packages.

 

My concern is not with how long it takes for the emails to be sent, but with the delay it introduces in loading the page.

 

Would it be faster to have 5 mail() commands, watch with only 1/5 the recipients? Could I somehow have the mail() command happen in the background so as to not hold up the rest of the script?

Link to comment
Share on other sites

Would it be faster to have 5 mail() commands, watch with only 1/5 the recipients?

 

Nope, they all still need to execute.

 

Could I somehow have the mail() command happen in the background so as to not hold up the rest of the script?

 

Not without shell access.

Link to comment
Share on other sites

I have 1&1 too, same package too. mail does run a little slow on because they are trying to prevent mass mailing/spamming from their servers.

The available information is that they will limit you to 99 To: address in one connection to the mail server. So, you will eventually need to break it up into smaller chunks.

We are a small organization
Link to comment
Share on other sites

I have 1&1 too, same package too. mail does run a little slow on because they are trying to prevent mass mailing/spamming from their servers.

The available information is that they will limit you to 99 To: address in one connection to the mail server. So, you will eventually need to break it up into smaller chunks.

We are a small organization

 

Plus they probably have like a million clients all squished onto a tiny server :)

Link to comment
Share on other sites

I have 1&1 too, same package too. mail does run a little slow on because they are trying to prevent mass mailing/spamming from their servers.

The available information is that they will limit you to 99 To: address in one connection to the mail server. So, you will eventually need to break it up into smaller chunks.

We are a small organization

 

Plus they probably have like a million clients all squished onto a tiny server :)

$4 a month... MILLIONS.

Link to comment
Share on other sites

not that I know of. PEAR is a library that must be on the server. I do not believe 1&1 has that library since their Linux server is mostly neglected by them.

PEAR's strengths include allowing you to pass mail server credentials in the PEAR mail function.

Again, move servers or deal with the lag of 1&1. I also use godaddy, they are faster (but I'm in the same state as their server). Little bit more a month though.

Link to comment
Share on other sites

not that I know of. PEAR is a library that must be on the server. I do not believe 1&1 has that library since their Linux server is mostly neglected by them.

 

Anyone can use a PEAR module on any server. You just need to get hold of the right classes.

 

Anyways, yes, I have heard people suggest that you may have more luck through phpmailer, I'm not sure about the PEAR one.

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.