DataObjects/Core_notify.php
[Pman.Core] / Mailer.php
index 353b309..d71b171 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,
+       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 $images         = array(); // generated list of cid images for sending
+    
     
     function Pman_Core_Mailer($args) {
         foreach($args as $k=>$v) {
@@ -47,7 +68,7 @@ class Pman_Core_Mailer {
         $templateFile = $this->template;
         $args = $this->contents;
         
-        $content  = clone($page);
+        $content  = clone($this->page);
         
         foreach((array)$args as $k=>$v) {
             $content->$k = $v;
@@ -70,37 +91,38 @@ class Pman_Core_Mailer {
         
         require_once 'HTML/Template/Flexy.php';
         
-        $htmlbody = false;
+        $tmp_opts = array();
+        if (!empty($this->templateDir)) {
+            $tmp_opts['templateDir'] = $this->templateDir;
+        }
+        
         
-        if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
+        $htmlbody = false;
+        $htmltemplate = new HTML_Template_Flexy( $tmp_opts );
+
+        if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
             // then we have a multi-part email...
             
             
-            $htmltemplate = new HTML_Template_Flexy(  );
             $htmltemplate->compile('mail/'. $templateFile.'.body.html');
-            $htmlbody =  $template->bufferedOutputObject($content);
+            $htmlbody =  $htmltemplate->bufferedOutputObject($content);
             
             // for the html body, we may want to convert the attachments to images.
             
+            $htmlbody = $this->htmlbodytoCID($htmlbody);
             
-             
-        } 
-        $template = new HTML_Template_Flexy( array(
-                'nonHTML' => true,
-        ));
+              
+        }
+        $tmp_opts['nonHTML'] = true;
+        // $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);
@@ -124,6 +146,30 @@ class Pman_Core_Mailer {
                                      '@' . $content->HTTP_HOST .'>';
         
         
+        
+        
+        if ($htmlbody !== false) {
+            require_once 'Mail/mime.php';
+            $mime = new Mail_mime(array('eol' => "\n"));
+            
+            $mime->setTXTBody($parts[2]);
+            $mime->setHTMLBody($htmlbody);
+            
+            foreach($this->images as $cid=>$cdata) { 
+            
+                $mime->addHTMLImage(
+                    $cdata['file'],
+                     $cdata['mimetype'],
+                     $cid.'.'.$cdata['ext'],
+                    true,
+                    $cdata['contentid']
+                );
+            }
+            $parts[2] = $mime->get();
+            $parts[1] = $mime->headers($parts[1]);
+        
+        }
+        
        // list($recipents,$headers,$body) = $parts;
         return array(
             'recipents' => $parts[0],
@@ -138,15 +184,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;
@@ -159,38 +215,93 @@ class Pman_Core_Mailer {
         $imgs= $dom->getElementsByTagName('img');
         
         foreach ($imgs as $i=>$img) {
-            $dom->
+            $url  = $img->getAttribute('src');
+            $conv = $this->fetchImage($url);
+            $this->images[$conv['contentid']] = $conv;
+            
+            $img->setAttribute('src', 'cid:' . $conv['contentid']);
             
             
         }
+        return $dom->saveHTML();
+        
         
         
     }
     function fetchImage($url)
     {
+        
+        if ($url[0] == '/') {
+            $file = $this->page->rootDir . $url;
+            require_once 'File/MimeType.php';
+            $m  = new File_MimeType();
+            $mt = $m->fromFilename($file);
+            $ext = $m->toExt($mt); 
+            
+            return array(
+                    'mimetype' => $mt,
+                   'ext' => $ext,
+                   'contentid' => md5($file),
+                   'file' => $file
+            );
+            
+            
+            
+        }
+        
+        //print_R($url); exit;
+        
+        
+        if (preg_match('#^file:///#', $url)) {
+            $file = preg_replace('#^file://#', '', $url);
+            require_once 'File/MimeType.php';
+            $m  = new File_MimeType();
+            $mt = $m->fromFilename($file);
+            $ext = $m->toExt($mt); 
+            
+            return array(
+                    '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);
         if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
-            return json_decode($cache);
+            $ret =  json_decode($cache);
+            $ret['file'] = $cache . '.data';
+            return $ret;
         }
         if (!file_exists(dirname($cache))) {
             mkdir(dirname($cache),0666, true);
         }
         
-        
-        $a = &new HTTP_Request($url);
+        require_once 'HTTP/Request.php';
+        $a = new HTTP_Request($url);
         $a->sendRequest();
-        file_put_contents($cache .'.data', base64_encode( $a->getResponseBody()));
+        file_put_contents($cache .'.data', $a->getResponseBody());
         
-        $a->getResponseHeader('Content-Type');
+        $mt = $a->getResponseHeader('Content-Type');
         
         require_once 'File/MimeType.php';
-        $m  = new File_MimeType;
-        
+        $m  = new File_MimeType();
+        $ext = $m->toExt($mt);
         
+        $ret = array(
+            'mimetype' => $mt,
+            'ext' => $ext,
+            'contentid' => md5($url)
+            
+        );
         
-    }
+        file_put_contents($cache, json_encode($ret));
+        $ret['file'] = $cache . '.data';
+        return $ret;
+    }