johnnietheblack
johnnietheblack

Reputation: 13330

Automated mailing list - PHP

i have an application on my site where people can sign up to receive newsletters, and then someone in a paid group can write and send one immediately to everybody who signed up...

...meaning that i need an efficient way to loop through my database of subscribers and mail them copies of the email using php.

clearly, there is the mail() function....i could put that in a loop...is there a better way?

Upvotes: 2

Views: 1360

Answers (5)

Jasper
Jasper

Reputation: 1858

I'd suggest finding a way to loop through, and remembering who you mailed already, because if it becomes a large list of people, you script might end and you'd have to reload it.

I have done it once using AJAX, gave me a great way to track where I was in the sending proces. Counted how many people to mail, put the id's in an array, had javascript loop and make seperate calls to a php-mail-page...

-edit- You can have a script in php, with a simple while loop, but then you should add a check in the DB to see if a mail was already sent to one person. If the script exceeds memory usage, just reload the page, and it will only sent to the ones that haven't received it yet...

Upvotes: 1

warren
warren

Reputation: 33435

Following on @paulbm's answer, why not create an alias on your server that points to all current email addresses? A short procmail script can prevent anyone other than one authorized sender using the alias.

It'd make mailing's easy, and rebuilding the list with new/changed email address would be pretty simple, too.

Upvotes: 1

PaulBM
PaulBM

Reputation: 103

You could use the BCC header option and send one email with a Blind Carbon Copy list of all the subscribers. So build the BCC string in the loop and send one email using mail()

Snippet from the PHP manual...

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

Replace 'Bcc: [email protected]' with $mySubscribersList

Upvotes: 3

theomega
theomega

Reputation: 32031

Try phplist (Homepage) if you need a full featured newsletter and mailinglist manager

Upvotes: 0

Sampson
Sampson

Reputation: 268344

PEAR's mail queue?

The Mail_Queue class puts mails in a temporary container, waiting to be fed to the MTA (Mail Transport Agent), and sends them later (e.g. a certain amount of mails every few minutes) by crontab or in other way.

Upvotes: 3

Related Questions