Posted on : 07-03-2009 | By : Japh
Ok, so there are two main topics being covered here:
- Sending emails with PHP
- Running PHP scripts with a cronjob
Sending emails with PHP
Step 1
First of all, we’re going to install the PEAR::Mail package. PHP’s built-in mail() function isn’t particularly secure, so using an actual SMTP email account seems nicer :)
If you don’t already have PEAR::Mail installed, you may need to request from your host that they install it. If you have SSH / shell access to your server, you can probably do it yourself by running the following command:
pear install mail |
Now you should be able to use this library with the following code in the top of your PHP script:
require_once('Mail.php'); |
This may seem odd, because you have no Mail.php file in your web root, but trust me it works. This is because PEAR is in PHP’s path variables, so if it doesn’t find the Mail.php file in your web root, it already knows to look there.
Step 2
The next part is actually writing some PHP that will send a message. You will need a few details of your mail server for this bit, such as SMTP server name, your username and your password. You will also need an email address to send to, and one to send from, of course!
<?php require_once "Mail.php"; // Inclusion of PEAR::Mail $from = "Joe Bloggs <j.bloggs@domain.com>"; $to = "John Citizen <citizenj@exampledomain.com>"; $subject = "Test message"; $message = 'Test message.'; $host = "smtp.domain.com"; $username = "jbloggs"; $password = "password"; $headers = array ( 'From' => $from, 'To' => $to, 'Subject' => $subject ); // Now we will create an SMTP object with the appropriate details $smtp = Mail::factory( 'smtp', array ( 'host' => $host, 'auth' => true, 'username' => $username, 'password' => $password ) ); // Here we actually send the email $mail = $smtp->send($to, $headers, $message); // And check for errors, or if the email was sent successfully if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?> |
I’ve put some comments in this script, just to make sure you know what’s going on, but basically that’s it!
Running PHP scripts with a cronjob
Step 3
Using cronjobs seems difficult, and some people are put off by the slightly archaic looking format… but they’re actually very simple. You put multiple entries (cronjobs) into the crontab, and then they run! Look at the following crontab entry:
0 * * * * /usr/local/bin/php -q /path/to/script/schedule.php |
What this says is, everytime the minute is 0 (i.e. on the hour), run that command. Look at this crontab entry:
*/5 2 * * * /usr/local/bin/php -q /path/to/script/schedule.php |
This one says, each time the minute is a multiple of 5 (i.e. every 5 minutes) and the hour is 2 (i.e. 2:00am), run that command.
So that’s basically it! With both those entries in the crontab, our emails will be sent every hour on the hour, and also every 5 minutes between 2:00am and 2:59am.
Hopefully this helps some people. If you have any questions, or if I made any mistakes, feel free to let me know in the comments :)
No related posts.





Awsome article Japh! I may well use this in the future
Great!
If I’ve missed anything you need, let me know.
This is awesome. I’ve been a slave to mail() for too long. Time to make a shift.
<3 cron
Been trying to figure out how to determine the path to the script i want to run. Is this a relative path? How can I determine this? I am using Plesk, PHP, MySQL.
Cheers!
Tim
Hello Tim, my apologies for the long delay replying! The path should probably be the full absolute path.
You could determine this by navigating to the directory containing the script and running the command “pwd” on the command line. This will tell you the full current path, then just add your script name to the end.
I hope that helps!
where i have to write cronjob ??
means in any PHP file ?
i want to send mail automatically on every month to my client with somedata from database mysql ..