Kekox Posted September 29, 2011 Share Posted September 29, 2011 Hi! I have a mysql database with a lot of emails there and I want to send an email to all the email addresses that are on my database.. There is like 7000 email addresses so could it be possible to set a interval time between each email sending? If it helps i'm using Linux / Debian 5 32bits Apache2 - php5 and php5-mysql Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/ Share on other sites More sharing options...
voip03 Posted September 29, 2011 Share Posted September 29, 2011 you can use sleep() http://php.net/manual/en/function.sleep.php Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/#findComment-1273852 Share on other sites More sharing options...
the182guy Posted September 29, 2011 Share Posted September 29, 2011 Also usleep() if you want to delay execution accurate to less than a second. You might want to send the emails in batches as well with wait in between. Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/#findComment-1273861 Share on other sites More sharing options...
xyph Posted September 29, 2011 Share Posted September 29, 2011 I'd store all the emails you'd like sent out in a database table with 4 columns table emails: CREATE TABLE IF NOT EXISTS `emails` ( `id` int(11) NOT NULL, `body_plain` text NOT NULL, `body_html` text NOT NULL, `modified` datetime NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; You then store emails to be sent out in a table like this perhaps CREATE TABLE IF NOT EXISTS `mail_que` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `email_id` int(11) NOT NULL, `created` datetime NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Now, say you wanted your script to send that email to everyone in the `users` table. You would execute a query like this INSERT INTO `mail_que` (`user_id`,`email_id`,`created`) SELECT `id`,5,NOW() FROM `users`; Where 5 is the id of the email in the emails table you want to send. Here's the tricky part. 7000 emails is a LOT to send out. If you're on a shared host, you may want to let them know what you plan on doing, and how you plan on doing it. Assuming you don't want to stress the SMTP server, I would create a CRON job to run every minute, and grab the first 20 emails from the queue. If the mailing is successful, delete those 20. This will take the script just under 6 hours to send 700 emails. You can do that with the following query: SELECT `q`.`id`, `u`.`email` FROM `mail_que` as `q` LEFT JOIN `users` as `u` ON `q`.`user_id` = `u`.`id` LIMIT 20; Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/#findComment-1273902 Share on other sites More sharing options...
Kekox Posted September 30, 2011 Author Share Posted September 30, 2011 I could done it. thank you for your help! but I'm having a problem.. For example I'll send 100 emails to the first 100 addresses listed on my database, if 30 of them are unactive then I get 30 emails with the error 'Mail Delivery Subsystem' is there a function I could use to check if the email is active before seding? Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/#findComment-1274382 Share on other sites More sharing options...
Buddski Posted September 30, 2011 Share Posted September 30, 2011 You can attempt to validate the email addresses by connecting to the MX records on the domain. This doesnt always work. One option is to send from an "unmaintained" email address, then using a Cron Job, PHP will log into the email and check for failed deliveries (some deliveries are unknown email address, others can be out of office auto-responses) and put a mark next to the email address (either a dead mail or a suspend sending for X amount of time) Quote Link to comment https://forums.phpfreaks.com/topic/248079-needing-a-php-script-to-send-emails/#findComment-1274385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.