Sending email with cron job? - php

hi i am working on send email every day 10am so i search lot of sites so i find using crone then it's possible to send email actually iam new to using crone so how can i install crone in my server please provide me some samples on sending email using php with crone.
Thanks for advance.

cron is probably already installed on your server. you will access it with:
crontab -e
To make it email with php you will probably want to use something like this: http://pear.php.net/package/Net_SMTP/

Cron is the inbuilt functionality provided by linux. it is a task scheduler. you need to set the cron to execute a php script every day at 10 am. this php script will contain the code for sending emails.
To see already set cron jobs use crontab -l to set new cron jobs use crontab -e. Your cron job looks as follows
1 2 3 4 5 /path/to/command arg1 arg2 OR 1 2 3 4 5 /root/backup.sh
Where,
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command - Script or command name to schedule
Check this link for more details How to Cron jobs
Also you may need to set the path to php executable in your php script, something like
#!/path/to/php/executable
as cron is Linux command it need to know which program to use to execute the given script. the above line goes as a first line before opening php tags.

Setup the cronjob:
$>crontab -e # this command line command sets up the cronjob and opens the vi editor
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
Vi editor opens: add your script there:
Example:
25 11 * * * .csh 2014_09_16 --> This will execute the csh script daily at 11:25am and send an email accordingly
Esc- Shift+zz to exit the editor by saving the changes
$>crontab -l # this cmd checks if the conjob is set

You can easily execute a PHP file using cronjobs, and let it take care of your email sending. I've written a tutorial here: http://rapliandras.hu/blog/how-to-execute-php-scripts-as-cronjobs/
If you are unsure of how exactly an email is sent using PHP, check out its mail function's documentation here: http://php.net/manual/en/function.mail.php

Related

How to create a quartz (cron) in Codeigniter 2 (php)?

I ask you how to create a quartz (cron) in with the framework of Codeigniter 2 (php), I need to send an email at 2:00 pm every day.
I await your kind support.
Regards,
To do so, you need to add a make a php file that will send email. Suppose your file name is send_email.php. Now test that file is working or not by executing php send_email.php in the directory of your file. If this file sends mail successfully, then you know that this command will send email every time you execute it.
Now, you need to add this command in your Operating System's cronjob list. If you are in Linux, then open your terminal and execute the command crontab -e. Then select edit option to edit your cronjob. Add following line at the bottom:
0 14 * * * php full_path_of_the_file/send_mail.php
The first 0 means zero minute. Second number 14 means 2PM then rest three starts are for all week, months and year. then our old php command to execute php file. Then path to our created php file. Here, only the file won't work. You need to provide full path of your php file that send mail.
Save the file and exit. Now, your operating system will send email every day at 2:00PM.

sending email periodically through php script

I am working on an automatic monitoring system. I have made a .NET application which have MySQL database. For this I developed a normal ADMIN Panel where admin can log in and get necessary reports coming from various queries fired on the database. There is also a "summary Report" in the panel which is just the rough weekly summary. Now What I want is, I want this report (all text) to get sent automatically to some email "xxxxx#xxx.com" with a seven day period. I have used some PHP scripts previously to send email on submit button click. Like the one below.
<?php
if(isset($_POST['isPost']))
{
$header="From:".$_POST['customer_mail']."\r\nName:".$_POST['name']."\r\nCity:".$_PO ST['city'];
$subject = $_POST['title'];
$message="From:$_POST[customer_mail]\r\nName:$_POST[name]\r\nPhone:$_POST[city]\r\n \r\n\r\n\r\n".$_POST['details'];
$to = 'xxxxxxxxx#xxx.com';
$send_contact=mail($to,$subject,$message,$header);
if($send_contact)
{
echo "<h6 style='text-align:center;color:green'>Sent Successfully</h6>";
}
else
{
echo "<h6 style='color:red'>Error sending e-mail'</h6>";
}
}
?>
this is normal mail sending script which works fine. But for the above purpose I want, Can anyone help me to set this action periodically and automatically or point me in the right direction on how to do this. I have googled for such php script but no satisfied results.
~ Regards
You can do this with cronjobs.
A long running process which executes commands at given times / dates / intervals.
Depending on what system you are, there are different methods.
If you have the script on a webserver someone is running for you / Webhost service
Ask the system administrator to run the script with a cronjob. Or search for any help documentation if you can setup this yourself in any admin-panel. Ask your webhoster /system admin for more information.
If you have the script on your own local machine:
UNIX
Try reading about crontab on how to run the php script, or any script for that matter.
For example type crontab -e and add the line below in your crontab, the cronjob will run your script every hour:
00 * * * * /usr/local/bin/php /home/Low-pointer/myscript.php
Here is some more information if you want to play with the intervals
Windows
If you use Windows you can use scheduled tasks to run the php command.
For example add the following command to the scheduler: C:\wamp\bin\php\php.exe -f C:\wamp\www\my_script.php
You can also use web cron services (Google it) these are online services which run a page (for example on your webserver) on designated times. For free or paid.
You can use Cpanel to send schedule emails through the cronjob(if you are using it).
Once you open cpanel theere would be crontab system.
Add your current code to a file(xx.php) and add this command to crontab in cpanel ,
/usr/bin/php -q /home/public_html/xx.php
like everyone have already said you must use cronjob to make your task.
I assume you use a Linux OS as your production environment.
So you need:
1) A php endpoint ( eg. www.mywebsite.com/emailsend.php ) OR a CLI php script that when called send the email.
2) The correct crontab rule
Here below an example of a simple shell script ( mailsend.sh ) that call an endpoint using CURL and save an html file with an eventual response given by the webserver
#!/bin/bash
curl http://www.mywebsite.com/emailsend.php -o /path/to/my/mailsendreport/"$(date '+%Y-%m-%d-%H-%M-%S')".html
To add a scheduled task to cron
crontab -e
then add a rule like below
50 23 * * 6 sh /path/to/mailsend.sh
What "50 23 * * 6" means? It means that every sixth day of the week, in the 23th hour, in the minute 50 your sh script will be called, and then your web app and the email is sent.
Once I wrote a small doc about this you can see it here
Cheers
You're looking for "cron job".
Edit: Or since it sounds like you might be on Windows, "Scheduled Tasks"

