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