How would i send an email, to say 3000 recipients - with a Max 500 emails / hours on my dedicated IP? So far my thought is to send each email every 9 seconds, this would come to about 450 emails an hour... but how could i do this?
My plan for the sending of the emails would be the following...
$emails = ARRAY OF EMAILS, MYSQL RESULT
for($emails){
mail($subject,$row[email],$headers);
}
This wont work, wrong kind of statement but this concept anyway....
What I would do is :
create a PHP script that is launched via cron once per hour
this script only sends 450 e-mails, at its own speed
when the 450 mails are sent, the script dies
and some time later, it is re-launched, by cron, to send 450 other mails.
The trick is : you have to know which mails where already sent.
Ordering the mails by id in your DB, or something like that, and using limit, would be OK, I suppose
If you want to sleep for a while between mails, use the sleep function ; something between 2 and 5 seconds would probably be OK, to be sure you script the chunk of 450 mails is finished before the script is re-launched by cron.
And, thinking about it :
You should put some logging stuff in place : if someone complains, saying he received 10 emails, it could help you find out why.
I wouldn't use the mail function : there are plenty of other possibilities, using libraries that are well-tested and provide lots of functionnalities, already developped : don't re-invent the wheel ;-)
Here are a couple of libraries I can think about :
Swift Mailer
Zend_Mail
PHPMailer
PEAR::Mail
Rmail
Store You messages for sending in a database, mark messages which are sent.
In a cron job select some of them that are not sent, and process them.
The frequency of the cron job determines the speed of sending the emails.
SwiftMailer does it this for you:
http://swiftmailer.org/docs/antiflood-plugin
http://swiftmailer.org/docs/throttler-plugin
You could use this very handy Timer class to do the heavy lifting for you (start, stop and get the elapsed time within your loop, etc): PHPClasses: Timer.php.
Thanks for all the answers!
The best way i found was actually to simply sleep() between calls using the sleep() as i tested 400 mails, this took 17 seconds :)
It is unlikely the user will send more than the 450 limit ... but if they do i have an if statement before the while() ends checking if there are more than 450 rows, if so it will sleep between each... this works without fiddly databases :)
Thanks!
Related
Hoping someone has some suggestions or work-arounds.
I recently wrote an email parser to work for mail forwarded through a pipe in CPanel and have the details entered in a mySQL table.
The client now wants historical mail importing to this database and has approx 50 Gmail accounts with some having over 20,000 messages in.
I have been able to get up to around 7000 emails out of 25000 on the 'test' account I'm using. To try and work out why I've stripped it down so there's a very simple php imap script running just to dump the a couple of parts of the email in to a cell in a table and was going to worry about parsing it after, but the same thing happens. It hits around the 6000-7000 email mark and stops - no errors or anything, it just stops.
I've just found out that Gmail has a bandwidth limit for IMAP of 750MB per hour and doing the math it looks quite likely I'm hitting that and that's why it's stopping.
First question - anyone know how I can check if that's the reason?
Secondly - assuming it is that can anyone think of a best solution?
I considered doing it in blocks of a few thousand emails but this would mean a lot of manual intervention because of the number of accounts and it would take a LONG time to download them all which I don't have.
The second (and so far best) idea I've had is to use one of Gmail's recommended methods to migrate all the mail to an email address created on the client's Cpanel and use the inbuilt pipe feature to forward the mail through my script in the same way I'm doing with their new mail. (I'm not 100% sure the migration will trigger CPanel to apply the pipe rule, I'd have to test).
Appreciate any input.
thanks in advance.
I don't have an answer to your first question.
But as far as your second question goes you don't necessarily have to re-run your program manually if you split the task up into a couple thousand emails. You could either use sleep to stop the program for an hour after x number of emails have been pulled over, or you could setup a cron job that runs once per hour and you would just have to save a pointer to the last email you processed in a database or temp file so you know where to pick up and start again next cron job run.
I have been working on this subject for a long time now and to be honest I am in need of some help if you guys have some time to answer to my problem ;)
I have a few k of emails in my database (around 2k and coming up...) and I would like to send two newsletters a week ... what would be the best way to achieve that ?
So far here is what I am doing :
- a script php (with meta tag refresh set to 600) which sends every 10 minutes an email to 50 people (using basic mail() function). It runs for 6 hours to get to the 2k emails. I am using the less expensive shared server at 1and1 (2EUR a month :D).
1- Is this way to proceed a good way ? I heard when you put people in BCC you go to SPAM directly ... Also many people do not even receive the emails ... no idea why. So far it is not really conclusive.
2- Do you think it would worth taking the dedicated server and send 1 email to each people thats to say 2k different emails ? Or is it a bad move ?
3- How do people proceed when sending newsletters ? I'm sure I look ridiculous with 2k emails and though I have difficulties in making my way through this ;)
Any help welcome.
Cheers.
G/
maximize time between connections to same mail servers
maximize number of servers you use
send to one person at a time
include images, text and html version, unsubscribe link
Check what mail servers respond (like IP blocking notice with link to unblock)
And yes, use cron
I have a cron which generates the whole mail info and puts in a database table using $mail_queue->put(.....) with an option to delete emails after they're sent.
Here's where I need a little help: What is the best way to send the emails after I have the above info? Running the $mail_queue->sendMailsInQueue() right away, using other cron job(s) or something else?
The server limit by the way is 100 emails / minute. Currently the last csv diff for Mail_Queue is not applied (currently working with the support on that), so I can't use the "delay" option.
I had an idea to use the $seconds_to_send option, but it's calculated on the base of create_time field, which isn't a big help, but it's also an option.
Any ideas and suggestions would be really appreciated.
Personally, I would go the cron way because it gives less opportunity for failure. Say your mail server stops responding or for some other reason becomes unavailable. Or what if your entire network goes offline for a few hours, but the servers are still generating emails. I say use the queue.
As for the delay thing, just have a service/cronjob pick up the queue every sixty seconds, pop 100 emails and send them, then quit. You might get a queue of emails to be sent but that's going to happen no matter what system you choose. The queue will empty during off-peak hours, anyways.
use two scripts. one for populating your mail_queue table with the emails you need to send and the second script to send those emails in chunks of 90 mails at a go. set the second script to be activated about every 2 minutes or so.
you could also just upgrade your hosting plan ;-)
why you dont loop through 100 emails and sleep for 60seconds. this costs you no servertime, the only downside your script runs a little longer.
I have to made a page which will send Email to Newsletter subscribers. There is more then 14000 subscriber. I want to use php mail() function to send Email to them. But I'm afraid that it will not be able to send email to all subscribers for php 30sec max_execution_time limit. Its not possible to test how much Email can be sent by sending Test Email to subscribers. So I want to know how much Email can be sent with mail() function in 30 second max_execution_time limit.
Will be very helpful if you can answer me.
also another question - Is mysql execution time is also count in php?
Apache version 2.2.13 (Unix)
PHP version 5.2.11
The php max_execution_time setting is customizable. 30 seconds is the default but you can set it to 0 seconds for no execution time limit at all. Use set_time_limit().
set_time_limit(0);
If you do this, you should be able to send all your email.
Please be careful about sending more than one email to the same mail server per second. You don't want to get blacklisted.
You should run this from a cronjob or spawn a background task or use something else better suited to batch jobs.
You might get 14000 emails out in 30sec if your mailserver is fast enough, but what happens when you get a few more subscribers and it stops working properly?
Perhaps you can set a flag in the database for each user, then reset the flag as their email is sent by a background task. That will help to avoid duplicates and so on if there is a problem with the mail server.
That depends on so many variables that a single answer is not possible. Factors include:
The speed of the CPU
The bandwidth available from the sending system to the MTA
The MTA's capacity to accept emails
The only way to find out is to try it.
I had this exact problem a while back on one of my projects. The solution is to isolate the email sending from the actual site.
I coded a small class that would be called to send an email. It would be passed a templated email, which it would then store into the database in a mail queue. On the back end, I had a cron job that called a mailer script every X seconds. Script looks at the database queue for emails, grabs X number from the queue to attempt to send (ordered by timestamp in), then would attempt delivery. Assuming no errors were thrown, script would mark the message as sent. Next step would be to purge all emails from the queue that were sent and older than X days (kept for logging).
Hope that is helpful.
Seriously, if you want to send the same mail to ten people from your normal mailapp, do you normally create ten identical mails or do you just send the mail once adding the recipients to the send list?
Edit: If the answer is "I send it once", I think you should look in that direction here as well (it is even described how to send to multiple recipients at http://www.php.net/mail)
What is the most proper way to sending email of minimal 1000 or more in PHP? Any reliable email queuing technique that is capable to handle that?
You could just insert your emails into a Mail Queue database table, and have a separate process check the queue and batch send a certain number at once.
There's a tested solution for that: PEAR Mail_Queue
Works fine for me.
as mercutio suggested, i would insert a new record into a mail queue table for each email waiting to be sent and then use a separate process (like a CRON) to check the table periodically for any queued items.
if any emails are queued (and the email is not customised for each recipient) i would then group the emails by domain and send blocks together to reduce the total number of emails that have to be sent, i.e. if you have 1000 emails queued and 250 are to gmail accounts i would send the 250 in 25 blocks of 10 (remember to Bcc recipients to avoid them seeing each other).
to actually send the mail i would use PEAR mail over php's mail() function
after sending the email either delete record(s) from the queue or change a status flag to show it was sent and loop - i would also add a counter to keep track of emails that failed to send and remove them after x failed attempts
to overcome timeout issues i would either,(depending on the situation)
- set the set_time_limit() to x seconds and keep track of the script execution time (killing the script after (x-1) seconds)
- call the script from the command line to avoid timeouts
- set a limit to the number of emails the script could send in one execution
Sure, the database table might be an idea. But what about sending 1000 e-mails with a 2mb attachment? you'd have to take that into account as well. I had the problem myself, and I eventually resorted to writing the e-mail to the database, and the files to the filesystem. The e-mail script I used then read the database records, and tried to fetch the attachments to send.
Are you sure you need do this mail queuing yourself?
Just deliver all mail to the local machine's mail transfer agent (sendmail...) and let that take care of the queuing and sending. After all, that's what was designed for.
In other words: don't worry about it!
I created Emailqueue, which is a server that allows you to add emails to a queue so your app get relieved of the stress of the mailing, and also provides useful additional options, like the ability to program emails to be sent in the future, or setting per-email sending priorities. I think this might very well be what you're searching for.
Emailqueue is available here: https://github.com/tin-cat/emailqueue
And there is also a Docker version that allows you to set up a working Emailqueue server in just a few minutes, here: https://github.com/tin-cat/emailqueue-docker
I've generally relied on a hack.
I have a database list of email addresses and then use a meta-redirect to self with an increasing 'offset' parameter that specifies which row in the database I am up to. Server redirects cause problems because browsers assume that the time taken indicates an infinite loop.