How to run a script under CRON that outputs a CSV file and is then emailed

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

run php script automatically

i have a php script that should be run automatically every day.
as the php script is run on a request,how can i do it?
is there any way else using cronjob task?
Two options:
Use crontab demon
Hire a worker and make him open script in a browser every 24 hours
The choice is yours :)
To use crontab, type crontab -e in console, the text file opens. Add a line at the end:
0 0 * * * /usr/bin/php /var/www/mysite/httpdocs/daily_stats.php
Where:
0 0 * * * - run every day at 00:00
/usr/bin/php -path to your PHP (can be determined by which php command)
/var/www/mysite/httpdocs/daily_stats.php - path to your PHP script
if which php outputs nothing, install PHP cli by running:
sudo aptitude install php5-cli
Good luck!
use cron job option who start automatically and give result before 24 hours
Use the cron job, this is the best solution.
otherwise, you can run an infinite loop inside php and sleep 24 hours.
horrible solution though.
If cron isn't available in some sort of way you could use Google app engine's cron for this. Because cron is the way to go.
If cron is not available you could execute a php script in CLI which will run all the time.
In the script you can make a infinite while loop.
In the while loop, check a file on disk or a db record (you can controll this file or db record from an external script, telling the looping script what to do (CLI execute another script at a given hour) and when to exit)
If you use a database,don't forget to initialize and close the db connection each time the loop runs.
I'd sleep the loop every 1 min or so.. you could use this instead of linux cron for many more things.

How can I set up cron job in my site using PHP?

I want to mail after 6 hours automatically to my user who hasn't fully completed form on my website.
Help Me
Use crontab -e to edit the cron table for your account.
In the crontab, put an entry something like...
0,10,20,30,40,50 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
or the equivalent
*/10 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
...which will run the cron handler php file every 10 minutes using wget (there are other options as well, and you may need to edit the command appropriately). (Note: you don't want to just run it every 6 hours, because then if someone happened to fill out the form right after it ran, it wouldn't have been 6 hours since they filled it out next time it runs, so you'd end up with 10-11 hour gaps.)
Then in your PHP file, find users who BOTH (a) haven't fully completed the form for at least 6 hours and (b) haven't been emailed yet. Send them an email, and mark them as having been emailed.
You will need to create the php script that does the checking and mailing, and then set the cron job like the following
/path/to/php -q /home/username/public_html/mycheckingscript.php
Obviously you will need to adjust the first path to point to your php binary, and the second path to point to the full location of your checking & mailing script.
I don't think you want to set the cron up using php. Instead write a php script and then have cron execute that script every hour or so. This would be something that is going to be dependent on your operating system.
For linux, here is the manpage for using crontab.
There is no way you can change/add a schedule on the cron job on the fly. according to my experience. because until now i did not find..

Categories