I know how to send email using PHPmail. But what if I need to send lots of emails?
This could be a “notification” to site subscribers when a new message is posted. Doing that directly in page handler will seriously affect performance and make browser loading too slow. So I need to have this job done in the “background”.
How can I “schedule” something in PHP, in my case – sending emails? In C++ for example I would create a separate thread but AFAIK there is no such thing as multithreading in PHP.
Create CRON job that sends out email-queue you’ve saved in a database:
Save an email with all info into a db.
Use a CRON job to periodically (like every half hour or so – depends on your hosting provider and the number of emails you’re sending out) grab emails from the queue and send them out. Then set a
sentflag on the email in the database, add sending info you want (e.g. time, errors, header, …).Be sure to break up the emails to chunks and send out them with pauses to avoid problems with anti-spam and anti-flood protections of some email servers. Some mailer libraries have plugins for this (like SwiftMailer).