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 * require_once 'Pman/Core/Mailer.php';
18 * $x= new Pman_Core_Mailer(array(
20 // if bcc is property of this, then it will be used (BAD DESIGN)
22 'template' => 'your_template',
23 // must be in templates/mail direcotry..
24 // header and plaintext verison in mail/your_template.txt
25 // if you want a html body - use mail/your_template.body.html
27 // 'bcc' => 'xyz@abc.com,abc@xyz.com', // string...
28 // 'contents' => array(), // << keys must be trusted
29 // if bcc is property of contents, then it will be used (BAD DESIGN)
31 // 'html_locale => 'en', // always use the 'english translated verison'
32 // 'cache_images => true, // -- defaults to caching images - set to false to disable.
33 // 'replaceImages => false, // should images be replaced.
34 // 'urlmap => array( // map urls from template to a different location.
35 // 'https://www.mysite.com/' => 'http://localhost/',
37 // 'locale' => 'en', // .... or zh_hk....
39 // 'attachments' => array(
41 // 'file' => '/path/to/file', // file location
42 // name => 'myfile.pdf', // (optional) - uses basename of file
46 // 'mail_method' => 'SMTP', // or SMTPMX
50 * recipents is gathered from the resulting template
58 * $x->toData(); // returns data needed for notify?? - notify should really
59 * // just use this to pass around later..
65 class Pman_Core_Mailer {
67 var $page = false; /* usually a html_flexyframework_page */
68 var $contents = false; /* object or array */
69 var $template = false; /* string */
70 var $replaceImages = false; /* boolean */
72 var $templateDir = false;
73 var $locale = false; // eg. 'en' or 'zh_HK'
74 var $urlmap = array();
77 var $html_locale = false; // eg. 'en' or 'zh_HK'
78 var $images = array(); // generated list of cid images for sending
79 var $attachments = false;
80 var $css_inline = false; // put the css into the html
81 var $css_embed = false; // put the css tags into the body.
83 var $mail_method = 'SMTP';
85 var $cache_images = true;
89 var $body_cls = false;
91 function __construct($args) {
92 foreach($args as $k=>$v) {
96 // allow core mailer debug setting.
97 $ff = HTML_FlexyFramework::get();
99 if (!empty($ff->Core_Mailer['debug'])) {
100 $this->debug = $ff->Core_Mailer['debug'];
102 //$this->log("URL MAP");
103 //$this->log($this->urlmap);
108 * ---------------- Global Tools ---------------
113 $templateFile = $this->template;
114 $args = (array)$this->contents;
115 $content = clone($this->page);
117 foreach($args as $k=>$v) {
121 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
123 $ff = HTML_FlexyFramework::get();
124 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
125 if (isset($ff->Pman['HTTP_HOST']) && $http_host != 'localhost') {
126 $http_host = $ff->Pman['HTTP_HOST'];
129 $content->HTTP_HOST = $http_host;
131 // this should be done by having multiple template sources...!!!
133 require_once 'HTML/Template/Flexy.php';
136 // 'forceCompile' => true,
137 'site_prefix' => false,
138 'multiSource' => true,
141 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
143 //print_R($fopts);exit;
144 if (!empty($fopts['DB_DataObject_translator'])) {
145 $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
147 if (!empty($fopts['locale'])) {
148 $tmp_opts['locale'] = $fopts['locale'];
150 if (!empty($fopts['templateDir'])) {
151 $tmp_opts['templateDir'] = $fopts['templateDir'];
154 if (!empty($this->templateDir)) {
155 $tmp_opts['templateDir'] = $this->templateDir;
158 // local opt's overwrite
159 if (!empty($this->locale)) {
160 $tmp_opts['locale'] = $this->locale;
164 $html_tmp_opts = $tmp_opts;
165 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
166 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
167 // then we have a multi-part email...
168 if (!empty($this->html_locale)) {
169 $html_tmp_opts['locale'] = $this->html_locale;
171 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
173 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
174 $htmlbody = $htmltemplate->bufferedOutputObject($content);
176 $this->htmlbody = $htmlbody;
178 // for the html body, we may want to convert the attachments to images.
179 // var_dump($htmlbody);exit;
181 if(!empty($content->body_cls) && strlen($content->body_cls)){
182 $htmlbody = $this->htmlbodySetClass($htmlbody, $content->body_cls);
185 if ($this->replaceImages) {
186 $htmlbody = $this->htmlbodytoCID($htmlbody);
189 if ($this->css_embed) {
190 $htmlbody = $this->htmlbodyCssEmbed($htmlbody);
193 if ($this->css_inline && strlen($this->css_inline)) {
194 $htmlbody = $this->htmlbodyInlineCss($htmlbody);
198 $tmp_opts['nonHTML'] = true;
199 //$tmp_opts['debug'] = true;
201 // print_R($tmp_opts);
202 // $tmp_opts['force'] = true;
204 $template = new HTML_Template_Flexy( $tmp_opts );
206 $template->compile('mail/'. $templateFile.'.txt');
208 /* use variables from this object to ouput data. */
209 $mailtext = $template->bufferedOutputObject($content);
210 //print_r($mailtext);exit;
214 //echo "<PRE>";print_R($mailtext);
216 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
217 require_once 'Mail/mimeDecode.php';
218 require_once 'Mail.php';
220 $decoder = new Mail_mimeDecode($mailtext);
221 $parts = $decoder->getSendArray();
222 if (PEAR::isError($parts)) {
224 //echo "PROBLEM: {$parts->message}";
230 require_once 'Mail/mime.php';
231 $mime = new Mail_mime(array(
233 //'html_encoding' => 'base64',
234 'html_charset' => 'utf-8',
235 'text_charset' => 'utf-8',
236 'head_charset' => 'utf-8',
238 // clean up the headers...
241 $parts[1]['Message-Id'] = '<' . $content->msgid .
242 '@' . $content->HTTP_HOST .'>';
245 if ($htmlbody !== false) {
246 // got a html headers...
248 if (isset($parts[1]['Content-Type'])) {
249 unset($parts[1]['Content-Type']);
251 $mime->setTXTBody($parts[2]);
252 $this->textbody = $parts[2];
253 $mime->setHTMLBody($htmlbody);
255 // var_dump($mime);exit;
256 foreach($this->images as $cid=>$cdata) {
261 $cid.'.'.$cdata['ext'],
269 if(!empty($this->attachments)){
270 //if got a attachments
271 $header = $mime->headers($parts[1]);
273 if (isset($parts[1]['Content-Type'])) {
274 unset($parts[1]['Content-Type']);
279 if(preg_match('/text\/html/', $header['Content-Type'])){
280 $mime->setHTMLBody($parts[2]);
281 $mime->setTXTBody('This message is in HTML only');
282 $this->textbody = 'This message is in HTML only';
284 $mime->setTXTBody($parts[2]);
285 $this->textbody = $parts[2];
286 $mime->setHTMLBody('<PRE>'.htmlspecialchars($parts[2]).'</PRE>');
289 foreach($this->attachments as $attch){
290 $mime->addAttachment(
293 (!empty($attch['name'])) ? $attch['name'] : '',
302 $parts[2] = $mime->get();
303 $parts[1] = $mime->headers($parts[1]);
308 'recipents' => $parts[0],
309 'headers' => $parts[1],
313 if ($this->rcpts !== false) {
314 $ret['recipents'] = $this->rcpts;
316 // if 'to' is empty, then add the recipents in there... (must be an array?
317 if (!empty($ret['recipents']) && is_array($ret['recipents']) &&
318 (empty($ret['headers']['To']) || !strlen(trim($ret['headers']['To'])))) {
319 $ret['headers']['To'] = implode(',', $ret['recipents']);
323 // add bcc if necessary..
324 if (!empty($this->bcc)) {
325 $ret['bcc'] = $this->bcc;
329 function send($email = false)
332 $ff = HTML_FlexyFramework::get();
336 $email = is_array($email) ? $email : $this->toData();
338 if (is_a($email, 'PEAR_Error')) {
339 $pg->addEvent("COREMAILER-FAIL", false, "email toData failed");
345 //$this->log( htmlspecialchars(print_r($email,true)));
347 ///$recipents = array($this->email);
348 // $mailOptions = PEAR::getStaticProperty('Mail','options');
350 $mailOptions = isset($ff->Mail) ? $ff->Mail : array();
351 //print_R($mailOptions);exit;
353 if ($this->mail_method == 'SMTPMX' && empty($mailOptions['mailname'])) {
354 $pg->jerr("Mail[mailname] is not set - this is required for SMTPMX");
358 $mail = Mail::factory($this->mail_method,$mailOptions);
360 $mail->debug = (bool) $this->debug;
363 $email['headers']['Date'] = date('r');
364 if (PEAR::isError($mail)) {
365 $pg->addEvent("COREMAILER-FAIL", false, "mail factory failed");
370 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
374 // this makes contents untrustable...
375 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
376 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
379 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
386 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
387 error_reporting($oe);
389 $pg->addEvent("COREMAILER-SENT", false,
390 'To: ' . ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
391 'Subject: ' . @$email['headers']['Subject']
394 $pg->addEvent("COREMAILER-FAIL", false,
395 "Sending to : " . ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
396 " Error: " . $ret->toString());
403 function htmlbodytoCID($html)
405 $dom = new DOMDocument();
406 // this may raise parse errors as some html may be a component..
407 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
408 $imgs= $dom->getElementsByTagName('img');
412 foreach ($imgs as $i=>$img) {
413 $url = $img->getAttribute('src');
414 if (preg_match('#^cid:#', $url)) {
417 $me = $img->getAttribute('mailembed');
422 if(!array_key_exists($url, $urls)){
423 $conv = $this->fetchImage($url);
425 $this->images[$conv['contentid']] = $conv;
429 $img->setAttribute('origsrc', $url);
430 $img->setAttribute('src', 'cid:' . $conv['contentid']);
434 return $dom->saveHTML();
437 function htmlbodyCssEmbed($html)
439 $ff = HTML_FlexyFramework::get();
440 $dom = new DOMDocument();
442 // this may raise parse errors as some html may be a component..
443 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
444 $links = $dom->getElementsByTagName('link');
446 foreach ($links as $link) { // duplicate as links is dynamic and we change it..!
449 //<link rel="stylesheet" type="text/css" href="{rootURL}/roojs1/css-mailer/mailer.css">
451 foreach ($lc as $i=>$link) {
452 //var_dump($link->getAttribute('href'));
454 if ($link->getAttribute('rel') != 'stylesheet') {
457 $url = $link->getAttribute('href');
458 $file = $ff->rootDir . $url;
460 if (!preg_match('#^(http|https)://#', $url)) {
461 $file = $ff->rootDir . $url;
463 if (!file_exists($file)) {
465 $link->setAttribute('href', 'missing:' . $file);
469 $file = $this->mapurl($url);
472 $par = $link->parentNode;
473 $par->removeChild($link);
474 $s = $dom->createElement('style');
475 $e = $dom->createTextNode(file_get_contents($file));
477 $par->appendChild($s);
480 return $dom->saveHTML();
485 function htmlbodyInlineCss($html)
487 $dom = new DOMDocument();
489 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
491 $html = $dom->getElementsByTagName('html');
492 $head = $dom->getElementsByTagName('head');
493 $body = $dom->getElementsByTagName('body');
496 $head = $dom->createElement('head');
497 $html->item(0)->insertBefore($head, $body->item(0));
498 $head = $dom->getElementsByTagName('head');
501 $s = $dom->createElement('style');
502 $e = $dom->createTextNode($this->css_inline);
504 $head->item(0)->appendChild($s);
506 return $dom->saveHTML();
509 require_once 'HTML/CSS/InlineStyle.php';
511 $doc = new HTML_CSS_InlineStyle($html);
513 $doc->applyStylesheet($this->css_inline);
515 $html = $doc->getHTML();
521 function htmlbodySetClass($html, $cls)
523 $dom = new DOMDocument();
525 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
527 $body = $dom->getElementsByTagName('body');
528 if (!empty($body->length)) {
529 $body->item(0)->setAttribute('class', $cls);
531 $body = $dom->createElement("body");
532 $body->setAttribute('class', $cls);
533 $dom->appendChild($body);
537 return $dom->saveHTML();
540 function fetchImage($url)
544 $this->log( "FETCH : $url\n");
546 if ($url[0] == '/') {
547 $ff = HTML_FlexyFramework::get();
548 $file = $ff->rootDir . $url;
549 require_once 'File/MimeType.php';
550 $m = new File_MimeType();
551 $mt = $m->fromFilename($file);
552 $ext = $m->toExt($mt);
557 'contentid' => md5($file), // mailer makes md5 cid's' -- cid with attachment-** are done by mailer.
565 //print_R($url); exit;
568 if (preg_match('#^file:///#', $url)) {
569 $file = preg_replace('#^file://#', '', $url);
570 require_once 'File/MimeType.php';
571 $m = new File_MimeType();
572 $mt = $m->fromFilename($file);
573 $ext = $m->toExt($mt);
578 'contentid' => md5($file),
585 // 2 files --- the info file.. and the actual file...
588 $uinfo = posix_getpwuid( posix_getuid () );
589 $user = $uinfo['name'];
591 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
592 if ($this->cache_images &&
593 file_exists($cache) &&
594 filemtime($cache) > strtotime('NOW - 1 WEEK')
596 $ret = json_decode(file_get_contents($cache), true);
597 $this->log("fetched from cache");
598 $ret['file'] = $cache . '.data';
601 if (!file_exists(dirname($cache))) {
602 mkdir(dirname($cache),0700, true);
605 require_once 'HTTP/Request.php';
607 $real_url = str_replace(' ', '%20', $this->mapurl($url));
608 $a = new HTTP_Request($real_url);
610 $data = $a->getResponseBody();
612 $this->log("got file of size " . strlen($data));
613 $this->log("save contentid " . md5($url));
615 file_put_contents($cache .'.data', $data);
618 $mt = $a->getResponseHeader('Content-Type');
620 require_once 'File/MimeType.php';
621 $m = new File_MimeType();
622 $ext = $m->toExt($mt);
627 'contentid' => md5($url)
631 file_put_contents($cache, json_encode($ret));
632 $ret['file'] = $cache . '.data';
638 foreach($this->urlmap as $o=>$n) {
639 if (strpos($in,$o) === 0) {
640 $ret =$n . substr($in,strlen($o));
641 $this->log("mapURL in $in = $ret");
645 $this->log("mapurl no change - $in");
658 if ($this->debug < 2) {
659 echo '<PRE>' . print_r($val,true). "\n";
662 $fh = fopen('/tmp/core_mailer.log', 'a');
663 fwrite($fh, date('Y-m-d H:i:s -') . json_encode($val) . "\n");