jump to navigation

Sending HTML-Mail With Attachement Using PHP’s PEAR::Mail Package January 3, 2012

Posted by Tournas Dimitrios in PHP.

Developing web-applications with  PHP is an exciting  process , you are never limited into an specific server environment . If one solution isn’t possible , just try another one . PHP has similar characteristics as the *nix terminal  , there is no “ultimate correct” way to accomplish a task , just use the method that sweets your taste .  Keep in mind that each method has his own unique Achilles’ heel , so you have to adopt your security strategy accordingly . One characteristic example is when sending mail with PHP , we have different options to choose from : PHP’s native mail() function , PHPMailer , SwiftMailer , ZF’s Zend_Mail , Pear’s Mail.  Certainly more options could be added to the list , but these are  definitely more popular and well known in the PHP community . PHP’s mail()  is probably the simplest method , but it lacks certain things . One of these is the validation of an email address and  if no extra security measures are implemented into the code then it is vulnerable to Email injection or mail form spamming .  Hopefully  this introduction emphasizes that no matter what solution is chosen , we have to be careful witch  security measures should be implemented .  This article is focused on  PEAR’s  Mail function , let’s demonstrate a couple  practical examples .

Firstly , the package has to be installed on our environment (read my article ,  Installing PEAR Package Manager on Wamp2 in Ten Simple Steps )  :
pear install – – alldeps mail         pear install – – alldeps Mail_Mime  

As an extra step verify that the installation is completed with “pear  list ” , it should be listed  in your terminals window . Sending a mail is simple as :

require "Mail.php";
$to = array("john@somedomain.com");
$headers = array(
'From' => "yourname@yourdomain.com",
'Cc' => "anothername@somedomain.com",
'Subject' => "Using PEAR::Mail to send email"
$body = <<< EOF
Hi John ,
Just testing how to send email from a PHP script with the PEAR::Mail class.
Kind regards ,
$mail = Mail::factory('mail');
$mail->send($to, $headers, $body);


The code above simply sends a plain text message with headers . It uses the built-in mail() function to send the message . Notice the line Mail::factory(‘mail’) . The PEAR::Mail class has multiple factories to choose from (mail, sendmail, and smtp) . Those of you who use object-oriented programming will know that it uses factories to load and implement different technologies with the same application programming interfaces (APIs) . In the case of the Mail() class , you can load the class elements needed to communicate with the mail server when the Mail object is created . The pear package has three factory methods :

  • mail factory – uses the built-in mail() function to send email .
  • Sendmail factory – initiates communication directly with the sendmail program .
  • Smtp factory – uses sockets to connect directly to a mail server. It also supports authentication , which of course allows for secure communication . For example you could use your Google mail account to send mail . Keep in mind that this functionality is suspended on most shared-hosting companies .

Of course we can also send attachments and  HTML – formatted mail with PEAR::Mail . That’s the reason we had installed the Mail_Mime extension in previous steps . A simple practical example could be :

// require PEAR Mail and Mail_mime classes
// define recipient
$recipient = 'john@gmail.com';
// define local path to excel file
$xlsFilename = 'zzz.xml';
// define mail headers
$headers = array('From'       => 'Admin <Admin@example.com>',
                               'To'         => $recipient,
                               'Subject'    => 'Demonstration Sending mail with Pear '
    $mime = new Mail_mime("\n");
    // if client reading this mail, reads only text-part (or cannot read HTML messages),
    // he'll see this text body message
    $mime->setTXTBody('This is the "TXT" part of the message');
    // if client reading this email, reads HTML (multi-part) messages,
    // he'll see this HTML body message
<h1>This is the HTML part of the message</h1>
<p style="background-color: beige;">Natoque magnis magna placerat mauris purus enim nisi ut nunc mattis magnis tincidunt, augue! Facilisis, odio! Eu risus egestas scelerisque? Aenean phasellus integer magnis a penatibus porttitor mauris turpis eros dapibus sociis, tristique urna porttitor sit, scelerisque scelerisque, elementum turps ?</p>

    // now, we'll add local file as an attachment. Second argument is a mime type
    // specification for XLSX filetype.
    // get body of the e-mail message from Mail_mime class.
    $body = $mime->get();
    // get headers
    $hdrs = $mime->headers($headers);
    // now, lets send e-mail using the PEAR Mail class.
    $mail = Mail::factory('mail');
    $mail->send($recipient, $hdrs, $body);
    // that's it!

Read also :

One more article is left : ” Sending HTML-email with Attachment Using the Swift mailer class ”  . So ,  stay tuned and happy coding 🙂



1. other PHP mail tools | Sam's PHP How-To - January 5, 2012

[…] Sending HTML-Mail With Attachement Using PHP’s PEAR::Mail Package Share this:PrintEmailMoreLike this:LikeBe the first to like this post. […]

2. My Homepage - January 5, 2012

… [Trackback]…

[…] Read More: tournasdimitrios1.wordpress.com/2012/01/03/sending-html-mail-with-attachement-using-phps-pearmail-package/ […]…

3. pozycjonowanie stron - January 17, 2012

I conceive other website proprietors should take this site as an model, very clean and wonderful user pleasant design and style . “When we lose one blessing, another is often, most unexpectedly, given in its place.” by Clive Staples Lewis.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s