page splitter

Hollen-B Web and Graphic Design Portfolio

Tutorials

Automated php emailer script using cronjobs.

In This tutorial I will show you a simple way to send out a Happy One Year Anniversary email.

| More
Share with friends and family

A majority of my projects require lots of email notifications and I don't have time to look at my data base everyday and see who needs to be emailed next. Thanks to Cron Jobs this task can easily be accomplished in your sleep.

Lets say im running a forum and I want to thank all my subscribers for being loyal readers, but I only want to send them this email each year. I first want to create my Cron Job, im going to go with the Standard setup. So log into your control panel and locate Cron Jobs. You then might be prompted to select whether you want to go with Standard or Advanced (Unix Style), click Standard. You now should be in the Cron Manager.

Next enter your email into the cron output field.

Then in the command line you want to add
/usr/local/bin/php -q /home/user/public_html/cron/mailer.php

If you do not know your public_html path you can add <?php phpinfo(); ?> to your mailer.php file which will display all your php information. Find the SCRIPT_FILENAME row and copy the path from there

As you can see im going to put my php script in a folder called cron in my root.

Now we want to tell the cron job how often to run this script. Since we only want to run this once a day we want to set the minutes to 0, hour to 1 am.

When you are done everything should look like this.
cron job

Save and then lets move on to the php section of this tutorial.

In your root directory you want to create a folder called cron, then create a new file and call it mailer.php In your mailer.php file lets create a function that sends the email.

sendEmail Function.

  1.  
  2. // SEND EMAIL
  3. function sendEmail($mail_to) {
  4. $from = "hollen949b@gmail.com";
  5. $message = "Hello! from your website";
  6. $headers = 'From: '.$from."\r\n" .
  7. 'Reply-To:'.$_POST['email']."\r\n" .
  8. "Content-Type: text/html; charset=iso-8859-1\n".
  9. 'X-Mailer: PHP/' . phpversion();
  10.  
  11. mail($mail_to, "Message From Your WebSite", $message, $headers);
  12. }
  13.  

Now we want to create another function that checks the database for dates_joined that are one year old. Then if the date is one year old run our sendEmail function to send out our anniversary email.

Here is a basic table structure of what my database table looks like. Since im writing this tutorial on 2009-10-21 im going to set my date_joined back one year for testing purposes.

 id  name  email  date_joined
 1  Brandon  brandonh@hollen-b.com  2008-10-21
 2  Dan  dan@hollen-b.com  2008-10-22
 3  Bob  bob@hollen-b.com  2008-10-23

Script that talks to our Data Base

This should be placed at the top of your mailer.php page.

  1.  
  2. // OPEN DATA BASE
  3. define("HOSTNAME","localhost");
  4. define("USERNAME","USERNAME HERE");
  5. define("PASSWORD","PASSWORD HERE");
  6. define("DATABASE","DATABASE HERE");
  7.  
  8. mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die("Connetion to database failed!");
  9. mysql_select_db(DATABASE);
  10.  

joinDateFilter function

  1. // WHO HAS BEEN A MEMBER FOR ONE YEAR
  2. function joinDateFilter(){
  3. $query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
  4. $mail_to = "";
  5. while ($row = mysql_fetch_array($query)){
  6. echo $row['name']." - ".$row['email']."\n";
  7. $mail_to = $row['email'].", ";
  8. }
  9. if (!empty($mail_to)){
  10. sendEmail($mail_to);
  11. }
  12. }

If everything is set up correctly our Cron Job should run once a day and send out our one year anniversary emails.

Please report any errors so I can fix them. For full script check out my google docs in these links below.

Links:
Create Database Script
mailer.php script

| More
Share with friends and family

Comments

Add Comment