Jump to content

Needing a PHP Script to send emails.


Kekox

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.