Page 1 of 1

Sending a E-mails to all users

Posted: Tue Jun 29, 2010 11:21 am
by jscrinc
When people sign up to my site, they will add their e-mail address to the system like any other login system, now I want the system to, when I hit a button in the admin area it will automatically send this email

Any help please!!!

Re: Sending a E-mails to all users

Posted: Wed Jun 30, 2010 9:27 am
by Jade
You need to query the database for all email addresses and then loop through them and send out an email one at a time.

Re: Sending a E-mails to all users

Posted: Wed Jun 30, 2010 9:42 am
by batfastad
Yep, and for the sending of the emails I would recommend the SwiftMailer PHP class... http://www.swiftmailer.org
Originally written by one of the Mods of these very forums!

IMO using PHP's built-in mail function is a bit old-school these days. Needs some work to get it working securely.

Re: Sending a E-mails to all users

Posted: Thu Jul 01, 2010 8:51 am
by jscrinc
Oh right, so there is no other way of doing it except one at a time, cause it would save on the time wasting??

Re: Sending a E-mails to all users

Posted: Thu Jul 01, 2010 9:09 am
by batfastad
If you used swiftmailer then you would query your DB. Then loop through the DB results, building an array of e-mail addresses.

Then give that array of e-mail addresses to your swiftmailer construct and swiftmailer will do the actual sending.

However depending on how many recipients you need to send to, that script might take a while to run.
I used swiftmailer recently to send mail to 5,000 of our customers and that took about 10 minutes to complete and that was sending through postfix on the same machine.
Then our local postfix had to process the queue and upload the actual messages to our remote postfix relay (a further 20 mins). Then our remote postfix relay dispersed them onto the internet from there (a further 5 mins). But we do that because sending mail from your ISP-allocated IP address is not really very cool so it's better to go through a remote relay.

The reason it takes so long is that swiftmailer has to do all sorts of chatter between itself and the SMTP server. Connecting, submitting the message, disconnecting, connecting again etc. It's just alot of communication is needed between PHP/Swiftmailer and the SMTP server to submit the messages.

Also when running lengthy e-mail scripts it's always best to run it from the command line so the script doesn't get terminated by the max_script_duration parameter in your php.ini

HTH