April 11, 2006

Mail with Phpmailer

You will find other articles relevant to this document in these sections:
Cameron Manderson @ 5:16 pm

A comon task when we code our application is sending an email. This can be anything from sending a password reminder to an order notification email. Often we need more flexibility than a simple PHP @mail() function call, such as sending mixed mime types, such as Text and HTML or providing multiple email attachments. We may also be required to send through an alternate/redundant mail SMTP server to our hosting company.

I have recommended for some time the use of PHPMailer. It is a LGPL licence and provides you with all your common email requirements.

It allows you to take a Object Oriented approach to your mail transfer.

You create a Mail Object and then can assign various variables to it, such as your TITLE, TO, CC, BCC fields and also define standard text/HTML content. A simple example follows utilising the php @mail function:

$mail = new PHPMailer();
$mail->IsMail();
$mail->IsHTML(true);
$mail->ReplyTo = 'cameronmanderson@gmail.com';
$mail->FromName = 'Cameron Manderson';
$mail->From = 'cameronmanderson@gmail.com';
$mail->AddAddress('cameronmanderson@gmail.com');
$mail->AddAddress('myfriend@gmail.com', 'Cameron Manderson'); // Optional Specify the name
$mail->Subject = 'My Mail Message Subject';
$mail->Body = $contentHTML;
$mail->AltBody = $contentText;
$mail->WordWrap = 72;
$mail->AddAttachment('/home/u/user/toolkit.zip', 'toolkit.zip'); // Demonstrates adding a simple attachment
if(!$mail->Send()) die("We know that mail hasn't been delivered");

As we can see this is quite an easy nice way to create our mail. A more complex example you can specify an alternate SMTP server by performing:

$mail->IsSMTP();
$mail->Host = 'mail.bigpond.com.au';

We can also use it to make attachments (File, String and Inline) through different methods.

Overall the package is quite a neat way of handling your basic mail needs. Its inheritant support of alternate SMTP and handling of HTML means that it suits a majority of our requirements for basic packages. If we require more complex functionality we can simply extend and overwrite the required methods by extending the class PHPMailer.
To learn more about PHPMailer visit their website here. If you wish to learn about setting up use of packages without requiring installation on your hosting service read here. You may wish to use this along with encrypting the mail contents using gnuPG.

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Furl
  • Reddit
  • YahooMyWeb

4 Comments »

  1. If your in the habit of using a configuration file and customising classes it’s quite easy to do with PHPMailer!

    Here’s what I do:

    SETUP A CONFIGURATION FILE

    < ?php
     
    // Site configuration settings
    ...
    ...
    ...
    // Email Settings (For PHPMailer)
    define('EMAIL_FROM_NAME', 'Webmaster' ); // from email name
    define('EMAIL_FROM_ADDRESS', 'webmaster@mywebsite.com'); // from email address
    // Do we need to relay to a different server???
    // -> Provide option to use external mail server.
    define('SMTP_MODE', 'disabled'); // enabled or disabled
    define('SMTP_HOST', '');
    define('SMTP_PORT', '');
    define('SMTP_USERNAME', '');
    ?>

    CREATE A CUSTOM CLASS

    require_once($_SERVER['DOCUMENT_ROOT'].'/lib/phpmailer/class.phpmailer.php');
     
    class MyMailer extends PHPMailer
    {
    var $priority = 3;
    var $to_name;
    var $to_email;
    var $From = null;
    var $FromName = null;
    var $Sender = null;
     
    function MyMailer()
    {
     
    if(defined('SMTP_MODE') && SMTP_MODE == 'enabled')
    {
    $this->Host = SMTP_HOST;
    $this->Port = SMTP_PORT;
    if(defined('SMTP_USERNAME') && !empty(SMTP_USERNAME))
    {
    $this->SMTPAuth  = true;
    $this->Username  = SMTP_USERNAME;
    $this->Password  =  SMTP_PASSWORD;
    }
    $this->Mailer = "smtp";
    }
    if(!$this->From) {
    $this->From = EMAIL_FROM_ADDRESS;
    }
    if(!$this->FromName) {
    $this-> FromName = EMAIL_FROM_NAME;
    }
    if(!$this->Sender) {
    $this->Sender = EMAIL_FROM_ADDRESS;
    }
    $this->Priority = $this->priority;
    }//end constructor
    } //end class
    ?>

    IN USE:

    < ?php
     
    // Include site config
    require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
     
    // Include the MyMail class
    require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MyMailer.php');
     
    // Instantiate the class
    $mailer = new MyMailer();
     
    // Set the subject
    $mailer->Subject = 'This is the subject';
     
    // Body
    $mailer->Body = 'This is the body';
     
    // Add an address to send to.
    $mailer->AddAddress('foo@bar.com.au', 'Foo Bar');
     
    if(!$mailer->Send())
    {
    echo 'ERROR: Unable to send email';
    }
    else
    {
    echo 'SUCCESS: Mail sent!';
    }
    // Clear all addresses and attachments ready for next email
    $mailer->ClearAddresses();
    $mailer->ClearAttachments();
    ?>

    Comment by Richard Lee — April 12, 2006 @ 10:09 am

  2. For those of you familiar to MVC framework, the configurations can be managed through your struts/phpmvc config. You could create a MailerPlugin which allows you to accept XML configurations for your particular site. Then you can use various param-set for each default for the email you want. From here you can get your Application Server to generate you a new email through $mailPlugin->getDefaultMail(). This way your configuration is centralised to an XML document and later you can easily change your implementation of the object that is returned; you may start with a simple PHPMail and later upgrade to a MyMail instance (like above). You application would therefore not need to worry about what correct type of instance to use as all mail creation is centralised to your plugin configuration, and all email code does not need to be changed if an upgrade to your email objects occur.

    Comment by Cameron Manderson — April 12, 2006 @ 12:11 pm

  3. […] Mail with Phpmailer […]

    Pingback by Your Question Here » ramasruthi — May 3, 2006 @ 1:47 pm

  4. nice tutorial,is this support all type of lanuages, mean for non english character unicode. thanks

    Comment by used cars — August 3, 2007 @ 10:26 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment