» PHPMailer for PHP4
» PHPMailer for PHP5/6
» PHPMailer-FE (Form2Email)
» PHPMailer-ML
» PHPMailer-BMH

Extending PHPMailer

Extending classes with inheritance is one of the most powerful features of object-oriented programming. It allows you to make changes to the original class for your own personal use without hacking the original classes. Plus, it is very easy to do. I've provided an example:

Here's a class that extends the PHPMailer class and sets the defaults for the particular site:
PHP include file: mail.inc.php


require("class.phpmailer.php");

class MyMailer extends PHPMailer {
    // Set default variables for all new objects
    public $From     = "from@email.com";
    public $FromName = "Mailer";
    public $Host     = "smtp1.site.com;smtp2.site.com";
    public $Mailer   = "smtp";                         // Alternative to IsSMTP()
    public $WordWrap = 75;

    // Replace the default error_handler
    function error_handler($msg) {
        print("My Site Error");
        print("Description:");
        printf("%s", $msg);
        exit;
    }

    // Create an additional function
    function do_something($something) {
        // Place your new code here
    }
}
    

Now here's a normal PHP page in the site, which will have all the defaults set above:
Normal PHP file: mail_test.php


require("mail.inc.php");

// Instantiate your new class
$mail = new MyMailer;

// Now you only need to add the necessary stuff
$mail->AddAddress("josh@site.com", "Josh Adams");
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the message body";
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name

if(!$mail->Send())
{
   echo "There was an error sending the message";
   exit;
}

echo "Message was sent successfully";
    
Design by WorxStudio.com Copyright © 1999-2009, Worx International Inc. All Rights Reserved. Privacy Policy