In part 1 of this series, we discussed how to track email views using an image. In part 2, we setup the tables and functions necessary to record the emails. Before we start building the reporting and campaign tracking mechanisms for step 3, we will need to adjust the code so it will be more maintainable in the future. Based on the traffic and emails I’ve received regarding this script, we are going to develop this into an easy-to-use, quick-to-deploy solution that can adapt to a variety of existing content management systems and web frameworks but still work as a standalone system.
This is simply a cleanup step and the code below is the revised version of the functionality from step 2. Please update the constants in the Config class to use this script.
Requests to this new version also have an argument change and the URL should now look like this (where ID is the id for the user that will receive the email):
http://YOUR-SITE.com/signup_complete.php?userId=ID
View Code PHP
<?php // SCRIPT CONFIGURATION class Config { const EMAIL_ADDRESS = 'you@site.com'; // YOUR EMAIL ADDRESS const SITE_DOMAIN = 'site.com'; // YOUR DOMAIN const IMG_PATH = '/images/email/'; // PATH TO PNG IMAGE AND TRACK.PHP const DB_HOST = 'localhost'; // YOUR DB HOST const DB_USERNAME = 'username'; // YOUR DB USERNAME const DB_PASSWORD = 'password'; // YOUR DB PASSWORD const DB_SCHEMA = 'database'; // YOUR DATABASE/SCHEMA NAME } // PRESISTENT DB CONNECT mysql_pconnect(Config::DB_HOST, Config::DB_USERNAME, Config::DB_PASSWORD); mysql_select_db(Config::DB_SCHEMA); // USER MODEL class User { public $id; public $emailAddress; public $status = 0; // WE'LL USE THIS AS AN ACTIVE/CAN-BE-EMAILED FLAG // STATIC METHODS public static function addUser($emailAddress) { $result = mysql_query('INSERT IGNORE INTO `user` SET `email_address` = "' . mysql_escape_string($emailAddress) . '";'); } // PUBLIC METHODS public function __construct($userId){ $this->id = $userId; if($this->fetchDetails()){ $this->status = 1; } else { $this->status = 0; } } // PRIVATE METHODS private function fetchDetails(){ $result = mysql_query('SELECT * FROM `user` WHERE `user_id` = ' . $this->id . ';'); if(mysql_num_rows($result) == 1){ $row = mysql_fetch_array($result); $this->emailAddress = $row['email_address']; return true; } else { return false; } } } // EMAIL MODEL class Email { public static function send(User $user){ if($user->status === 1){ $subject = 'Test email from ' . Config::SITE_DOMAIN; $body = '<html><body><h1>Test Email</h1><img src="http://' . Config::SITE_DOMAIN . Config::IMG_PATH . 'image' . $user->id . '-' . rand(0, 9999) . '.png" /></body></html>'; $headers = "From: " . Config::EMAIL_ADDRESS . "\r\n"; $headers .= "Content-type: text/html\r\n"; return mail($user->emailAddress, $subject, $body, $headers); } else { return false; } } } $user = new User($_REQUEST['userId']); Email::send($user); ?>
I never knew that PHP can be formated in a java sort of way with class being public and private.
I believe the class itself can have final and abstract applied to it, but public/private/protected scoping only applies to members of the object. I may be wrong.