简体   繁体   English

使用PHP MySQL的电子邮件通知系统

[英]Email Notification System Using PHP MySQL

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. 我希望每天发送超过5000封电子邮件。 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. 使用cron作业定期获取用户并发送电子邮件是否有效,或者有更好的方法来推送此电子邮件,同时避免电子邮件提供商在我的IP上将其列入黑名单。

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. 因此,无论何时添加帖子,我都会在第一个表(通知)中插入通知,然后从另一个存储用户ID和组ID的表中(以下)获取该组的所有关注者。 I then insert for each user in the second table (user_notification). 然后,我在第二个表(user_notification)中为每个用户插入。

My cron job fetchs like 20 records from user_notification table and also fetch the notification message from notification table. 我的cron作业从user_notification表中获取了20条记录,还从通知表中获取了通知消息。 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? 请问有什么更好的方法吗?

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. 除非您与ISP有非常特殊的关系,否则极有可能将您列入黑名单,否则您的电子邮件将直接成为垃圾邮件。 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. 您可以使用群发邮件服务API来为您完成一些肮脏的工作,例如黑猩猩或经常联系。

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. 因此,您拥有的代码很可能会工作,减去应通过邮寄服务及其关联的API完成的电子邮件位。

5000 emails a day is roughly 4 emails per minute, or one email every 15 seconds. 每天5000封电子邮件大约是每分钟4封电子邮件,或者每15秒一封电子邮件。 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. 如果您是我,我会编写一个cron例程,使其每分钟运行一次,该例程将发送应发送的电子邮件。 This will ensure you avoid being treated like a spammer for sending out 2500 emails quickly, for example. 例如,这将确保您避免像垃圾邮件发送者那样被视为快速发送2500封电子邮件的人。 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. 您还可以在用户收到通知后立即发送电子邮件,而无需执行cron例程,但是,如果您的网站由于某种原因而导致活动激增,您可能会发现某些电子邮件无法通过。

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. 您还可以考虑使用像MailChimp这样的第三方服务,该服务已经在电子邮件提供商中拥有良好的权限,但是这种服务的价格很高,每分钟要发送4封电子邮件,我认为这不值得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM