如何使用 PHP 向用户发送每日电子邮件通知?

发布于 2024-11-07 19:27:29 字数 832 浏览 0 评论 0 原文

我有一个简单的用户注册表单,其中有一个复选框,如果用户的任何项目有活动,用户可以收到每日电子邮件通知...就像 Stack Overflow 有一个“通知 [电子邮件受保护] 每日收到任何新答案"。

我目前在 LAMP 环境中实现此功能的想法如下:

  1. 在用户数据库中,如果用户希望收到每日电子邮件,则设置一个布尔值。

  2. 如果有任何项目活动,项目将使用当前时间戳进行更新。

  3. 每天晚上(午夜),都会执行一个 PHP 文件(可能通过 cron 作业),该文件扫描项目数据库以识别当天哪些项目有活动。对于有活动的项目,选择项目所有者名称并扫描用户表以检查用户是否希望收到每日电子邮件通知。如果是,添加到收件人列表;否则,忽略。

在开始实施之前,如果我有一些问题/疑虑,希望得到一些指导:

  1. 我处于共享托管环境中。我需要采取哪些预防措施,以免被托管公司或接收邮件服务器误识别为垃圾邮件?

  2. 我是否需要“分块”出收件人列表(一次 50 封电子邮件)并向每个组发送电子邮件?这就像放置 sleep(30); 一样简单吗?在每次调用 mail() 之间?

  3. 我正在使用 CodeIgniter 框架,并将让 cron 作业调用控制器中的适当函数以在午夜运行它。如何限制仅来自 cron 作业的调用,以防止某些未经授权的用户从浏览器调用此函数?

谢谢。

I have a simple user signup form with a checkbox that lets users get a daily email notification if there was activity on any of their projects...much like Stack Overflow has a "Notify [email protected] daily of any new answers".

My current thinking to implement this in a LAMP environment is as follows:

  1. In the user database, set a boolean value if the user wishes to receive a daily email.

  2. If there is any project activity, the project gets updated with the current timestamp.

  3. Each night (midnight), a PHP file is executed (likely through a cron job) that scans through the project database to identify which projects had activity that day. For projects with activity, the project owner name is selected and the user table is scanned to check if the user wishes to receive a daily email notification. If yes, add to a recipient list; else, ignore.

Questions / concerns I have that would appreciate some guidance on before I start to implement:

  1. I'm in a shared hosting environment. What precautions do I need to take from being misidentified as spam either by the hosting company or the receiving mail servers?

  2. Do I need to "chunk" out the recipient list (50 emails at a time) and email each group? Is this as simple as putting a sleep(30); between each call to mail()?

  3. I'm using the CodeIgniter framework and will have the cron job call the appropriate function in a controller to run this at midnight. How do I limit calls from only the cron job to prevent some unauthorized user from calling this function from the browser?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花ソ最美的依靠 2024-11-14 19:27:29
  1. 如果您确实更改了 php 中的“From”标头,请确保将其更改为该服务器上托管的域。当 b.com 的服务器发送邮件 @a.com 时,它看起来很可疑。

  2. 我会单独发送电子邮件foreach ($Users as $User)...,因为这允许您个性化电子邮件内容。即使您现在不需要个性化电子邮件,以后也可能需要,并且当您需要时,对它的支持已经存在。

  3. 首先,我会将脚本存储在网络根目录之外。我不确定 CodeIgniter 是否允许您执行此操作,但该脚本不需要由 Apache 提供服务。 Cron 不关心脚本存储在哪里。此外,我还检查了脚本执行的时间。如果不是午夜,就不要发送电子邮件。另外,您可以保留日志,并在发送前检查以确保当天尚未发送电子邮件。

  1. If you do change the "From" header in php, make sure you change it to the domain that's hosted on that server. It looks suspicious when mail @a.com is being sent by b.com's servers.

  2. I would send the emails individually foreach ($Users as $User)..., since this allows you to personalize the email contents. Even if you don't need to personalize emails now, you might want to later, and the support for it will already be there when you need it.

  3. First, I would store the script outside of the web root. I'm not sure if CodeIgniter will let you do this, but there is no need for the script to ever be served by Apache. Cron doesn't care where the script is stored. Additionally, I've checked the time when the script is executed. If it's not midnight, then don't blast out the emails. Also, you could keep a log around and also check to make sure the emails haven't already been sent that day before sending.

2024-11-14 19:27:29

1) 从 SPF 记录和 DKIM 开始(如果可以的话),让邮件服务器知道期待来自您的服务器的电子邮件

2) 首先,您需要将收件人放在 BCC 字段中,以便不是每个用户都拥有 49 的电子邮件地址您系统上的其他用户。进一步的一步是单独处理每封电子邮件,仅将收件人放入“收件人”字段中。这种方法还允许您为用户定制每封电子邮件(也许输入“嗨[名字]”。

3)让 cron 作业像这样
<代码>
wget http://localhost/send-emails

然后在您的脚本中,检查 $_SERVER 并确保只允许来自 127.0.0.1 的请求

1) Start with an SPF record and a DKIM if you can that lets mail servers know to expect email from your servers

2) First, you need to put the recipients in the BCC field so that it not each user has the email addresses of 49 other users on your system. One step further is to do each email separately, putting only the recipient in the TO field. This approach also allows your to tailor each email to the user (perhaps putting in "Hi [First name]".

3) Have the cron job something like this

wget http://localhost/send-emails

Then in your script, check $_SERVER and make sure you only allow requests from 127.0.0.1

莫相离 2024-11-14 19:27:29

关于第三个问题:您可以使用 .htaccess 文件来阻止访问该特定页面,也可以使用命令行参数在 cron 中调用脚本,并在 $argv

About the third question: You can either use an .htaccess file to prevent access to that specific page or you can call your script in cron with a command line parameter and check for that variable in $argv.

雪若未夕 2024-11-14 19:27:29

1)SPF记录是最重要的。使用来自域的电子邮件,因此[电子邮件受保护],其中whatever.com SPF 记录设置是否正确。

2) 限制电子邮件总是好的,尤其是刚开始时。您应该检查您的共享服务器策略,通常为 200-500/小时。计算一下还有多少秒。例如,300/小时就是每 12 秒 1 次。发送良好的电子邮件几周后,您应该可以发送更多的电子邮件。

3) 您可以将 cron 文件放在 webroot 之外,或者通过 .htaccess 或其他方法限制访问。

1) The SPF record is the most important thing. Use the email from the domain so [email protected], where whatever.com has the SPF records set correctly.

2) It's always good to throttle email, especially when first starting out. You should check your shared servers policies, which are normally 200-500/hour. Calculate how many seconds that comes to. For example 300/hour is 1 every 12 seconds. After a few weeks of sending good emails, you should be ok to send larger amounts.

3) You can have the cron file outside the webroot or limit access via .htaccess or another method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文