DataObjects/Core_watch.php
[Pman.Core] / Mailer.php
index 50beb91..4fdebf5 100644 (file)
        contents
        template
        replaceImages => true|false,
+       locale => 'en' .... or zh_hk....
        rcpts => array()   // override recipients..
+       attachments => array(
+        array(
+          file: 
+          name : (optional) - uses basename of file
+          mimetype : 
+        ), 
+        ......
+  
     ]
  *
  *  recipents is gathered from the resulting template
@@ -47,9 +56,11 @@ class Pman_Core_Mailer {
     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
-    
+    var $attachments = false;
     
     function Pman_Core_Mailer($args) {
         foreach($args as $k=>$v) {
@@ -103,16 +114,25 @@ class Pman_Core_Mailer {
         if (!empty($fopts['DB_DataObject_translator'])) {
             $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
         }
-         if (!empty($fopts['locale'])) {
+        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( $tmp_opts );
-
+        $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);
@@ -124,6 +144,9 @@ class Pman_Core_Mailer {
               
         }
         $tmp_opts['nonHTML'] = true;
+        
+        
+        //print_R($tmp_opts);
         // $tmp_opts['force'] = true;
         $template = new HTML_Template_Flexy(  $tmp_opts );
         
@@ -149,24 +172,29 @@ class Pman_Core_Mailer {
             //exit;
         } 
         
+        $isMime = false;
         
+        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",
-//                                    'html_encoding' => 'base64',
-                                    'html_charset' => 'utf-8',
-                                    'text_charset' => 'utf-8',
-                                    'head_charset' => 'utf-8'
-                ));
+            // 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;
@@ -180,11 +208,47 @@ class Pman_Core_Mailer {
                     $cdata['contentid']
                 );
             }
-            $parts[2] = $mime->get();
-            $parts[1] = $mime->headers($parts[1]);
+            $isMime = true;
+        }
         
+        if(!empty($this->attachments)){
+            //if got a attachments
+            $header = $mime->headers($parts[1]);
+            
+            if (isset($parts[1]['Content-Type'])) {
+                unset($parts[1]['Content-Type']);
+            }
+            
+            if (!$isMime) {
+            
+                if(preg_match('/text\/html/', $header['Content-Type'])){
+                    $mime->setHTMLBody($parts[2]);
+                    $mime->setTXTBody('This message is in HTML only');
+                }else{
+                    $mime->setTXTBody($parts[2]);
+                    $mime->setHTMLBody('<PRE>'.htmlspecialchars($parts[2]).'</PRE>');
+                }
+            }
+            foreach($this->attachments as $attch){
+                $mime->addAttachment(
+                        $attch['file'],
+                        $attch['mimetype'],
+                        (!empty($attch['name'])) ? $attch['name'] : '',
+                        true
+                );
+            }
+            
+            $isMime = true;
         }
         
+        if($isMime){
+            $parts[2] = $mime->get();
+            $parts[1] = $mime->headers($parts[1]);
+        }
+//        echo '<PRE>';
+//        print_r('parts');
+//        print_r($parts[2]);
+//        exit;
        // list($recipents,$headers,$body) = $parts;
         return array(
             'recipents' => $parts[0],
@@ -195,8 +259,14 @@ class Pman_Core_Mailer {
     function send()
     {
         
+        $pg = HTML_FlexyFramework::get()->page;
+        
+        
         $email = $this->toData();
         if (is_a($email, 'PEAR_Error')) {
+            $pg->addEvent("COREMAILER-FAIL",  false, "email toData failed"); 
+      
+            
             return $email;
         }
         if ($this->debug) {
@@ -206,8 +276,11 @@ class Pman_Core_Mailer {
         $mailOptions = PEAR::getStaticProperty('Mail','options');
         //print_R($mailOptions);exit;
         $mail = Mail::factory("SMTP",$mailOptions);
-        $headers['Date'] = date('r'); 
+        $email['headers']['Date'] = date('r'); 
         if (PEAR::isError($mail)) {
+             $pg->addEvent("COREMAILER-FAIL",  false, "mail factory failed"); 
+      
+            
             return $mail;
         } 
         $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
@@ -219,6 +292,12 @@ class Pman_Core_Mailer {
         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
         $ret = $mail->send($rcpts,$email['headers'],$email['body']);
         error_reporting($oe);
+        if ($ret === true) { 
+            $pg->addEvent("COREMAILER-SENT",  false,
+                'To: ' .  ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
+                'Subject: '  . @$email['headers']['Subject']
+            ); 
+        }  
        
         return $ret;
     }
@@ -277,10 +356,10 @@ 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
             );
             
         }