简体   繁体   English

如何从数据库中自动触发电子邮件发送

[英]how to trigger email sending automatically from the database

Hello I'm battling with some problem here. 你好,我在这里遇到一些问题。 I have a message in my mysql database that I want to send based on the scheduled date is there any way to do this : that when is the exact date the server should send the maill by itself, OR is there anyway to make a page load itself everyday without my access to send the email(if no one visit the site). 我在我的mysql数据库中有一条消息,我希望根据预定日期发送有任何方法可以执行此操作:何时是服务器应该自己发送邮件的确切日期,或者无论如何都要进行页面加载我每天都无法发送电子邮件(如果没有人访问该网站)。 Thanks. 谢谢。 I'm using php and mysql db. 我正在使用php和mysql db。 I have scheduleddate column in my database 我在我的数据库中安排了列

You could use a cron job to call a script which checks the database at a set interval (perhaps once per hour) and then have that script pull out the relevant messages and sends them. 您可以使用cron作业调用脚本,该脚本以设定的间隔(可能每小时一次)检查数据库,然后让该脚本提取相关消息并发送它们。

The cron for that would look something like: 这个cron看起来像这样:

0 * * * * /path/to/email/script.php

This script could then query the database and send those messages which need sending. 然后,此脚本可以查询数据库并发送需要发送的消息。 Of course you could set this to run every single minute ( * * * * * ), but I'd be very wary of having any script run every single minute! 当然你可以将它设置为每分钟运行一次( * * * * * ),但我会非常警惕每一分钟运行任何脚本!

This isn't really ideal thought, and instead you should look at using some form of queue management. 这不是一个非常理想的想法,相反,您应该考虑使用某种形式的队列管理。 If you take a look at queues.io you can find lots of info on various popular queue projects projects. 如果你看看queues.io,你可以找到很多关于各种流行的队列项目项目的信息。

I also found this post on setting up background jobs using resque very helpful! 我还发现这篇关于使用resque设置后台作业的帖子非常有帮助!

Of course how you integrate such a system is very much dependent on your codebase and your environment. 当然,如何集成这样的系统在很大程度上取决于您的代码库和您的环境。

Some frameworks have this kinda functionality integrated very well, I have worked with Laravel 4's queue component quite a lot and it really does make it very easy to work with.. 有些框架将这种功能集成得非常好,我已经与Laravel 4的队列组件进行了大量合作,它确实使得它非常容易使用。

In reality many people just end up using a cron/script combo to do the job, but it's important to know there are other (better) ways to achieve this. 实际上,许多人最终只使用cron / script组合来完成这项工作,但重要的是要知道还有其他(更好)的方法来实现这一目标。

The best way to do this without killing your CPU utilization is by scheduling it via a scheduled cron task on your server. 在不中断CPU利用率的情况下执行此操作的最佳方法是通过服务器上的计划cron任务进行计划。 Write your code to send the email, then add that page to a daily cron in your server's Admin Control Panel. 编写代码以发送电子邮件,然后将该页面添加到服务器管理控制面板中的每日cron。

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

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