Reputation: 9
I have this code and I want to add my code with cron scheduler so tell me the process to add my code with cron. I want to generate automatic emails with the help of corn or this code. I am using this for a website to generate automatically mails in 15 days.
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source.
$msg = 'Hello World';
$subj = 'test mail message';
$to = '[email protected]';
$from = '[email protected]';
$name = 'My Name';
echo smtpmailer($to,$from, $name ,$subj, $msg);
function smtpmailer($to, $from, $from_name = 'Example.com', $subject, $body, $is_gmail = true)
{
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if($is_gmail)
{
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = '[email protected]';
$mail->Password = '***********';
}
else
{
$mail->Host = 'smtp.mail.google.com';
$mail->Username = '[email protected]';
$mail->Password = '**********';
}
$mail->IsHTML(true);
$mail->From="[email protected]";
$mail->FromName="[email protected]";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('[email protected]', 'CC: to phpgang.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send())
{
$error = 'Mail error: '.$mail->ErrorInfo;
return true;
}
else
{
$error = 'Message sent!';
return false;
}
}
?>
Upvotes: 0
Views: 887
Reputation: 347
You will need to configure the cron on the server. This usually requires SSH or similar access, and is usually done from the command line using a program such as Putty. However, depending on your hosting setup, you may be able to update it using a CPanel cron scheduler or other user friendly tools. I'd recommend contacting your hosting provider if you are not familiar with managing cron schedulers on your own.
Instructions on setting up scripts to run on cron on Linux are covered here:
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Quote: To edit your crontab file, type the following command at the UNIX / Linux shell prompt: $ crontab -e
You can then add entries to the cron file for individual scripts:
1 2 3 4 5 /root/path/to/script.php
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
As stated in the comment below, you can also run the script "every minute" or "every hour" by replacing the minute or hour numbers with asterisks:
Run script every minute of every hour of every day (script runs every 60 seconds):
* * * * * /root/path/to/script.php
Run script every hour at the 15th minute of the hour:
15 * * * * /root/path/to/script.php
Upvotes: 1
Reputation: 993
I know two ways you can put PHP scripts in a scheduler so that they are executed periodically, in your case shooting emails
You can configure the scripts in a windows scheduler =>using php script in task scheduler on window 7
If you are using some hosting that has CPanel (most of the hosting do) you can schedule the jobs in the CPanel as well so that your production server shoots mails periodically. Here are the links that will help you in CPanel. http://www.siteground.com/tutorials/cpanel/cron_jobs.htm http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/CronJobs
Happy Scheduling ;)
Upvotes: 0