5 * code that used to be in Pman (sendTemplate / emailTemplate)
7 * template is in template directory subfolder 'mail'
9 * eg. use 'welcome' as template -> this will use templates/mail/welcome.txt
10 * if you also have templates/mail/welcome.body.html - then that will be used as
17 * $x= new Pman_Core_Mailer($opts)
23 replaceImages => true|false,
24 rcpts => array() // override recipients..
27 * recipents is gathered from the resulting template
35 * $x->asData(); // returns data needed for notify?? - notify should really
36 * // just use this to pass around later..
42 class Pman_Core_Mailer {
44 var $page = false; /* usually a html_flexyframework_page */
45 var $contents = false; /* object or array */
46 var $template = false; /* string */
47 var $replaceImages = false; /* boolean */
50 var $images = array(); // generated list of cid images for sending
53 function Pman_Core_Mailer($args) {
54 foreach($args as $k=>$v) {
61 * ---------------- Global Tools ---------------
67 $templateFile = $this->template;
68 $args = $this->contents;
70 $content = clone($this->page);
72 foreach((array)$args as $k=>$v) {
76 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
78 $ff = HTML_FlexyFramework::get();
79 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
80 if (isset($ff->Pman['HTTP_HOST'])) {
81 $http_host = $ff->Pman['HTTP_HOST'];
85 $content->HTTP_HOST = $http_host;
89 // this should be done by having multiple template sources...!!!
91 require_once 'HTML/Template/Flexy.php';
94 $htmltemplate = new HTML_Template_Flexy( );
96 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
97 // then we have a multi-part email...
100 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
101 $htmlbody = $htmltemplate->bufferedOutputObject($content);
103 // for the html body, we may want to convert the attachments to images.
105 $htmlbody = $this->htmlbodytoCID($htmlbody);
112 $template = new HTML_Template_Flexy( array(
116 $template->compile('mail/'. $templateFile.'.txt');
118 /* use variables from this object to ouput data. */
119 $mailtext = $template->bufferedOutputObject($content);
130 //echo "<PRE>";print_R($mailtext);
132 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
133 require_once 'Mail/mimeDecode.php';
134 require_once 'Mail.php';
136 $decoder = new Mail_mimeDecode($mailtext);
137 $parts = $decoder->getSendArray();
138 if (PEAR::isError($parts)) {
140 //echo "PROBLEM: {$parts->message}";
147 $parts[1]['Message-Id'] = '<' . $content->msgid .
148 '@' . $content->HTTP_HOST .'>';
153 if ($htmlbody !== false) {
154 require_once 'Mail/mime.php';
155 $mime = new Mail_mime(array('eol' => "\n"));
157 $mime->setTXTBody($parts[2]);
158 $mime->setHTMLBody($htmlbody);
160 foreach($this->images as $cid=>$cdata) {
165 $cid.'.'.$cdata['ext'],
170 $parts[2] = $mime->get();
171 $parts[1] = $mime->headers($parts[1]);
177 // list($recipents,$headers,$body) = $parts;
179 'recipents' => $parts[0],
180 'headers' => $parts[1],
187 $email = $this->toData();
188 if (is_a($email, 'PEAR_Error')) {
191 ///$recipents = array($this->email);
192 $mailOptions = PEAR::getStaticProperty('Mail','options');
193 $mail = Mail::factory("SMTP",$mailOptions);
194 $headers['Date'] = date('r');
195 if (PEAR::isError($mail)) {
198 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
199 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
200 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
201 error_reporting($oe);
206 function htmlbodytoCID($html)
208 $dom = new DOMDocument;
209 $dom->loadHTML($html);
210 $imgs= $dom->getElementsByTagName('img');
212 foreach ($imgs as $i=>$img) {
213 $url = $img->getAttribute('src');
214 $conv = $this->fetchImage($url);
215 $this->images[$conv['contentid']] = $conv;
217 $img->setAttribute('src', 'cid:' . $conv['contentid']);
221 return $dom->saveHTML();
226 function fetchImage($url)
229 if ($url[0] == '/') {
230 $file = $this->page->rootDir . $url;
231 require_once 'File/MimeType.php';
232 $m = new File_MimeType();
233 $mt = $m->fromFilename($file);
234 $ext = $m->toExt($mt);
239 'contentid' => md5($file),
247 //print_R($url); exit;
250 if (preg_match('#^file:///#', $url)) {
251 $file = preg_replace('#^file://#', '', $url);
252 require_once 'File/MimeType.php';
253 $m = new File_MimeType();
254 $mt = $m->fromFilename($file);
255 $ext = $m->toExt($mt);
260 'contentid' => md5($file),
267 // 2 files --- the info file.. and the actual file...
268 $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
269 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
270 $ret = json_decode($cache);
271 $ret['file'] = $cache . '.data';
274 if (!file_exists(dirname($cache))) {
275 mkdir(dirname($cache),0666, true);
278 require_once 'HTTP/Request.php';
279 $a = new HTTP_Request($url);
281 file_put_contents($cache .'.data', $a->getResponseBody());
283 $mt = $a->getResponseHeader('Content-Type');
285 require_once 'File/MimeType.php';
286 $m = new File_MimeType();
287 $ext = $m->toExt($mt);
292 'contentid' => md5($url)
296 file_put_contents($cache, json_encode($ret));
297 $ret['file'] = $cache . '.data';