Sending HTML-Mail With Attachement Using PHP’s PEAR::Mail Package January 3, 2012Posted 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 :
<?php require "Mail.php"; $to = array("email@example.com"); $headers = array( 'From' => "firstname.lastname@example.org", 'Cc' => "email@example.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 , EOF; $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 :
<?php // require PEAR Mail and Mail_mime classes require('Mail.php'); require('Mail/mime.php'); // define recipient $recipient = 'firstname.lastname@example.org'; // 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 $mime->setHTMLBody('</pre> <h1>This is the HTML part of the message</h1> <pre></pre> <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> <pre>'); // now, we'll add local file as an attachment. Second argument is a mime type // specification for XLSX filetype. $mime->addAttachment($xlsFilename, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $xlsFilename); // 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 :
- Sending HTML-email with Attachment Using Zend Framework’s Mail component
- Sending HTML-email with Attachment Using PHP’s mail() Function
- A Simple Example : Sending Email with Attachment Using Phpmailer
One more article is left : ” Sending HTML-email with Attachment Using the Swift mailer class ” . So , stay tuned and happy coding🙂