I have a mail script… It works ok if I send mail to just a few addresses in the database but fails when I try to send to 900 odd emails. The script is apparently utilizing more resources than allocated for the shared hosting (CPU 42% average)

Script is something like:

	$headers  = "MIME-Version: 1.0\\r\
	$headers .= "Content-type: text/html; charset=iso-8859-1\\r\
	$headers .= "From: ### <###>\\r\
	$headers .= "Reply-To: ".$firstname." <".$email.">\\r\
	$to = "".$firstname." <".$email.">";
	$subject = "Our Monthly Newsletter";

	$message = 'Several mysql quries here grabing data from database and html to display';

	$x = 1;
    $delay = 50; 
    $send = @mail($to, $subject, $message, $headers);
    if($x == $delay) {
    $x = 0;

I have made it sleep to take some load off… How can I optimise this?

I can only sent 200 emails per hour as well.

Any help appricated.


Two questions:
1> how long does the script run?
2> What’s the max execution time for script?

Sending newsletters from shared hosting just isn’t a good idea.

For one, it puts your entire account at risk. High CPU utilization is generally a condition for termination in the TOS, so they can decide to give up on you and cancel the account. So is being reported as a spammer, which will happen whether you are spamming or not when you have enough subscribers.

Secondly, you’ll have deliverability problems. Your server is going to end up on spam blacklists, and since you don’t run the server, you may not be able to get it off. It doesn’t even have to be one of your users hitting the “spam” button on your newsletter, it can be anyone else you share the server with. There are just tens of thousands of IPs of shared hosting servers on spam blacklists for that reason, and that means a lot of your recipients won’t ever see your mail.

I suggest looking into an e-mail marketing service to host the list and send the mails.

I cant tell how long the script runs as I get a error on the page as it times out I assume.

If I use another e-mail marketing service can you grab info from the database? As the newletters are populated from data within the database… Store listings, classes, latest news etc…

Thats a bugger as the emails are for people who have subcribed online to be on the list.

I emailed the host… they said optimize the script and send emails in batches so it doesnt exceed the limit of 200 per hour

You would want to integrate with the API of the service you choose to add new subscribers to your list there, rather than pointing to your own database. You won’t be able to do that as the company needs to maintain the subscriber list, and do the opt-in verifications, in order to keep the servers off of blacklists and your mail deliverable.

Abit lost here,

You have to manually add the users to their list? can you send newsletter populated from our database?

I mean can the newsletter sent va a company like that be poulated with info from our database or does a newsletter have to be manualy setup on their templates?

When someone does something on your site that’s supposed to sign them up for your newsletter, you make an API call to your e-mail service sending the e-mail, name and any other info about the subscriber to them. They add it to your list in their database and send out the e-mail to verify the subscriber wants to join your list.

The details depend on who you work with, you have to read their documentation.

Can you recomend anyone?

rather custom code, i would like to recommend: PHPMailer or Swiftmailer for mass mailing purpose.

SitePoint Blogs - E-mail Marketing: 13 Services To Help You Make Contact

I use MailChimp.