Pman.Dialog.Image.bjs
[Pman.Core] / Mailer.php
index 2a46d47..dd5cfe0 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..
@@ -172,6 +173,8 @@ class Pman_Core_Mailer {
             //exit;
         } 
         
+        $isMime = false;
+        
         require_once 'Mail/mime.php';
         $mime = new Mail_mime(array(
             'eol' => "\n",
@@ -206,71 +209,47 @@ class Pman_Core_Mailer {
                     $cdata['contentid']
                 );
             }
-            $parts[2] = $mime->get();
-            $parts[1] = $mime->headers($parts[1]);
-        
+            $isMime = true;
         }
-//        echo '<PRE>';
-//        print_r('parts');
-//        print_r($parts);exit;
-        if($this->attachments){
+        
+        if(!empty($this->attachments)){
             //if got a attachments
-            $contentType = 'Content-Type: text/plain; charset=utf-8';
+            $header = $mime->headers($parts[1]);
+            
             if (isset($parts[1]['Content-Type'])) {
-                $contentType = $parts[1]['Content-Type'];
                 unset($parts[1]['Content-Type']);
             }
-            $random_hash = md5(date('r', time()));
-            $parts[1]['Content-Type'] = "multipart/mixed; boundary=mixed-$random_hash";
             
-$str = "
---mixed-$random_hash
-Content-Type: multipart/alternative; boundary=alt-$random_hash
-
---alt-$random_hash
-Content-Type: $contentType
-
-{$parts[2]}
-
---alt-$random_hash--
-
-";
-
-foreach($this->attachments as $attch){
-$str .= "
---mixed-$random_hash
-Content-Type: {$attch['mimetype']}; ".
-((empty($attch['name'])) ? '' : "name=\"{$attch['name']}\"") .
-"Content-Transfer-Encoding: base64 
-Content-Disposition: attachment
-
-{$attch['file']}
-";
-}
-            $str .= "--mixed-$random_hash--";
-
-            $parts[2] = $str;
-//            --mixed-{t.random_hash}
-//Content-Type: multipart/alternative; boundary=alt-{t.random_hash}
-//
-//--alt-{t.random_hash}
-//Content-Type: text/plain; charset=utf-8
-//Content-Transfer-Encoding: 7bit
-//
-//{t.msg}
-//
-//--alt-{t.random_hash}--
-//
-//--mixed-{t.random_hash}
-//Content-Type: application/pdf; name="doc.pdf"
-//Content-Transfer-Encoding: base64 
-//Content-Disposition: attachment
-//
-//{t.attach}
-//--mixed-{t.random_hash}--
+            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],
@@ -278,11 +257,17 @@ Content-Disposition: attachment
             '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) {
@@ -292,8 +277,11 @@ Content-Disposition: attachment
         $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;
@@ -305,6 +293,12 @@ Content-Disposition: attachment
         $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;
     }