sending email periodically through php script - php

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"

Related

PHP simple parser to run only once a day

I am using the simple_html_dom script to parse a value from a website.
My code:
<?php
include('simpleparser/simple_html_dom.php');
$html = file_get_html('http://www.example.com');
foreach($html->find('strong') as $e) // the tag that I am fetching
echo $e->innertext ;
?>
Now, I'd like to run this only once per day as the data, I am parsing updates only once every day.
I've read a couple of articles about the cron task, but can not get it to work. The examples seem to overcomplicate things and are not relevant to my case.
My hosting plan has the cron scheduler disabled and no shell access and I don't know how else to set it up.
To create cronjob from the command line use:
#write out current crontab
crontab -l > mycron
# echo new cron into cron file | runs everyday at 22h
echo "* 22 * * * php /full/path/to/script.php" >> mycron
#install new cron file
crontab mycron
rm mycron
To create a cronjob using Parallels Plesk Panel, take a look at this answer
UPDATE:
I have no shell access and cronjob disabled in my Plesk Panel.
Use a online cronjob https://www.setcronjob.com/
If there's really no way for you to setup a cron job on your hosting - you could also use some online service to trigger your script once in a while.
For example https://cron-job.org seems to do what you need
Attaching a sample of settings they provide
You can contact with your hosting server but temporary solution for this kind of problem is to use cronjob service there are lot of free cronjob service out there in web. you can try those service .
I used this kind of service while creating a DDoS bot .. :p ..
You can use these cronjob service but there are more ...
https://www.setcronjob.com/
https://www.easycron.com/
search in google with "Cron job service" you'll find thousands of service like this
Happy coding :)
I use windows task-schedule. To execute a .php script you can either insert it like this or create a .bat file and execute it through there.

run a php page once a day using NAS synology

have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.

Running PHP code using cron jobs

I have some executable PHP code that I'm trying to implement using cron jobs. The page sends a couple emails. It works just fine when I type in the address into my URL bar and load the page (the url is akin to www.mysite.com/mypage.php) but for some reason it doesn't work when I do it as part of a cron job. I have double checked the permissions and the file is executable, so that's not the issue. I am getting an email confirming that the cron job was completed, but the emails that are supposed to be sent by the program do not come through. Here is my code in crontabs:
SHELL= /bin/bash/
HOME = /
MAILTO = "mymail#gmail.com"
* * * * * /usr/bin/php /usr/share/nginx/html/mypage.php
Any idea why this might not be working?
EDIT ABOUT PERMISSIONS: Possibly relevant: I am editing cron jobs by typing 'sudo crontab -e'. I was able to successfully set up another cron job that just emails me text. However, I tried setting up another text-only-email cron job NOT in sudo (ie typing 'crontab -e') and that did not work. I didn't receive any emails. I also got an error when I typed 'crontab -e' about it not being able to read .nano_history and permission being denied but I was able to bypass that by pressing Enter
Check to see that wget is installed on your server. it should be. Then use wget to call your program from the cron tab. This way you do not have to concern yourself with calling php from the command line.
wget http://999.999.99.9/hr/stats/send_stats.php?task=first

How to fetch (poll) data periodically from an rss feed

I have a rss feed (xml) that is updated frequently. I need to send notifications to APNS if new update is avaliable on rss feed (xml). So far I know I can parse xml with php and send the result to APNS with my local Apache Server on Mac (MAMP).
But I do that by simply entering php xxx.php command on terminal window.The File xxx.php first parses the xml file and then sends the results to the APNS server.
My question is
How can I periodically run this php file on a server?
Do I need a Virtual Private Server?
If so what is the code or function for running a php file continuously or every 10 mins ?
To run a file periodically you have to manage cron jobs, I wanted to describe what you have to do but I found a good profound article, It's worthy to have a look on it.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
You can use cron on a linux/mac server to run periodically any command (for example: php xxx.php)
I) In a command prompt you can edit the crontab with: crontab -e
II). At the end you add a line for every script you want to run periodically. Each line must follow this pattern:
`minute` `hour` `day of the month` `month` `day of the week` `command`
For example if you want to run the script every hour you add this line:
* */1 * * * php xxx.php
III) Then exit with CTRL+X and save.
More examples on Wikipedia.
What you need is a cron job: See this Wikipedia entry.
Check to see if your host allows you to run cron jobs, and if not -- consider contacting them to see if they can set it up for you.

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.

Categories