如何使用 PHP mysql 通过 cron 作业发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3368307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send emails via cron job usng PHP mysql
提问by JDesigns
i managed to send multiple emails (check here).i am stuck with sending automated emails via cron.
我设法发送了多封电子邮件(在这里检查)。我坚持通过 cron 发送自动电子邮件。
This is what i need - while the admin send emails, i store the message, emails, event date in the database. now i am trying to set a cron job to send emails to all these ids from the table with the message i have as a reminder. i am not familiar with cron job scripting, can anyone help in guiding me the right way to write script that i can place in cron tab. I am planning to send two mails - one day exactly before the event and on the day of event.thanks
这就是我需要的 - 当管理员发送电子邮件时,我将消息、电子邮件、事件日期存储在数据库中。现在我正在尝试设置一个 cron 作业,将电子邮件发送到表中的所有这些 ID,并带有我作为提醒的消息。我不熟悉 cron 作业脚本,任何人都可以帮助指导我编写可以放在 cron 选项卡中的脚本的正确方法。我计划发送两封邮件 - 活动前一天和活动当天。谢谢
采纳答案by Nathan Loding
Just write a normal PHP script -- make one that will work if it's launched directly from the browser. Then schedule that very same PHP file to run in cron, using this as a guide:
只需编写一个普通的 PHP 脚本——如果它直接从浏览器启动,那么编写一个可以工作的脚本。然后安排相同的 PHP 文件在 cron 中运行,以此为指导:
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
Basically, using the values at the beginning, specify the schedule (minute, hour, day of week, day of month, etc.). Then set the user it runs as, which is likely to be "apache" or whatever your web server daemon is running under. Then set the "command" that cron is running to be php php_email_script.php(where "php_email_script.php" is the name of your PHP file.
基本上,使用开始时的值,指定时间表(分钟、小时、星期几、月份中的某天等)。然后设置它运行的用户,这可能是“apache”或您的 Web 服务器守护程序正在运行的任何用户。然后设置 cron 运行的“命令” php php_email_script.php(其中“php_email_script.php”是您的 PHP 文件的名称。
回答by mvds
30 minutes and still no answer, here's a few open doors:
30 分钟仍然没有答案,这里有几扇敞开的门:
- cron reads it's rules from system-wide
/etc/crontab, or from you personal crontab which you edit withcrontab -e - cron takes a format where you say on which minute / hour / day / month things should happen, use google or
man crontabfor the format - cron has the amazing side effect of mailing the output of the command to the user owning the crontab
- cron 从系统范围
/etc/crontab或您编辑的个人 crontab 中读取它的规则crontab -e - cron 采用一种格式,您可以在其中说明事情应该在哪分钟/小时/天/月发生,使用 google 或
man crontab格式 - cron 具有将命令输出邮寄给拥有 crontab 的用户的惊人副作用
Now you are stating that you're using php. The easiest way to get some php running from cron, is to issue a wget -O - -q http://yoursite.com/yourprocessingscript.php?verysecret=123123and have an appropriate processing script on yoursite.com. (you may want to let that script check $_SERVER['REMOTE_ADDR'])
现在您声明您正在使用 php。从 cron 运行一些 php 的最简单方法是wget -O - -q http://yoursite.com/yourprocessingscript.php?verysecret=123123在 yoursite.com 上发出一个并有一个适当的处理脚本。(您可能想让该脚本检查 $_SERVER['REMOTE_ADDR'])
So in short, if you just put the right magic in /etc/crontab, like
简而言之,如果你只是在 /etc/crontab 中添加了正确的魔法,比如
0 0 * * * jay wget -q -O - "http://yoursite.com/processmidnight.php?secret=yes_very"
and have your script produce some sensible output, you will get a mail delivered to local user jay, which you may want to forward.
并让您的脚本产生一些合理的输出,您将收到一封邮件发送给本地用户 jay,您可能希望转发该邮件。

