DataObjects/Core_watch.php
[Pman.Core] / Mailer.php
index fc30d0b..4fdebf5 100644 (file)
        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
@@ -164,6 +172,8 @@ class Pman_Core_Mailer {
             //exit;
         } 
         
+        $isMime = false;
+        
         require_once 'Mail/mime.php';
         $mime = new Mail_mime(array(
             'eol' => "\n",
@@ -198,19 +208,47 @@ class Pman_Core_Mailer {
                     $cdata['contentid']
                 );
             }
-            $parts[2] = $mime->get();
-            $parts[1] = $mime->headers($parts[1]);
-        
+            $isMime = true;
         }
-        print_r($parts);
-        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']);
             }
+            
+            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],
@@ -221,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) {
@@ -232,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;
@@ -245,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;
     }