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