简体   繁体   English

如何在预定时间用PHP发送邮件?

[英]How to send mail in php at scheduled time?

I want to build an application that enables users to schedule emails to send any time. 我想构建一个应用程序,使用户可以随时安排电子邮件发送。 simply, write email message and schedule it so that the server sends it at time specified. 简单地说,写电子邮件并安排它,以便服务器在指定的时间发送它。 I am using zend framework. 我正在使用zend框架。 How to do it in php? 如何在PHP中做到这一点? Can it be done with cron jobs? 可以用cron工作吗? If yes, then what are the disadvantages of using cron? 如果是,那么使用cron有哪些缺点?

I would tackle this using a cron job. 我会用cron工作解决这个问题。

Simply create a script that checks for messages to send at a certain time. 只需创建一个脚本,检查在特定时间发送的消息。 The user schedules for say 1PM (using a database of course), the script runs every 5 min, or so, and it checks (the db), are there any messages to go out for the current time? 用户安排说下午1点(当然使用数据库),脚本每5分钟左右运行一次,然后检查(数据库),是否有当前时间的消息? If so, it sends out the emails, else it sleeps. 如果是这样,它会发送电子邮件,否则就会睡觉。

Clean and simple way of handling it. 干净简单的处理方式。

Disadvantages? 缺点是什么?

I can't see any, this is what a cron is made for, running tasks at specific times. 我看不到任何东西,这就是cron的用途,在特定时间运行任务。

can it be done with cron jobs? 可以用cron工作吗?

Cron is a time-based job scheduler in Unix-like computer operating systems. Cron是类Unix计算机操作系统中基于时间的作业调度程序。 The name cron comes from the word "chronos", Greek for "time". cron这个名字来自“chronos”这个词,希腊语来自“时间”。 1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. 1 Cron使用户能够安排作业(命令或shell脚本)在特定时间或日期定期运行。 It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email. 它通常用于自动化系统维护或管理,但其通用性意味着它可用于其他目的,例如连接到Internet和下载电子邮件。

http://ubuntuforums.org/showthread.php?t=586478 http://ubuntuforums.org/showthread.php?t=586478

I would run a cron job every minute and check if there are any mails ready to be scheduled. 我会每分钟运行一个cron作业并检查是否有任何邮件准备安排。 The quote from the forum topic below instructs how to run cron every minute. 以下论坛主题的引用指示如何每分钟运行cron。

crontab -e

then set a tab like 然后设置一个标签

* * * * * /command

The first star is the minute section, so having a star there will execute every minute 第一颗星是分钟部分,因此每星期都会有一颗星星

In case it makes it more clear if you wanted every 5 mins then it would be 如果你想要每5分钟更清楚一次,那就更好了

*/5 * * * * /command/to/execute

And the other stars are from left to right 其他的星星是从左到右

minute hour dayofmonth month dayofweek* 每小时一个月的一个小时dayofweek *

*0=sunday * 0 =周日

Disadvantages of cron? cron的缺点?

if yes, then what are the disadvantages of using cron 如果是,那么使用cron有什么缺点

When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). 当你做很多cronjobs时,你将不得不产生很多过程(产生昂贵的产卵过程的成本)。 In that case it would be better to have background process(es) running continually and fetch messages from message queue. 在这种情况下,最好让后台进程连续运行并从消息队列中获取消息。 But when you want to run a cronjob only ever minute than I assume this will not be a big case. 但是当你想要运行一个cronjob时,我认为这不会是一个大案例。

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

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