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