如何在CRON下运行输出CSV文件然后通过电子邮件发送的脚本

I need to run a script that queries my DB and outputs the results as a CSV file. I have the script working to produce the CSV I now just need to figure out how to schedule this task for 2am and email the output CSV to a list of email addresses using PHP.

Any help or ideas would be appreciated.

you edit your crontab and simply schedule what you want to happen

e.g.

$ crontab -e

to edit your crontab. Add an entry to run your script

MAILTO=me@example.com
00 02 * * * cd /here && runMy2AMScript

And that's about it. your script does all the work, cron just calls it. For more information about crontab - use man crontab or man 5 crontab.

If your unclear how to schedule sending an email - that's 2 tasks scheduling (which is above) and a script that sends an email - which you can do any number of ways (such as using swiftmailer. Pick a technique and ask another specific question if you are stuck).

Any help or ideas would be appreciated.

More information would be appreciated. Where do you run into problems? Is it in the scheduling? If so, do a Google search for "Scheduled tasks" in case the webserver is running Windows, or "Cronjobs" if the webserver is running Linux, Unix or BSD.

E-mail a file is rather simple if you use a library; I would suggest looking into attaching files with Swiftmailer.

I don't have much idea about cron. But once I tried firing mail using cron. My php site folder is home/USER/www. So this is what I did:

1. created a txt file: cron.txt
2. placed cron required informtion : 29 16 * * * php /home/USER/www/test_cron.php
3. ( test_cron.php contains code for a dummy mail )
4. opened terminal and executed command : crontab cron.txt

and its been two month and I daily get an email on 4:29pm

Hope this information will help you.