3

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

Low-Pointer
  • 163
  • 2
  • 13
  • That's what the report button is for. Please stop crying as a seperate comment on each answer. Downvote, report, move on. – Jacob Goulden Apr 30 '14 at 18:52

4 Answers4

4

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.

Community
  • 1
  • 1
Timmetje
  • 7,641
  • 18
  • 36
  • Fred thanks for the edit, but I'm really curious (English not my native language. When Google is used as a verb(which it has become), is it still with a capital letter? – Timmetje Apr 30 '14 at 18:56
  • You're welcome Tim. I tend to think so, since it is a company name (*that has been transformed*) and not an actual noun/word in the dictionary (*but that could soon change*) lol. However, that is a very good question, yet I tend to lean more towards capitalizing it; it's debatable though. – Funk Forty Niner Apr 30 '14 at 22:21
  • That might be the difference, in my country they actually added it to the dictionary 2 years ago or something. – Timmetje May 01 '14 at 07:42
  • The word "Google" is in fact inside my Robert & Collins Senior Edition dictionary (*yeah, I love a real book as a reference*) *wink*, and it is capitalized. – Funk Forty Niner May 01 '14 at 17:34
3

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
Wit Wikky
  • 1,542
  • 1
  • 14
  • 28
1

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

Sergio Rinaudo
  • 2,303
  • 15
  • 20
0

You're looking for "cron job".

Edit: Or since it sounds like you might be on Windows, "Scheduled Tasks"

Jessica
  • 7,075
  • 28
  • 39
  • ya, but how to implement such scheduled tasks in a web app? – Low-Pointer Apr 30 '14 at 18:33
  • @Low-Pointer: Technically it's not part of the web app. It's a stand-alone application. `cron` would execute it as a command-line executable: http://www.php.net/manual/en/features.commandline.usage.php – David Apr 30 '14 at 18:35
  • So look for basic tutorials on using those tools. Are you on Windows or linux? You need to put in a little more effort, and swearing about it isn't going to get you any points. – Jessica Apr 30 '14 at 18:39
  • So give the OP an actual answer instead of what should have been a comment. You have not provided an actual answer, *per se.* – Funk Forty Niner Apr 30 '14 at 18:41
  • 1
    Neither have you. That's like the pot calling the kettle black; IMO. – Funk Forty Niner Apr 30 '14 at 18:44
  • Fortunately for me, I'm not the one who has to get this problem solved. The impetus is not on me to do the most work. – Jessica Apr 30 '14 at 18:44
  • As it stands, your answer qualifies more as a comment than an "answer". You've been around SO long enough to know the difference. – Funk Forty Niner Apr 30 '14 at 18:46