Sending mass email using PHP
Asked Answered
S

11

54

I am currently writing a music blog. The administrator posts a new article every 2-3 days. Once the administrator posts an article, a mass email will be sent to around 5000 subscribers immediately.

What is the best way to implement the mass mail feature?

Does the following function work?

function massmail() 
{
  $content = '...';
  foreach ($recipients as $r) {
    $_content = $content . '<img src="http://xxx/trackOpenRate.php?id='.$r.'">';
    mail($r, 'subject', $_content);
  }
}

Another question: If all 5000 subscribers are using Yahoo Mail, will Yahoo treat it as a DDOS attack and block the IP address of my SMTP server?

Scheming answered 13/7, 2009 at 7:47 Comment(3)
Yes, but rather poorly for 5000 recipients I'd imagine.Gaytan
What happens if I send 5000 emails with PHP mail() function. But all mails to be in BCC, not to loop with mail() ? Is it ok way ?Markos
@user346665: Since thousands of BCC emails will go into the email's header, then you will get the following fatal error (got it myself): php-server postfix/sendmail[26573]: fatal: [email protected](33): unable to extract recipients: message header length exceeds safety limitViaduct
L
49

First off, using the mail() function that comes with PHP is not an optimal solution. It is easily marked as spammed, and you need to set up header to ensure that you are sending HTML emails correctly. As for whether the code snippet will work, it would, but I doubt you will get HTML code inside it correctly without specifying extra headers

I'll suggest you take a look at SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (which is less likely to mark your mail as spam).

Ledeen answered 13/7, 2009 at 7:58 Comment(1)
+1 You may also want to do some delayed/timed sending, depending on how well you can control the mail server. Some destination servers may block you if you send hundreds of messages at once. Also, you may want to keep a detailed log on the status of each email sent. Having 100+ test email accounts (your test accounts), and testing them incrementally with different content will help.Stereoisomerism
M
39

I would insert all the emails into a database (sort of like a queue), then process them one at a time as you have done in your code (if you want to use swiftmailer or phpmailer etc, you can do that too.)

After each mail is sent, update the database to record the date/time it was sent.

By putting them in the database first you have

  1. a record of who you sent it to
  2. if your script times out or fails and you have to run it again, then you won't end up sending the same email out to people twice
  3. you can run the send process from a cron job and do a batch at a time, so that your mail server is not overwhelmed, and keep track of what has been sent

Keep in mind, how to automate bounced emails or invalid emails so they can automatically removed from your list.

If you are sending that many emails you are bound to get a few bounces.

Metalline answered 13/7, 2009 at 8:46 Comment(0)
T
18

This is advice, not an answer: You are much, much better off using dedicated mailing list software. mailman is an oft-used example, but something as simple as mlmmj may suffice. Sending mass mails is actually a more difficult task than it actually appears to be. Not only do you have to send the mails, you also have to keep track of "dead" addresses to avoid your mail, or worse, your mailserver, being marked as spam. You have to handle people unsubscribing for much the same reason.

You can implement these things yourself, but particularly bounce handling is difficult and unrewarding work. Using a mailing list manager will make things a lot easier.

As for how to make your mail palatable for yahoo, that is another matter entirely. For all its faults, they seem to put great stock in SPF and DomainKey. You probably will have to implement them, which will require co-operation from your mail server administrator.

Toothbrush answered 13/7, 2009 at 8:1 Comment(1)
Thanks for this advice, Michiel. I was looking to send out around 4000 emails to customers who pre-ordered from a website I manage. There are lots of good arguments to use a service, and when I looked at the cost to add these subscribers to my client's Mailchimp account (for one month), it was about a $30US increase. Much more economical than doing it by hand.Humism
D
15

You may consider using CRON for that kind of operation. Sending mass mail at once is certainly not good, it may be detected as spam, ddos, crash your server etc.

So CRON could be a great solution, send 100 mails at once, then wait a few minutes, next 100, etc.

