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