Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending +-200 emails using php mail() function in a loop

Tags:

php

email

mysql

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. Source: PHP manual

What are larger volumes? A 100 or a 1000?? Can I safely make it loop 200 times without much problems? (I can't install pear)

like image 426
Glenn Avatar asked Apr 14 '10 17:04

Glenn


People also ask

What is PHP mail () function?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

How does PHP mail send email?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );

What is the best way to send an email from PHP?

Using the PHP mail() function. PHP's built-in mail() function is one of the simplest ways to send emails directly from the web server itself. It just takes three mandatory parameters: the email address, email subject and message body—and sends it to the recipient.

How many emails can PHP mail send?

Thus, it is possible to send 20, 2000 or 20000 e-mails, with the mail function of PHP, without risk of being blocked.


1 Answers

You can loop it 200 times with few problems I would imagine, although it will be much slower than a custom mailer or a package set up properly to handle that.

The end result depends on many factors. The main thing you'll want to make sure of is that you use set_time_limit() to give the script enough time to do the work. Offloading the work into some kind of queue that's serviced by a cron script can make life easier on you as well, as keeping PHP scripts running for a long time will bring up other resource problems.

Back in the day, I used to send about 50,000 emails to a subscriber newsletter using PHP's mail function and a RedHat server with Exim installed. It would take 4-6 hours with the custom script I had running. There was nothing efficient about it, but it did the job.

like image 138
zombat Avatar answered Sep 19 '22 05:09

zombat