Durnan answered 13/7, 2009 at 8:21 Comment(2)
When running a PHP script from CRON there is no php timeout as you are running PHP from the command line. This means instead of setting a CRON every few minutes you could put a sleep cmd in your code that counts 100 emails sent then sleep(some_time) and the script will continue.Daladier
A little addition to support @Daladier 's statement: php.net/manual/en/features.commandline.differences.phpViaduct
S
11

Do not send email to 5,000 people using standard PHP tools. You'll get banned by most ISPs in seconds and never even know it. You should either use some mailing lists software or an Email Service Provider do to this.

Stockmon answered 13/7, 2009 at 10:54 Comment(1)
That might be true yes, but if you keep your mails below 100 messages per hour then most ISP's wouldn't give you to much grief; however as soon as you abuse their network then you're in for it! Just make sure your headers are structured to standards; Don't abuse networks and you'll be safe.Pray
P
8

Why don't you rather use phplist? It's also built on top of PHP Mailer and a lot of industry leaders are using it. I've used it myself a couple of times to send out bulk mails to my clients. The nice thing about phplist is that you can throttle your messages on a domain level plus a time limit level.

What we've also done with a couple of internal capture systems we've got was to push our user base to the mailling list and then have a cron entry triggering a given mail each day. The possibilities are endless, that's the awesome thing about open source!

Pray answered 13/7, 2009 at 9:53 Comment(1)
Do you mean www.phplist.org instead of .com ?Zeiler
S
4

Also the Pear packages:

http://pear.php.net/package/Mail_Mime http://pear.php.net/package/Mail http://pear.php.net/package/Mail_Queue

sob.

PS: DO NOT use mail() to send those 5000 emails. In addition to what everyone else said, it is extremely inefficient since mail() creates a separate socket per email set, even to the same MTA.

Silici answered 13/7, 2009 at 17:9 Comment(0)
D
3

Also have a look at the PHPmailer class. PHPMailer

Deterrent answered 13/7, 2009 at 8:30 Comment(2)
If I am not wrong, isn't SwiftMailer built on top of PHPMailer?Ledeen
There are alot of things built on top of PHPMailer, PHPList is also built on top of PHPMailerDeterrent
H
1

You can use swiftmailer for it. By using batch process.

<?php
    $message = Swift_Message::newInstance()
      ->setSubject('Let\'s get together today.')
      ->setFrom(array('[email protected]' => 'From Me'))
      ->setBody('Here is the message itself')
      ->addPart('<b>Test message being sent!!</b>', 'text/html');

    $data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
    while($row = mysql_fetch_assoc($data))
    {
       $message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
    }

    $message->batchSend();
?>
Horror answered 6/7, 2017 at 9:41 Comment(0)
C
0

I already did it using Lotus Notus and PHP.

This solution works if you have access to the mail server or you can request something to the mail server Administrator:

1) Create a group in the mail server: Sales Department

2) Assign to the group the accounts you need to be in the group

3) Assign an internet address to the group: [email protected]

4) Create your PHP script using the mail function:

$to = "[email protected]";
mail($to, $subject, $message, $headers);



It worked for me and all the accounts included in the group receive the mail.

The best of the lucks.

Colza answered 14/1, 2016 at 16:5 Comment(0)
E
0

There is more into it aside from using a software. If you could create a bulk emailer program that sends intermittently. Say if you will send 5,000 recipients, create a loop that would send 38 lists per sending then pause for 10 seconds. I have an actual experience sending 500 manually per days for the past weeks and so far i have good results.

Another consideration are the content of your email. Nowadays it is a standard that you need to put your physical office address and the "unsubscribe" opt-out. These are factors that majority of recipient emails servers are checking. If you don't have these they will classify you as spammer.

Mailchimp is my best recommendation to use if you want a paid service provider in sending to your email subscriber NOT sending unsolicited or cold email marketing.

Hope it helps.

Elis answered 12/10, 2019 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.