Mailer.php
[Pman.Core] / Mailer.php
1 <?php
2
3 /**
4  *
5  *  code that used to be in Pman (sendTemplate / emailTemplate)
6  * 
7  *  template is in template directory subfolder 'mail'
8  *   
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 
11  *     the html body
12  * 
13  *
14  *  usage:
15  *
16  *
17  *  $x= new Pman_Core_Mailer($opts)
18  *
19  *  $x= Pman_Core_Mailer(array(
20        'page' => $this,
21                 // if bcc is property of this, then it will be used (BAD DESIGN)
22        'rcpts' => array(),   // override recipients..
23        //'bcc' => array(),
24        'contents'  => array(),    //  << keys must be trusted
25                                 // if bcc is property of contents, then it will be used (BAD DESIGN)
26        'template' => 'your_template.html', 
27        'html_locale => 'en',              // always use the 'english translated verison'
28        'cache_images => true,           // -- defaults to caching images - set to false to disable.
29        'replaceImages => false,         // should images be replaced.
30         //'urlmap => array(             // map urls from template to a different location.
31       //      'https://www.mysite.com/' => 'http://localhost/'
32        //)
33        'locale' => 'en', // .... or zh_hk....
34        
35        'attachments' => array(
36             array(
37               'file' => '/path/to/file', // file location
38               name => 'myfile.pdf', // (optional) - uses basename of file
39               mimetype : 
40             ), 
41         
42         //'mail_method' =>  'SMTP', // or SMTPMX
43   
44     )
45  *
46  *  recipents is gathered from the resulting template
47  *   -- eg.
48  *    To: <a>,<b>,<c>
49  * 
50  * 
51  *  if the file     '
52  * 
53  * 
54  *  $x->toData(); // returns data needed for notify?? - notify should really
55  *                  // just use this to pass around later..
56  *
57  *  $x->send();
58  *
59  */
60
61 class Pman_Core_Mailer {
62     var $debug          = 0;
63     var $page           = false; /* usually a html_flexyframework_page */
64     var $contents       = false; /* object or array */
65     var $template       = false; /* string */
66     var $replaceImages  = false; /* boolean */
67     var $rcpts   = false;
68     var $templateDir = false;
69     var $locale = false; // eg. 'en' or 'zh_HK'
70     var $urlmap = array();
71     
72     
73     var $html_locale = false; // eg. 'en' or 'zh_HK'
74     var $images         = array(); // generated list of cid images for sending
75     var $attachments = false;
76     var $css_inline = false; // not supported
77     var $css_embed = false; // put the css tags into the body.
78     
79     var $mail_method = 'SMTP';
80     
81     var $cache_images = true;
82       
83     var $bcc = false;
84     
85     function Pman_Core_Mailer($args) {
86         foreach($args as $k=>$v) {
87             // a bit trusting..
88             $this->$k =  $v;
89         }
90         // allow core mailer debug setting.
91         $ff = HTML_FlexyFramework::get();
92         
93         if (!empty($ff->Core_Mailer['debug'])) {
94             $this->debug = $ff->Core_Mailer['debug'];
95         }
96         //$this->log("URL MAP");
97         //$this->log($this->urlmap);
98         
99     }
100      
101     /**
102      * ---------------- Global Tools ---------------   
103      */
104     
105     function toData()
106     {
107     
108         $templateFile = $this->template;
109         $args = (array)$this->contents;
110         
111         $content  = clone($this->page);
112         
113         foreach($args as $k=>$v) {
114             $content->$k = $v;
115         }
116         
117         $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
118         
119         $ff = HTML_FlexyFramework::get();
120         $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
121         if (isset($ff->Pman['HTTP_HOST'])) {
122             $http_host  = $ff->Pman['HTTP_HOST'];
123         }
124         
125         
126         $content->HTTP_HOST = $http_host;
127         
128         
129         
130         
131         // this should be done by having multiple template sources...!!!
132         
133         require_once 'HTML/Template/Flexy.php';
134         
135         $tmp_opts = array(
136            // 'forceCompile' => true,
137             'site_prefix' => false,
138         );
139         if (!empty($this->templateDir)) {
140             $tmp_opts['templateDir'] = $this->templateDir;
141         }
142         $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
143         if (!empty($fopts['DB_DataObject_translator'])) {
144             $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
145         }
146         if (!empty($fopts['locale'])) {
147             $tmp_opts['locale'] = $fopts['locale'];
148         }
149         
150         // local opt's overwrite
151         if (!empty($this->locale)) {
152             $tmp_opts['locale'] = $this->locale;
153         }
154         
155         $htmlbody = false;
156         $html_tmp_opts = $tmp_opts;
157         $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
158         if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
159             // then we have a multi-part email...
160             
161             if (!empty($this->html_locale)) {
162                 $html_tmp_opts['locale'] = $this->html_locale;
163             }
164             $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
165             
166             $htmltemplate->compile('mail/'. $templateFile.'.body.html');
167             $htmlbody =  $htmltemplate->bufferedOutputObject($content);
168             
169             $this->htmlbody = $htmlbody;
170             
171             // for the html body, we may want to convert the attachments to images.
172 //            var_dump($htmlbody);exit;
173             if ($this->replaceImages) {
174                 $htmlbody = $this->htmlbodytoCID($htmlbody);    
175             }
176             if ($this->css_embed) {
177                 $htmlbody = $this->htmlbodyCssEmbed($htmlbody);    
178               
179             }
180         }
181         $tmp_opts['nonHTML'] = true;
182         
183         
184         //print_R($tmp_opts);
185         // $tmp_opts['force'] = true;
186         $template = new HTML_Template_Flexy(  $tmp_opts );
187         
188         $template->compile('mail/'. $templateFile.'.txt');
189         
190         /* use variables from this object to ouput data. */
191         $mailtext = $template->bufferedOutputObject($content);
192         //print_r($mailtext);exit;
193        
194         
195         
196         //echo "<PRE>";print_R($mailtext);
197         
198         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
199         require_once 'Mail/mimeDecode.php';
200         require_once 'Mail.php';
201         
202         $decoder = new Mail_mimeDecode($mailtext);
203         $parts = $decoder->getSendArray();
204         if (PEAR::isError($parts)) {
205             return $parts;
206             //echo "PROBLEM: {$parts->message}";
207             //exit;
208         } 
209         
210         $isMime = false;
211         
212         require_once 'Mail/mime.php';
213         $mime = new Mail_mime(array(
214             'eol' => "\n",
215             //'html_encoding' => 'base64',
216             'html_charset' => 'utf-8',
217             'text_charset' => 'utf-8',
218             'head_charset' => 'utf-8',
219         ));
220         // clean up the headers...
221         
222         
223         $parts[1]['Message-Id'] = '<' .   $content->msgid   .
224                                      '@' . $content->HTTP_HOST .'>';
225         
226           
227         if ($htmlbody !== false) {
228             // got a html headers...
229             
230             if (isset($parts[1]['Content-Type'])) {
231                 unset($parts[1]['Content-Type']);
232             }
233             $mime->setTXTBody($parts[2]);
234             $mime->setHTMLBody($htmlbody);
235 //            var_dump($mime);exit;
236             foreach($this->images as $cid=>$cdata) { 
237             
238                 $mime->addHTMLImage(
239                     $cdata['file'],
240                      $cdata['mimetype'],
241                      $cid.'.'.$cdata['ext'],
242                     true,
243                     $cdata['contentid']
244                 );
245             }
246             $isMime = true;
247         }
248         
249         if(!empty($this->attachments)){
250             //if got a attachments
251             $header = $mime->headers($parts[1]);
252             
253             if (isset($parts[1]['Content-Type'])) {
254                 unset($parts[1]['Content-Type']);
255             }
256             
257             if (!$isMime) {
258             
259                 if(preg_match('/text\/html/', $header['Content-Type'])){
260                     $mime->setHTMLBody($parts[2]);
261                     $mime->setTXTBody('This message is in HTML only');
262                 }else{
263                     $mime->setTXTBody($parts[2]);
264                     $mime->setHTMLBody('<PRE>'.htmlspecialchars($parts[2]).'</PRE>');
265                 }
266             }
267             foreach($this->attachments as $attch){
268                 $mime->addAttachment(
269                         $attch['file'],
270                         $attch['mimetype'],
271                         (!empty($attch['name'])) ? $attch['name'] : '',
272                         true
273                 );
274             }
275             
276             $isMime = true;
277         }
278         
279         if($isMime){
280             $parts[2] = $mime->get();
281             $parts[1] = $mime->headers($parts[1]);
282         }
283         $ret = array(
284             'recipents' => $parts[0],
285             'headers' => $parts[1],
286             'body' => $parts[2],
287             'mailer' => $this
288         );
289         // add bcc if necessary..
290         if (!empty($this->bcc)) {
291            $ret['bcc'] = $this->bcc;
292         }
293         return $ret;
294     }
295     function send($email = false)
296     {
297         
298         $pg = HTML_FlexyFramework::get()->page;
299         
300         
301         $email = is_array($email)  ? $email : $this->toData();
302         if (is_a($email, 'PEAR_Error')) {
303             $pg->addEvent("COREMAILER-FAIL",  false, "email toData failed"); 
304       
305             
306             return $email;
307         }
308         
309         //$this->log( htmlspecialchars(print_r($email,true)));
310         
311         ///$recipents = array($this->email);
312         $mailOptions = PEAR::getStaticProperty('Mail','options');
313         //print_R($mailOptions);exit;
314         
315         if ($this->mail_method == 'SMTPMX' && empty($mailOptions['mailname'])) {
316             $pg->jerr("Mail[mailname] is not set - this is required for SMTPMX");
317             
318         }
319         
320         $mail = Mail::factory($this->mail_method,$mailOptions);
321         if ($this->debug) {
322             $mail->debug = (bool) $this->debug;
323         }
324         
325         $email['headers']['Date'] = date('r'); 
326         if (PEAR::isError($mail)) {
327             $pg->addEvent("COREMAILER-FAIL",  false, "mail factory failed"); 
328       
329             
330             return $mail;
331         } 
332         $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
333         
334         
335         
336         // this makes contents untrustable...
337         if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
338             $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
339         }
340         
341         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
342         $ret = $mail->send($rcpts,$email['headers'],$email['body']);
343         error_reporting($oe);
344         if ($ret === true) { 
345             $pg->addEvent("COREMAILER-SENT",  false,
346                 'To: ' .  ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
347                 'Subject: '  . @$email['headers']['Subject']
348             ); 
349         }  else {
350             $pg->addEvent("COREMAILER-FAIL",  false, $ret->toString());
351         }
352         
353         return $ret;
354     }
355     
356     function htmlbodytoCID($html)
357     {
358         $dom = new DOMDocument();
359         // this may raise parse errors as some html may be a component..
360         @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
361         $imgs= $dom->getElementsByTagName('img');
362         
363         foreach ($imgs as $i=>$img) {
364             $url  = $img->getAttribute('src');
365             if (preg_match('#^cid:#', $url)) {
366                 continue;
367             }
368             $me = $img->getAttribute('mailembed');
369             if ($me == 'no') {
370                 continue;
371             }
372             
373             $conv = $this->fetchImage($url);
374             $this->images[$conv['contentid']] = $conv;
375             
376             $img->setAttribute('src', 'cid:' . $conv['contentid']);
377             
378             
379         }
380         return $dom->saveHTML();
381         
382         
383         
384     }
385     function htmlbodyCssEmbed($html)
386     {
387         $ff = HTML_FlexyFramework::get();
388         $dom = new DOMDocument();
389         
390         // this may raise parse errors as some html may be a component..
391         @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
392         $links = $dom->getElementsByTagName('link');
393         $lc = array();
394         foreach ($links as $link) {  // duplicate as links is dynamic and we change it..!
395             $lc[] = $link;
396         }
397         //<link rel="stylesheet" type="text/css" href="{rootURL}/roojs1/css-mailer/mailer.css">
398         
399         foreach ($lc as $i=>$link) {
400             //var_dump($link->getAttribute('href'));
401             
402             if ($link->getAttribute('rel') != 'stylesheet') {
403                 continue;
404             }
405             $url  = $link->getAttribute('href');
406             $file = $ff->rootDir . $url;
407             
408             if (!preg_match('#^(http|https)://#', $url)) {
409                 $file = $ff->rootDir . $url;
410
411                 if (!file_exists($file)) {
412 //                    echo $file;
413                     $link->setAttribute('href', 'missing:' . $file);
414                     continue;
415                 }
416             } else {
417                $file = $this->mapurl($url);  
418             }
419             
420             $par = $link->parentNode;
421             $par->removeChild($link);
422             $s = $dom->createElement('style');
423             $e = $dom->createTextNode(file_get_contents($file));
424             $s->appendChild($e);
425             $par->appendChild($s);
426             
427         }
428         return $dom->saveHTML();
429         
430         
431     }
432     
433     
434     
435     function fetchImage($url)
436     {
437         
438         
439         $this->log( "FETCH : $url\n");
440         
441         if ($url[0] == '/') {
442             $ff = HTML_FlexyFramework::get();
443             $file = $ff->rootDir . $url;
444             require_once 'File/MimeType.php';
445             $m  = new File_MimeType();
446             $mt = $m->fromFilename($file);
447             $ext = $m->toExt($mt); 
448             
449             return array(
450                     'mimetype' => $mt,
451                    'ext' => $ext,
452                    'contentid' => md5($file),  // mailer makes md5 cid's' -- cid with attachment-** are done by mailer.
453                    'file' => $file
454             );
455             
456             
457             
458         }
459         
460         //print_R($url); exit;
461         
462         
463         if (preg_match('#^file:///#', $url)) {
464             $file = preg_replace('#^file://#', '', $url);
465             require_once 'File/MimeType.php';
466             $m  = new File_MimeType();
467             $mt = $m->fromFilename($file);
468             $ext = $m->toExt($mt); 
469             
470             return array(
471                 'mimetype'  => $mt,
472                 'ext'       =>   $ext,
473                 'contentid' => md5($file),
474                 'file'      => $file
475             );
476             
477         }
478         
479         // CACHE???
480         // 2 files --- the info file.. and the actual file...
481         // add user
482         // unix only...
483         $uinfo = posix_getpwuid( posix_getuid () ); 
484         $user = $uinfo['name']; 
485         
486         $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
487         if ($this->cache_images &&
488                 file_exists($cache) &&
489                 filemtime($cache) > strtotime('NOW - 1 WEEK')
490             ) {
491             $ret =  json_decode(file_get_contents($cache), true);
492             $this->log("fetched from cache");
493             $ret['file'] = $cache . '.data';
494             return $ret;
495         }
496         if (!file_exists(dirname($cache))) {
497             mkdir(dirname($cache),0700, true);
498         }
499         
500         require_once 'HTTP/Request.php';
501         $a = new HTTP_Request($this->mapurl($url));
502         $a->sendRequest();
503         $data = $a->getResponseBody();
504         
505         $this->log("got file of size " . strlen($data));
506         $this->log("save contentid " . md5($url));
507         
508         file_put_contents($cache .'.data', $data);
509         
510         
511         $mt = $a->getResponseHeader('Content-Type');
512         
513         require_once 'File/MimeType.php';
514         $m  = new File_MimeType();
515         $ext = $m->toExt($mt);
516         
517         $ret = array(
518             'mimetype' => $mt,
519             'ext' => $ext,
520             'contentid' => md5($url)
521             
522         );
523         
524         file_put_contents($cache, json_encode($ret));
525         $ret['file'] = $cache . '.data';
526         return $ret;
527     }  
528     
529     function mapurl($in)
530     {
531         
532         foreach($this->urlmap as $o=>$n) {
533             if (strpos($in,$o) === 0) {
534                 $ret =$n . substr($in,strlen($o));
535                 $this->log("mapURL in $in = $ret");
536                 return $ret;
537             }
538         }
539         $this->log("mapurl no change - $in");
540         return $in;
541          
542         
543     }
544  
545     
546     
547     function log($val)
548     {
549         if (!$this->debug) {
550             return;
551         }
552         if ($this->debug < 2) {
553             echo '<PRE>' . print_r($val,true). "\n"; 
554             return;
555         }
556         $fh = fopen('/tmp/core_mailer.log', 'a');
557         fwrite($fh, date('Y-m-d H:i:s -') . json_encode($val) . "\n");
558         fclose($fh);
559         
560         
561     }
562     
563 }