Wednesday, August 1, 2007

PHPMailer Helper Class Demystified

Hi, welcome to another episode of my programming tutorial here on Vic2x Programming Tutorials Portal. This is about the famous PHP class used commonly in most PHP mail applications. It's called PHPMailer, you can download the thing from http://phpmailer.sourceforge.net/

At first, upon looking at the manual on how to use PHPMailer class, I was telling myself, this will be another get-a-headache. But, sometimes, we just need to crack codes for our survival, the deadlines are always fast approaching. What I did, I studied the class on how to initialize its member variables and what member functions to call before sending a mail or set of emails. I noticed however, that by just doing the initialization and calling several member functions, would take some time to make unless you just copy-paste your previous work using the PHPMailer Class. I decided to make a helper class to do the tedious initializations and calling member functions just to send an email. Helper class for beginners' sake, are simply helper class! Just kidding. Ok, seriously, a helper class will simply make your life easier. Most of the time, you will only have small number of lines in your programs to call some complicated functions or classes, in our case, the PHPMailer. That means, if you need to have 15 lines of codes just to send an email using PHPMailer class, using a helper class will lessen the lines of codes. Ok, I know what you're talking, let me give you the listings:

Prerequisites:
  1. You must have downloaded the PHPMailer class. The filename is class.phpmailer.php
    Simply extract that file in the folder where the following PHP files reside. Mine, I copied all of them to my htdocs folder, which is the root of your PHP engine.
  2. Once again, the following PHP scripts below should be located in the same folder as your class.phpmailer.php
Listing 1. Name your this PHP file as, sender.php


<?php
require_once('xmail.php');
$myMailer = new XMail("Test Subjet", "yourEmail@domain.com", "Juan Dela Cruz");
$emailAdds = "joemarie.aliling@gmail.com,joemarie.aliling@gmail.com";
$emailBody = "<h1>This is a test message</h1>";
if($myMailer->send($emailAdds, $emailBody)){
echo 'Mail has been sent.';
}else{
echo 'Failed to send mail.';
}
?>


Listing 2. This is the helper class, the XMail class, name this as xmail.php


<?php
require_once 'class.phpmailer.php';
class XMail{
var $objMail;
function XMail($subj='',$frmEmail='',$frmName=''){
objMail = new PHPMailer(); //instantiate our PHPMailer object
$this->objMail->From = $frmEmail;
$this->objMail->FromName = $frmName;
$this->objMail->isMail(); //we'll use the php core mail function
$this->objMail->Subject = $subj;
$this->objMail->isHTML(true); //we'll be sending an HTML email format..
}
function send($mailID, $mailBody){
$this->objMail->Body = $mailBody;
$arr = explode(",", $mailID); //separate the comma separated emails..
foreach($arr as $key => $value){ //loop through all addresses..
$this->objMail->AddAddress($value,"Recipient"); //set the recepient's address..
if(!$this->objMail->send()) return false; //exit with failure..
}
return true;
}
}
?>


Sorry, I made the listings' font size smaller so as to make it fit on the page.

There you are.. Simply execute the sender.php on your browser. But take note, it must be run on a server with mail delivery engine so that you can test the real action of the code. The code is easy to understand, take a while to read it, I commented all that need to be explained. You can use the XMail class for single email address or multiple addresses recipients. Just use comma to separate each email address. The mailer used in the XMail is simply the PHP's mail function. You can also set that to SMTP based one. Just go to the site of PHPMailer and see the documents on how to use SMTP delivery with the PHPMailer. If you have questions on the code, drop me a comment here, alright!

The codes presented here are free to use, reverse-engineer and use them in your applications
I hope, I have given you some insights on how Helper Classes or functions can help your programming tasks easier. Well, well, well, there you have it, PHPMailer Helper Class Demystified!

I would like to acknowledge the PHPMailer Team, thanks for the source! More power to you all!

-Author, Philippines

4 comments:

zendAngle said...

Hey dude! Thanks for the info! I'm using it right now in my PHP codes and modified a few structure of your Helper Class. It's working in my app. Thanks!

bokybokx said...

Boss.... Yoh... Not yet reach this part of PHP... Just basic stuff... But thanks boss.... Hey... Got trouble here... How could I make a picture gallery with next and previous button??? Need help...??? Bokybokx... Thanks...

j03m4r13 said...

You can have a complete library for this boxx. There's a lot of free or open source codes doing picture gallery. Once installed, you only need to care a little and everything will work fine. Just try to search on google.

The whole concept is this, well, it's should be better be DB driven, if that's for the web, go for mySQL. All pictures can be stored in the DB itself, using BLOB datatype. Once you have that, it's a lot easier coding the whole thing. This calls to the technique called Paging. If you're interested, you can ask me a sample code if you like.

Alright?? Cool! Peace Man! Wiki Wiki! Spread the Words! May the Force be with you!

bokybokx said...

senyore...

Just searching now.. . thanks a lot man.. . Need your codes... For our project... Hehehe.. "3dbacolod.com" - hope I could show you when we finish it....

Just either post the codes here or on our Friendster group...

Muchas Gracias.....