15.7 Sending HTML E-mail


You want to send HTML-enabled e-mail with PHP.

Technique

HTML-enabled messages are simply MIME-encoded messages in which the attachment is of type text/html . So, create a new class that inherits from the one in recipe 15.6:

 <?php /**  *  A class for sending HTML e-mails  *  * @author Sterling Hughes <sterling@php.net>  */ class HTML_Email extends MIME {     // {{{ HTML_Email()     /**      *  Constructor adds the To From and Subject      *  fields of your e-mail      *      * @param $to string Who your sending the message to.      * @param $from string Who the message is from.      * @param $subject string The subject of the message      *      * @return object A new HTML_Email object.      */     function HTML_Email ($to, $from, $subject) {         $this->MIME($to, $from, $subject);     }     // }}}     // {{{ html_data()     /**      *  Add your HTML message to the e-mail      *      * @param $html string The HTML message.      *      * @return null      */     function html_data ($html) {         $this->attachment("", $html, "text/html");     }     // }}}     // {{{ plain_data()    /**      * Add your Plain text message to the e-mail      *      * @param $data string The Plain text message.      *      * @return null      */     function plain_data ($data) {         $this->body = $data;     }     // }}} } ?> 

Comments

Sending an HTML-enabled e-mail is nearly the same thing as sending any attachment, but the content-type must be text/html and the filename must not be set. Here we create a simple class that will interface with the MIME class (as well as inheriting its core methods ) You can use the HTML_Email class like so:

 <?php $mm = new HTML_Email('andrei@php.net',                      'sterling@php.net',                      'Annoying HTML Message'); $mm->html_data($html); $mm->plain_data(strip_tags($html)); $mm->send(); ?> 

As a side note on usability, always make sure that when you send an HTML message, you also send a plain text version for people (like myself ) who can't stand receiving HTML e-mails.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net