How to send emails via cron job usng PHP mysql - php

i managed to send multiple emails (check here).i am stuck with sending automated emails via 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

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

30 minutes and still no answer, here's a few open doors:
cron reads it's rules from system-wide /etc/crontab, or from you personal crontab which you edit with crontab -e
cron takes a format where you say on which minute / hour / day / month things should happen, use google or man crontab for the format
cron has the amazing side effect of mailing the output of the command to the user owning the 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=123123 and have an appropriate processing script on yoursite.com. (you may want to let that script check $_SERVER['REMOTE_ADDR'])
So in short, if you just put the right magic in /etc/crontab, like
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.

Related

PHP- Sending a daily email

I am making a website that sends people who sign up for it an email every day in the morning. The problem I have run into is that I don't know how to send an email at the same time everyday in php. I do know how to send an email, all I need to know how to send one daily.
You can setup a CronJob for that. Only make the PHP that sends the email you want and prepare a CronJob in your server that executes every day (you can specify the time of the day).
I would use Tectite Formmail
If you have, for example, GoDaddy hosting, you can follow this instructions: CronJob GoDaddy
Yes, you need to run cron, it will already be installed and configured on your server.
To build-up your list, set up a form that subscribes people to your list, adding them to a database - don't forget to use double-opt-in.
Then write a PHP script that generates a message for everyone on your list and sends it to them - there is a code example that does exactly this bundled with PHPMailer. To let cron know how to run the script, make this the first line of the file (before <?php):
#!/usr/bin/env php
Finally, get cron to run it - this is trivially simple - just symlink it into /etc/cron.daily/ and it will get run when cron runs each day. Look in /etc/crontab to see when this is, and alter it if you like. For example:
ln -s /path/to/my/script /etc/cron.daily/myscript

Automatic Mail Sending on specific Dates in PHP

I am having some mail ids in my database. I have to send mail to those mail ids automatically on some specific dates that i have mentioned in my database.How to do that in php.
It depends on your server type. If you use linux you can use cronjobs to program the execution of a specific php file at a certain time.
If you are hosted, your host may offer a cronjob menu in their cpanel, else you would have to get a better hosting plan that offer that. Or at least access to crontab file where you program the different Cronjobs.
Executing a PHP script with a CRON Job
You need to write cron jobs for sending the automatic emails on a particular date. Without this it is not possible.
Syntax
Here is a simple cron job:
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script.
Timing Syntax
This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.
It consists of five parts:
minute
hour
day of month
month
day of week
Or
You can set the cron in you cpanel.
Here are a few general steps of the logic that you can follow:
The PHP script should:
extract the email addresses and the specific dates from your database
into arrays.
check whether
today's date is
in the array containing the dates. Be sure to format the date appropriately!
loop through the email addresses (preferably in a foreach() loop),
if the above check returns 'true'.
within this loop, use PHP's mail() function to send your email.
Your next step would be to set up a scheduled task that runs this script daily, usually by means of a cron job. This step depends on the server you've got.
NOTE: These steps illustrate the most basic way to do what you require. There may be other, more complex ways that would be faster, cleaner and less intensive on your server.
As answered by Albert James it depends on the system type on which your application is running. If it is Linux then you can get the php script which send mail executed by cron jobs and if you are using windows machine then you need to execute that php script with schedule task : How to run a PHP file in a scheduled task (Windows Task Scheduler)
Also here is the option if you don't want to use schedule task\cron jobs (Though I haven't used that): How to send schedule emails using php without cron job
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
example

Send mails with PHP regularly at a specific time

I have three mysql tables; Users, Tasks and Task_Users, where each user can create their tasks through a web application I'm developing in PHP.
The thing is, I want my application to send an email to the users one day before the task occurs. The first thing that came to my mind was to do a MySQL job or trigger to send the email but I dont have an idea of how to do id,
What would be the best way make this happen or how could I call a job through PHP and execute it every day and verify that there is just one day left before the taks ocurrs and then if that is true send the email?
Here's my table definition. Thanks in advance.
Users
----------
UserId,Names,email,pass
Tasks
------------
TaskId,TaskName,Descripcion,TaskDateTime
Tasks_Users
------------
TaskId,UserID
You can write a CRON Job for this.
What's CRON Job ?
Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.
What do you have to do ?
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:
How to run a CRON Job after it's written ?
http://www.inmotionhosting.com/support/edu/cpanel/301-run-cron-job
If you have any issues let me know.
I'd make a cron job that regularly runs a script (say every 5min?) which checks for tasks that need emails sent. Then the script script sends the necessary emails.
*/5 * * * * php /path/to/script.php
Would call script.php, which checks and sends emails, every 5minutes.
Some info on cron is here:
http://www.pantz.org/software/cron/croninfo.html

