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