简体   繁体   中英

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. How to do it in php? Can it be done with cron jobs? If yes, then what are the disadvantages of using cron?

I would tackle this using a cron job.

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? 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.

can it be done with cron jobs?

Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time". 1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. 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.

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. The quote from the forum topic below instructs how to run cron every minute.

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 * * * * /command/to/execute

And the other stars are from left to right

minute hour dayofmonth month dayofweek*

*0=sunday

Disadvantages of cron?

if yes, then what are the disadvantages of using cron

When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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