DataObjects/core.sql
[Pman.Core] / Mailer.php
index 6e9114f..b62ed9a 100644 (file)
@@ -3,6 +3,13 @@
 /**
  *
  *  code that used to be in Pman (sendTemplate / emailTemplate)
+ * 
+ *  template is in template directory subfolder 'mail'
+ *   
+ *  eg. use 'welcome' as template -> this will use templates/mail/welcome.txt
+ *  if you also have templates/mail/welcome.body.html - then that will be used as 
+ *     the html body
+ * 
  *
  *  usage:
  *
        page => 
        contents
        template
-       replaceImages => true|false
+       replaceImages => true|false,
+       locale => 'en' .... or zh_hk....
+       rcpts => array()   // override recipients..
     ]
  *
+ *  recipents is gathered from the resulting template
+ *   -- eg.
+ *    To: <a>,<b>,<c>
+ * 
+ * 
+ *  if the file     '
+ * 
+ * 
  *  $x->asData(); // returns data needed for notify?? - notify should really
  *                  // just use this to pass around later..
  *
  */
 
 class Pman_Core_Mailer {
-    
+    var $debug          = false;
     var $page           = false; /* usually a html_flexyframework_page */
     var $contents       = false; /* object or array */
     var $template       = false; /* string */
     var $replaceImages  = false; /* boolean */
+    var $rcpts   = false;
+    var $templateDir = false;
+    var $locale = false; // eg. 'en' or 'zh_HK'
     
-    
+    var $html_locale = false; // eg. 'en' or 'zh_HK'
     var $images         = array(); // generated list of cid images for sending
     
     
@@ -70,45 +90,64 @@ class Pman_Core_Mailer {
         
         
         
+        
         // this should be done by having multiple template sources...!!!
         
         require_once 'HTML/Template/Flexy.php';
         
+        $tmp_opts = array(
+           // 'forceCompile' => true,
+            'site_prefix' => false,
+        );
+        if (!empty($this->templateDir)) {
+            $tmp_opts['templateDir'] = $this->templateDir;
+        }
+        $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
+        if (!empty($fopts['DB_DataObject_translator'])) {
+            $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
+        }
+        if (!empty($fopts['locale'])) {
+            $tmp_opts['locale'] = $fopts['locale'];
+        }
+        
+        // local opt's overwrite
+        if (!empty($this->locale)) {
+            $tmp_opts['locale'] = $this->locale;
+        }
+        
         $htmlbody = false;
-        $htmltemplate = new HTML_Template_Flexy(  );
-
+        $html_tmp_opts = $tmp_opts;
+        $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
         if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
             // then we have a multi-part email...
             
+            if (!empty($this->html_locale)) {
+                $html_tmp_opts['locale'] = $this->html_locale;
+            }
+            $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
             
             $htmltemplate->compile('mail/'. $templateFile.'.body.html');
             $htmlbody =  $htmltemplate->bufferedOutputObject($content);
             
             // for the html body, we may want to convert the attachments to images.
+//            var_dump($htmlbody);exit;
+            $htmlbody = $this->htmlbodytoCID($htmlbody);
             
-            $this->htmlbodytoCID($htmlbody);
-            
-             
+              
         }
+        $tmp_opts['nonHTML'] = true;
         
         
-        
-        $template = new HTML_Template_Flexy( array(
-                'nonHTML' => true,
-        ));
+        //print_R($tmp_opts);
+        // $tmp_opts['force'] = true;
+        $template = new HTML_Template_Flexy(  $tmp_opts );
         
         $template->compile('mail/'. $templateFile.'.txt');
         
         /* use variables from this object to ouput data. */
         $mailtext = $template->bufferedOutputObject($content);
-        
-        
-        
-        
-        
-        
-        
-        
+        //print_r($mailtext);exit;
+       
         
         
         //echo "<PRE>";print_R($mailtext);
@@ -125,31 +164,38 @@ class Pman_Core_Mailer {
             //exit;
         } 
         
-        
+        require_once 'Mail/mime.php';
+        $mime = new Mail_mime(array(
+            'eol' => "\n",
+            //'html_encoding' => 'base64',
+            'html_charset' => 'utf-8',
+            'text_charset' => 'utf-8',
+            'head_charset' => 'utf-8',
+        ));
+        // clean up the headers...
         
         
         $parts[1]['Message-Id'] = '<' .   $content->msgid   .
                                      '@' . $content->HTTP_HOST .'>';
         
-        
-        
-        
+          
         if ($htmlbody !== false) {
-            require_once 'Mail/mime.php';
-            $mime = new Mail_mime(array('eol' => "\n"));
+            // got a html headers...
             
+            if (isset($parts[1]['Content-Type'])) {
+                unset($parts[1]['Content-Type']);
+            }
             $mime->setTXTBody($parts[2]);
             $mime->setHTMLBody($htmlbody);
-            
+//            var_dump($mime);exit;
             foreach($this->images as $cid=>$cdata) { 
             
                 $mime->addHTMLImage(
                     $cdata['file'],
                      $cdata['mimetype'],
-                    $cdata['mimetype'],
-                    $cid.'.'.$cdata['ext'],
+                     $cid.'.'.$cdata['ext'],
                     true,
-                    $cid
+                    $cdata['contentid']
                 );
             }
             $parts[2] = $mime->get();
@@ -157,8 +203,6 @@ class Pman_Core_Mailer {
         
         }
         
-        
-        
        // list($recipents,$headers,$body) = $parts;
         return array(
             'recipents' => $parts[0],
@@ -173,15 +217,25 @@ class Pman_Core_Mailer {
         if (is_a($email, 'PEAR_Error')) {
             return $email;
         }
+        if ($this->debug) {
+            echo '<PRE>';echo htmlspecialchars(print_r($email,true));
+        }
         ///$recipents = array($this->email);
         $mailOptions = PEAR::getStaticProperty('Mail','options');
+        //print_R($mailOptions);exit;
         $mail = Mail::factory("SMTP",$mailOptions);
-        $headers['Date'] = date('r');
+        $headers['Date'] = date('r'); 
         if (PEAR::isError($mail)) {
             return $mail;
         } 
+        $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
+        
+        if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
+            $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
+        }
+        
         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
-        $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
+        $ret = $mail->send($rcpts,$email['headers'],$email['body']);
         error_reporting($oe);
        
         return $ret;
@@ -189,8 +243,9 @@ class Pman_Core_Mailer {
     
     function htmlbodytoCID($html)
     {
-        $dom = new DOMDocument;
-        $dom->loadHTML($html);
+        $dom = new DOMDocument();
+        // this may raise parse errors as some html may be a component..
+        @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
         $imgs= $dom->getElementsByTagName('img');
         
         foreach ($imgs as $i=>$img) {
@@ -211,7 +266,8 @@ class Pman_Core_Mailer {
     {
         
         if ($url[0] == '/') {
-            $file = $this->page->rootDir . $url;
+            $ff = HTML_FlexyFramework::get();
+            $file = $ff->rootDir . $url;
             require_once 'File/MimeType.php';
             $m  = new File_MimeType();
             $mt = $m->fromFilename($file);
@@ -228,9 +284,9 @@ class Pman_Core_Mailer {
             
         }
         
-        print_R($url);
+        //print_R($url); exit;
+        
         
-        exit;
         if (preg_match('#^file:///#', $url)) {
             $file = preg_replace('#^file://#', '', $url);
             require_once 'File/MimeType.php';
@@ -239,24 +295,29 @@ class Pman_Core_Mailer {
             $ext = $m->toExt($mt); 
             
             return array(
-                    'mimetype' => $mt,
-                   'ext' => $ext,
-                   'contentid' => md5($file),
-                   'file' => $file
+                'mimetype'  => $mt,
+                'ext'       =>   $ext,
+                'contentid' => md5($file),
+                'file'      => $file
             );
             
         }
         
         // CACHE???
         // 2 files --- the info file.. and the actual file...
-        $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
+        // add user
+        // unix only...
+        $uinfo = posix_getpwuid( posix_getuid () ); 
+        $user = $uinfo['name']; 
+        
+        $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
         if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
             $ret =  json_decode($cache);
             $ret['file'] = $cache . '.data';
             return $ret;
         }
         if (!file_exists(dirname($cache))) {
-            mkdir(dirname($cache),0666, true);
+            mkdir(dirname($cache),0700, true);
         }
         
         require_once 'HTTP/Request.php';