| [ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # This file is part of Plume CMS, a website management application. 6 # Copyright (C) 2001-2006 Loic d'Anterroches and contributors. 7 # 8 # Plume CMS is free software; you can redistribute it and/or modify 9 # it under the terms of the GNU General Public License as published by 10 # the Free Software Foundation; either version 2 of the License, or 11 # (at your option) any later version. 12 # 13 # Plume CMS is distributed in the hope that it will be useful, 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 # GNU General Public License for more details. 17 # 18 # You should have received a copy of the GNU General Public License 19 # along with this program; if not, write to the Free Software 20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 # 22 # ***** END LICENSE BLOCK ***** */ 23 24 /** 25 * Generate multipart emails. 26 * 27 * Class to easily generate multipart emails. It supports embedded 28 * images within the email. It can be used to send both a text version 29 * and the HTML equivalent version of a message. 30 * 31 * The encoding of the message is utf-8 by default. 32 * 33 * Usage example: 34 * <code> 35 * $email = new Plume_Mail('from_email@example.com', 'to_email@example.com', 36 * 'Subject of the message'); 37 * $img_id = $email->addAttachment('/var/www/html/img/pic.jpg', 'image/jpg'); 38 * $email->addMessage('<html><head></head><body>'."\n" 39 * .'This is text before <img src="cid:'.$img_id.'"> and after.'."\n" 40 * .'</body></html>', 41 * 'text/html'); 42 * $email->sendMail(); 43 * </code> 44 * 45 * @credits krisdover on http://www.php.net/manual/en/function.mail.php 46 * @credits umu on http://www.php.net/manual/en/function.imap-8bit.php 47 */ 48 class Plume_Mail 49 { 50 var $header; 51 var $parts; 52 var $message; 53 var $subject; 54 var $to_address; 55 var $boundary; 56 var $encoding = 'utf-8'; 57 58 /** 59 * Delimitation character of the headers. 60 */ 61 var $hd = "\n"; 62 63 /** 64 * Extra headers. 65 * 66 * Associative array of extra headers to add to the message. 67 */ 68 var $headers = array(); 69 70 /** 71 * Construct the base email. 72 * 73 * FIXME: To provide a document as text and an alternative as 74 * HTML, the content type should be multipart/alternative with one 75 * plain/text and one HTML document. An option somewhere should 76 * enable this option. 77 * 78 * @param string The email of the sender. 79 * @param string The destination email. 80 * @param string The subject of the message. 81 * @param string Encoding of the message ('utf-8) 82 */ 83 function Plume_Mail($src, $dest, $subject, $encoding='utf-8') 84 { 85 $this->to_address = $dest; 86 $this->subject = $subject; 87 $this->parts = array(); 88 $this->boundary = '------------' . md5(uniqid(time())); 89 $this->encoding = 'utf-8'; 90 $this->header = 'From: '.$src.$this->hd 91 .'MIME-Version: 1.0'.$this->hd 92 .'Content-Type: multipart/related;'."\n" 93 .' boundary="'.$this->boundary.'"'.$this->hd 94 .'X-Mailer: Plume CMS - http://plume-cms.net/'; 95 } 96 97 /** 98 * Add the base plain text message to the email. 99 * 100 * @param string The message 101 * @param string The mime-type ('text/plain;') 102 */ 103 function addMessage($msg='', $ctype='text/plain') 104 { 105 // Base message is always the first element. 106 array_unshift($this->parts, 107 'Content-Type: '.$ctype.'; charset='.$this->encoding 108 ."\n\n".$msg); 109 } 110 111 /** 112 * Add an attachment to the message. 113 * 114 * The file to attach must be available on disk and you need to 115 * provide the mimetype of the attachment manually. 116 * 117 * The id of the attachment can be used for embedding images in 118 * HTML emails. Avoid abusing the use of them or your emails will 119 * be flagged as spam. 120 * 121 * @param string Path to the file to be added. 122 * @param string Mimetype of the file to be added. 123 * @return string The id of the attachment. 124 */ 125 function addAttachment($file, $ctype){ 126 $fname = basename($file); 127 $data = file_get_contents($file); 128 $i = count($this->parts); 129 $content_id = 'part'.$i.sprintf('%09d', crc32($fname)) 130 .strrchr($this->to_address, '@'); 131 $this->parts[$i] = 'Content-Type: '.$ctype.'; name="'.$fname."\n" 132 .'Content-Transfer-Encoding: base64'."\n" 133 .'Content-ID: <'.$content_id.'>'."\n" 134 .'Content-Disposition: inline;'."\n" 135 .' filename="'.$fname."\n\n". 136 chunk_split(base64_encode($data), 68, "\n"); 137 return $content_id; 138 } 139 140 /** 141 * Generate the message. 142 */ 143 function buildMessage() 144 { 145 $this->message = 'This is a multipart message in mime format.'."\n"; 146 foreach ($this->parts as $part) { 147 $this->message .= '--'.$this->boundary."\n".$part."\n"; 148 } 149 $this->message .= '--'.$this->boundary.'-- '."\n"; 150 } 151 152 /** 153 * Get the message body as a string. 154 * 155 * @return string Message body 156 */ 157 function getMessage() 158 { 159 $this->buildmessage(); 160 return $this->message; 161 } 162 163 /** 164 * Effectively sends the email. 165 */ 166 function sendMail(){ 167 $this->buildmessage(); 168 mail($this->to_address, $this->subject, $this->message, $this->header); 169 } 170 171 /** 172 * Will be used when allowing additional headers. 173 * 174 * http://www.php.net/manual/en/function.imap-8bit.php 175 */ 176 function quoted_printable_encode($text) { 177 // split text into lines 178 $lines = explode(chr(13).chr(10), $text); 179 for ($i=0; $i<count($lines); $i++) { 180 $line =& $lines[$i]; // $line is modified by reference 181 if (strlen($line)===0) { 182 continue; // do nothing, if empty 183 } 184 $reg_exp = '/[^\x20\x21-\x3C\x3E-\x7E]/e'; 185 $replace = 'sprintf( "=%02X", ord ( "$0" ) ) ;'; 186 $line = preg_replace($reg_exp, $replace, $line); 187 188 // encode x09,x20 at lineends 189 $length = strlen($line); 190 $last_char = ord($line{$length-1}); 191 192 // imap_8_bit does not encode x20 at the very end of a text, 193 // here is, where I don't agree with imap_8_bit, 194 // please correct me, if I'm wrong, 195 // or comment next line for RFC2045 conformance, if you like 196 if ($i != count($lines)-1) { 197 if (($last_char==0x09)||($last_char==0x20)) { 198 $line{$length-1}='='; 199 $line .= ($last_char==0x09) ? '09' : '20'; 200 } 201 } 202 $line=str_replace(' =0D', '=20=0D', $line); 203 // finally split into softlines no longer than 76 chars, 204 // for even more safeness one could encode x09,x20 205 // at the very first character of the line 206 // and after soft linebreaks, as well, 207 // but this wouldn't be caught by such an easy RegExp 208 preg_match_all('/.{1,73}([^=]{0,2})?/', $line, $match); 209 $line = implode('='.chr(13).chr(10), $match[0]); // add soft crlf's 210 211 } 212 // join lines into text 213 return implode(chr(13).chr(10), $lines); 214 } 215 } 216 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
|