Email Notification System Using PHP MySQL

10,469

Solution 1

Unless you have a very specific relationship with your ISP, it is extremly likely you will be blacklisted, or your e-mails will go straight to spam. It is difficult to establish mass e-mails as legitimate. You can use a mass mailing services API to do some of the dirty work for you, like mail chimp or constant contact.

So the code you have will most likely work, minus e-mailing bit which should be done via a mailing service and their associated API.

Solution 2

5000 emails a day is roughly 4 emails per minute, or one email every 15 seconds. If I were you I would write a cron routine to run every minute that will send the emails which are due to be sent. This will ensure you avoid being treated like a spammer for sending out 2500 emails quickly, for example. It also means your users get email notifications quickly.

You could also send out the emails as soon as a user gets a notification, bypassing the need for a cron routine however if your site hits a surge in activity for whatever reason you could find some emails don't get through.

You could also consider using a 3rd party service like MailChimp which already has good authority with email providers, however that comes at a price and for 4 emails a minute I wouldn't consider it worthwhile.

Share:
10,469
andychukse
Author by

andychukse

Updated on June 04, 2022

Comments

  • andychukse
    andychukse almost 2 years

    I am currently working on a project that requires sending email notifications to users. The way it works, users follow certain catergories of posts/group, whenever there is a new post or comment under the post category/group they get email message notifying them with a snippet of the post itself.

    I am expecting to send upwards of 5000 emails daily. Is it effective to use cron job to fetch users and send the emails at intervals or is there a better means to push this email while avoiding blacklisting on my IP by email providers.

    Below is my table structure

    First Table

    Notification
    ID // autoincrement
    message // the message sent to users
    createDate // date created
    

    Second Table

    User_Notification
    ID // auto increment
    userid // Id of users
    notification_id // id of notification from first table
    status // 0 or 1 
    

    So whenever a post is added, I insert the notification in the first table (notification) and then fetch all followers of that group from another table (following) where i store userids and the group ids. I then insert for each user in the second table (user_notification).

    My cron job fetchs like 20 records from user_notification table and also fetch the notification message from notification table. I also fetch user email from my user table.

    $firstquery = $lnk->mysqli->query("select * from user_notification where status=0 order by id ASC limit 0, 20", MYSQLI_USE_RESULT);
    $secondquery = $lnk->mysqli->query("select * from notification where id=$notification_id", MYSQLI_USE_RESULT);
    $thirdquery = $lnk->mysqli->query("select email from user_table where id IN($userids)", MYSQLI_USE_RESULT);
    
    for($no=0;$no<counter;$no++)// loop to send emails
    {
    $lnk->emailsender($subject,$emailbody,$usr_email[$no]);
    }
    

    Please is there any better way to do this?

  • andychukse
    andychukse almost 11 years
    Thanks I'm going for mandrill.com (owned by mailchimp). I appreciate your candid advice.