Sending a few hundred emails with mail() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Sending a few hundred emails with mail() function

Post by paladaxar »

Alright...tonight I decided to send out a mass email to everyone that uses one of my sites. But in doing so, I ran into some problems. I ran a query to select all of the email address from my db, then put the mail() function into a while loop to send a message to each user. Well, when I tried to load the page, it took a really long time (since the mail function is fairly slow) and then it timed out (at least I think that's what it did). It gave asked me if i wanted to open the page with a program, or "save it to my computer".

I have no idea if it sent one email, half of the emails or all of them. I dont want to try it again until I know for sure that I am doing it in a way that will work (I dont want all of my users to get 5 emails while I play around with settings).

Am I even right to put this script into a page? Or should it be a file that is run on the server...and if so, how would I do that?

Here's my script:

Code: Select all

<?php
	session_start();
	include( log into the database );
	$query = "select distinct email from users";
	$result = mysql_query($query);
	while ( $line = mysql_fetch_row($result) )
	{
		mail($line[0], $subject, $body, "From: myemail@mysite.com");
	}
	$result = mysql_query($query);
	echo "The emails have been sent to: <br>";
        $num_sent = mysql_num_rows($result);
	echo "$num_sent email addresses<br><br>";
	while ( $line = mysql_fetch_row($result) ) 
	{
		echo "$line[0]<br>";
	}
?>
Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Considering the mail function will, open a connection, send the email and then close the connection it is definantly not recommended for mass mailing. I would recommend using an email library, such as swiftmailer.org -- it also has it's own forum here, along with an anti-flood plugin. It is very easy to implement and can do wonders :)

Alternatively, if you want to stick to mail, you'll need to increase php's memory usage as well as maximum execution time.
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

hmmm...I downloaded the swift mailer. It looks really complicated. I have no idea what to do with all of those files. Also, I dont really need anything fancy, I just want to send a few hundred simple emails. Is there any other way to do this? In a simple way?
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

If you want to use mail() function you should consider to use a LIMIT clause on your query to avoid timeout problems.
http://dev.mysql.com/doc/refman/5.1/en/select.html
http://dev.mysql.com/doc/refman/4.1/en/ ... ation.html


by using LIMIT on your query you may set an OFFSET and a LIMIT so you can easily control them with a simple form.
For exaple you can send 300 email from the first of the db table

Code: Select all

$query = "select distinct email from users LIMIT 0, 300";
when done you may use a form with a "Continue" button to re-run the script changing the LIMIT information just to send 300 e-mail from the next 300 addresses of the db table

Code: Select all

$query = "select distinct email from users LIMIT 300, 300";
This is the idea, try some coding, if you have problems just ask for help. Remember to post your solution ;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

paladaxar wrote:hmmm...I downloaded the swift mailer. It looks really complicated. I have no idea what to do with all of those files. Also, I dont really need anything fancy, I just want to send a few hundred simple emails. Is there any other way to do this? In a simple way?
Oh come on... don't be scared! http://swiftmailer.org/docs/ is a good place to start. If you're looking for simple, than doing it by hand is not the way to go... libraries are meant to simplify things.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you don't watn to spam 300 users with a 'test' run.. You might want to run fakemail and use that as smtp server (instead of really delivering all those e-mails...)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if you're going to use mail, use the set_time_limit() function to prevent the page from timing out.
Post Reply