Low-Pointer
Low-Pointer

Reputation: 163

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 "[email protected]" 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 = '[email protected]';

$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

Upvotes: 3

Views: 5509

Answers (4)

Timmetje
Timmetje

Reputation: 7694

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.

Upvotes: 4

Sergio Rinaudo
Sergio Rinaudo

Reputation: 2363

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

Upvotes: 1

Wit Wikky
Wit Wikky

Reputation: 1542

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

Upvotes: 3

Jessica
Jessica

Reputation: 7005

You're looking for "cron job".

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

Upvotes: 0

Related Questions