15.6 Sending Binary Attachments


You want to send binary attachments, not just the plain text attachments described in recipe 15.5.

Technique

All that is necessary is to change the content-type declaration, but I'm getting tired of retyping the same formatting for the messages. So, I think I will create a class that will do it for me:

 <?php mt_srand((double)microtime()*1000000); /**  *  MIME class for building muiltipart MIME messages.  *  * @author Sterling Hughes <sterling@php.net>  */ class MIME {     // {{{ properties     var $attachments = array();     var $to = "";     var $from = "";     var $subject ="";     var $body = "";     // }}}     // {{{ MIME()     /**      * Constructor class that adds the values of the      * required parts.      *      * @param $to string Who to send the message to.      * @param $from string Who sent the message.      * @param $subject string subject of the message      * @param $body string the body of the message.      *      * @return object the new MIME object.      */     function MIME ($to, $from='', $subject='', $body='') {         $this->to = $to;         $this->from = $from;         $this->subject = $subject;         $this->body = $body;     }     // }}}     // {{{ attachment()     /**      *  Add an attachment to the message      *      * @param $name string Name of the attachment.      * @param $body string Body of the attachment.      * @param $type string Content-type of the attachment.      * @param $encoding string encoding of the attachment.      *      * @return null      */     function attachment ($name = "",                          $contents = "",                          $type = "application/octet-stream",                          $encoding = "base64")   {         $this->attachments[] =                             array("filename" => $name,                                   "type"     => $type,                                   "encoding" => $encoding.                                   "data"     => $contents);     }     // }}}     // {{{ _build()     function _build () {         $boundary = 'b' . md5(uniquid(mt_rand())) + getmypid();         if ($this->from != "")             $ret = "From: " . $this->from . "\r\n";         $ret .= 'Content-type: multipart/mixed; ';         $ret .= "boundary = $boundary\r\n\r\n";         $ret .= "This is a MIME encoded message.\r\n\r\n";         $ret .= "--$boundary";         $ret .= "Content-type: text/plain\r\n";         $ret .= "Content-Transfer-Encoding: 7bit\r\n\r\n";         $ret .= $this->body . "\r\n--$boundary";         foreach ($this->attachments as $attachment) {             $attachment[data] = base64_encode($attachment[data]);             $attachment[data] = chunk_split($attachment[data]);             $data =                 "Content-type: $attachment[type]" .                $attachments[filename] ?                  "; name = \"$attachments[filename]\"" : "" .                "\r\n" .                "Content-Transfer-Encoding: $attachments[encoding]" .                "\r\n\r\n$attachment[data]\r\n";              $ret .= "\r\n--$data--$boundary";         }         $ret .= "--\r\n";         return($ret);     }     // }}}     // {{{  send()     /**      * Send the prebuilt message      *      * @return bool true on success, false on failure.      */     function send () {         return @mail($this->to,                      $this->subject,                      '',                      $this->_build());     } } ?> 

You would use this class like this:

 <?php //posted data is $to, $email, $subject, $body $mm = new MIME($to, $email, $subject, $body); $mm->attachment("picture.jpg",                 $contents,                 "image/jpeg"); $mm->send(); ?> 

Comments

The MIME class will take all the standard email information and send a mail message to the specified recipient. Let's go over the basic methods and functions of this class.

The first interesting method is the MIME() method. This method is a constructor, meaning that it is called when an instance of the class is created. The purpose of this function is to set the $to , $from , $subject , and $body attributes, which will be used when sending the e-mail.

The next method is the attachment() method. This method adds an attachment to a 2D array that looks like the following:

 attachments   element-array [filename => name of file to send with the                                              attachment                                  type => content type of file                                  encoding => type of file encoding,                                              defaults to base64                                  data => the content of the attachment] 

Next we have the _build() method. This method builds the entire message and returns the contents. The attachments to the message are all encoded with base 64 encoding and the body of the message is in the standard 8-bit encoding.

Finally, the send() method uses the built-in mail() function to send an email to the specified address with the content made with the _build method. One thing that should be noted is that the mail() function specifies the entire message in the additional headers function and leaves the body argument empty.



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