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