Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Mass Email Best Practices? (PHPMailer + Gmail)

I'm thinking about how to handle sending large amounts of email from my web applications, and whether there are any best practices for doing so. StackOverflow is already labeling this as 'subjective', which it may be to an extent, but I need to know the most successful way to implement this system, or whether any standardized approach exists.

In my webapp, there are users who are heads of groups of 1 to 10,000 users. This user must be able to email send a message to all of these users through my system. Therefore, my system is responsible for sending up to 10,000 emails to individual users for each group head.

As far as I can tell, there is no rate limit in GMail for sending messages to individuals (although there is a 500 recipient max).

Right now, my current setup is:

  • When a message is sent through the system, it enters an email queue.
  • A cron script grabs messages from the queue every few minutes, and sends out those emails.
  • All email is taking place through GMail's SMTP server.
  • The actual application doing the mailing is PHPMailer.

This setup, as the user base grows, will probably not suffice. The questions I have are:

  1. Should I be using a local SMTP server instead?
  2. Should I use a mail binary on the local machine instead? I this case, I could probably skip the queue altogether?
  3. Is there an accepted way to do this?

Thanks!

like image 674
Kenny Avatar asked Nov 26 '09 17:11

Kenny


People also ask

Does PHPMailer need sendmail?

No, PHPMailer (and any other mail library for any programming language, for that matter) is just an interface for the email service and not a mailer daemon by itself. You do not need POP (you can shut that down anytime) but you will need sendmail or some other SMTP service to actually do the work PHP Mailer requests.

What is PHPMailer used for?

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.

How do I know if PHPMailer is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>


1 Answers

Google App Engine

I would write this in Google app engine (python) because:

  • It scales well.
  • It has a good email api.
  • It has a taskqueue with a good api to access it.
  • Because python is a real nice language.
  • It is (relatively) cheap.

PHP

If I would implement it in PHP I would

  • Find yourself a good SMTP server which allows you to sent this volume of mails because Gmail will not allow you to sent this kind of volume. I am for sure that this will cost you some money.
  • Find yourself a decent PHP email library to sent messages with like for example PHPMailer like you said.
  • Use a message queue like for example beanstalkd to put email messages in queue and sent email asynchronously. First because with this the user will have snappier page load. Second with a message queue like beanstalkd you can regulate speed of sending better which will prevent from overloading your pc with work. You will need to have ssh access to the server to compile(install) beanstalkd. You can find beanstalkd at beanstalkd
  • You would also need ssh access to run a PHP script in the background which will process the message queue. You can find a beanstalkd-client at php beanstalkd-client

from php/apache/webpage

This is the page from which you will sent messages out to user. From this page you will sent message to beanstalkd by coding something in the lines of this:

// register Pheanstalk class loader
require_once('pheanstalk_init.php');
$pheanstalk = new Pheanstalk('127.0.0.1');
$message = ""; // This would contain your message
$pheanstalk->put(json_encode($message);

You have to put messages in message queue using put command

From long running PHP script in background:

The code would look something like this:

// register Pheanstalk class loader
require_once('pheanstalk_init.php');
$pheanstalk = new Pheanstalk('127.0.0.1');

while(true) {
  $job =  $pheanstalk->reserve();
  $email = json_decode($job->getData());
  // Sent email using PHP mailer.
  $pheanstalk->delete($job);
}

Like I am saying it is possible with both PHP and Google app engine but I would go for app engine because it is easier to implement.

like image 159
Alfred Avatar answered Sep 20 '22 17:09

Alfred