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