DataObjects/Core_watch.php
[Pman.Core] / Mailer.php
index d973fcf..8ae5336 100644 (file)
@@ -20,6 +20,7 @@
        page => 
        contents
        template
+       html_locale => 'en' == always use the 'english translated verison'
        replaceImages => true|false,
        locale => 'en' .... or zh_hk....
        rcpts => array()   // override recipients..
@@ -139,7 +140,9 @@ class Pman_Core_Mailer {
             
             // for the html body, we may want to convert the attachments to images.
 //            var_dump($htmlbody);exit;
-            $htmlbody = $this->htmlbodytoCID($htmlbody);
+            if ($this->replaceImages) {
+                $htmlbody = $this->htmlbodytoCID($htmlbody);    
+            }
             
               
         }
@@ -172,7 +175,7 @@ class Pman_Core_Mailer {
             //exit;
         } 
         
-        $isMine = false;
+        $isMime = false;
         
         require_once 'Mail/mime.php';
         $mime = new Mail_mime(array(
@@ -208,39 +211,47 @@ class Pman_Core_Mailer {
                     $cdata['contentid']
                 );
             }
-            $isMine = true;
-            
-        
+            $isMime = true;
         }
         
-        if($this->attachments){
+        if(!empty($this->attachments)){
             //if got a attachments
+            $header = $mime->headers($parts[1]);
             
             if (isset($parts[1]['Content-Type'])) {
-                
                 unset($parts[1]['Content-Type']);
             }
-            $mime->setTXTBody($parts[2]);
+            
+            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'],
-                        $attch['name']
+                        (!empty($attch['name'])) ? $attch['name'] : '',
+                        true
                 );
-                
             }
             
-            print_r($mime);
-            $isMine = true;
+            $isMime = true;
         }
         
-        if($isMine){
+        if($isMime){
             $parts[2] = $mime->get();
             $parts[1] = $mime->headers($parts[1]);
         }
-        echo '<PRE>';
-        print_r('parts');
-        print_r($parts);exit;
+//        echo '<PRE>';
+//        print_r('parts');
+//        print_r($parts[2]);
+//        exit;
        // list($recipents,$headers,$body) = $parts;
         return array(
             'recipents' => $parts[0],
@@ -248,11 +259,17 @@ class Pman_Core_Mailer {
             'body' => $parts[2]
         );
     }
-    function send()
+    function send($email = false)
     {
         
-        $email = $this->toData();
+        $pg = HTML_FlexyFramework::get()->page;
+        
+        
+        $email = is_array($email)  ? $email : $this->toData();
         if (is_a($email, 'PEAR_Error')) {
+            $pg->addEvent("COREMAILER-FAIL",  false, "email toData failed"); 
+      
+            
             return $email;
         }
         if ($this->debug) {
@@ -262,8 +279,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;
@@ -275,6 +295,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;
     }
@@ -288,6 +314,9 @@ class Pman_Core_Mailer {
         
         foreach ($imgs as $i=>$img) {
             $url  = $img->getAttribute('src');
+            if (preg_match('#^cid:#', $url)) {
+                continue;
+            }
             $conv = $this->fetchImage($url);
             $this->images[$conv['contentid']] = $conv;
             
@@ -350,7 +379,7 @@ class Pman_Core_Mailer {
         
         $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 =  json_decode(file_get_contents($cache), true);
             $ret['file'] = $cache . '.data';
             return $ret;
         }