Scheduling tasks in PHP without cron

Is it possible to schedule a task with PHP? like, I want to choose a date with the jQuery datepicker, and submit it. When that date equals today's date, an email will be sent for example (or any other PHP script). The date will be different depending on the user, also, every user can have a number of scheduled tasks to run.
EDIT
The OS is Linux, and the website is hosted on godaddy.com
Also, it's the end users who will choose when to run these tasks, so they can use command lines for that.
EDIT
Any Ruby on Rails or Django solutions are welcomed as well.
If you're using a Unix server, try at.
http://www.manpagez.com/man/1/at/
It works in a similar way to cron, in that it's totally separate from PHP, and you need to shell_exec() an external command. However, it has a simple command-line interface. Like cron jobs, at jobs survive reboots.
If you use at you'll have to write a separate script to perform the task you want scheduled.
Example:
shell_exec("echo 'php script.php' | at -t 201208011234.56");
Will run script.php on August 1st, 2012 at 12:34:56.
You need some process in the server side to run the task at the scheduled time.
A popular approach is to store the task information in some database and have a cron job checking the task queue from time to time - it is very reliable and safe.
It is impossible to give a better answer without more information about the environment where it will be deployed. For example:
target OS
hosting type (dedicated, shared, cloud)
do you have administrator privileges?
is it exposed to the internet?
Most likely you will not be able to do that in a shared hosting environment, anyway, but I must say that having a website calling shell_exec is not very wise from a security standpoint, so I would avoid that if the site is exposed to the Internet.
A good hosting provider should have some kind of background task scheduler available, even if it is not crontab. If your hosting provider hasn't, trying to pull some stunt to fill the gap probably is a bad idea.
[update]
As I said in the edited part of the original post, I'm hosting it on godaddy.com, and I can't let the clients run these command lines etc. Is there a solution with other languages too?
I'm not hosting at godaddy anymore but they used to have a "Cron Manager" at their control panel. I think the correct goDaddy cron setup is
/usr/bin/wget -O - -q "http://yoursite.com/cron.php" > /dev/null 2>&1
at the Command Option in the Cron Manager. This "cron.php" will check the task queue to see if there are any email to send (the task queue being a simple table in your database where you will record any task scheduled by your customers).
You can do this without cron by simply adding a check into the main index page, or a standard include file - but that's only for when you really can't get access to cron (e.g. shared hosting). In this check you'd grab the list of outstanding actions and execute them, possibly by using
if (time_since_last_run > 5*60) // minutes
shell_exec("/usr/bin/php /var/www/check-tasks.php &");
The best solution is to use cron to launch a PHP script that will check for scheduled items within your system and execute them. You can either do this via wget or directly via calling PHP.
One shared hosting site that I have is externally scheduled from another of my servers via cron and wget http://othersite.example.com.
example crontab entry:
0 * * * * wget -O - -q -t 1 http://othersite.example.com/cron.php

run a php automatically after every hour

I am using a shared windows hosting in which sending 120 mails/hour is allowed though php code.
i have a php page to send emails more than 200 mails at a time.But i want to run the page, after every hour(scheduled task).
I will split the emails(in 100s) and want send automatically after every hour.
How is this possible in php to run a php page after every hour ?
Thanks
Use PHP Cron Jobs with cPanel
cPanel Simple Cron
you can get started with the simple cron tool built into cPanel. The url for it is:
https://www.yoursite.com/page.php
The command to run:
/usr/local/bin/php -f /home/(username)/public_html/page.php
Next you'll want to select an option from all the select boxes. Remember to select an option in each box. If you want something to run every hour, select Minute: 60; Hour:Every; Day: Every; Month: Every; Weekday: Every;
Click save and you are all set! You'll get an email every time the cron job runs, but if you don't want to get it - put :blackhole: into the output email field at the top.
Use a script on Google AppEngine to "ping" yours (Scheduled Tasks to be more precise).
It's free to use.
You can schedule a task in Windows - control panel->administrative tools is where you find task scheduler.

Categories