Page 1 of 1

Sending a few hundred emails with mail() function

Posted: Tue Dec 26, 2006 9:30 pm
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

Posted: Tue Dec 26, 2006 9:40 pm
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.

Posted: Tue Dec 26, 2006 9:51 pm
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?

Posted: Wed Dec 27, 2006 4:00 am
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 ;)

Posted: Wed Dec 27, 2006 10:34 am
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.

Posted: Wed Dec 27, 2006 10:58 am
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...)

Posted: Wed Dec 27, 2006 11:01 am
by Burrito
if you're going to use mail, use the set_time_limit() function to prevent the page from timing out.