DataObjects/pman.links.ini
[Pman.Core] / Mailer.php
index 42b66ab..32c934c 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..
  *
@@ -29,7 +45,7 @@ class Pman_Core_Mailer {
     var $contents       = false; /* object or array */
     var $template       = false; /* string */
     var $replaceImages  = false; /* boolean */
-    
+    var $rcpts   = false;
     
     var $images         = array(); // generated list of cid images for sending
     
@@ -51,7 +67,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;
@@ -75,18 +91,18 @@ class Pman_Core_Mailer {
         require_once 'HTML/Template/Flexy.php';
         
         $htmlbody = false;
-        
-        if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
+        $htmltemplate = new HTML_Template_Flexy(  );
+
+        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.
             
-            $this->htmlbodytoCID($htmlbody);
+            $htmlbody = $this->htmlbodytoCID($htmlbody);
             
              
         }
@@ -143,28 +159,18 @@ class Pman_Core_Mailer {
             
             foreach($this->images as $cid=>$cdata) { 
             
-                $mime->addAttachment(
+                $mime->addHTMLImage(
                     $cdata['file'],
-                    $cdata['mimetype'],
-                    $name        = $cid.'.'.$cdata['ext'],
-                    $isfile      = true,
-                    $encoding    = 'base64',
-                    $disposition = 'attachment',
-                    $charset     = '',
-                    $language    = '',
-                    $location    = '',
-                    $n_encoding  = null,
-                    $f_encoding  = null,
-                    $description = '',
-                    $h_charset   = null
+                     $cdata['mimetype'],
+                     $cid.'.'.$cdata['ext'],
+                    true,
+                    $cdata['contentid']
+                );
             }
             $parts[2] = $mime->get();
             $parts[1] = $mime->headers($parts[1]);
         
-
-        
-        
-        
+        }
         
         
         
@@ -185,12 +191,13 @@ class Pman_Core_Mailer {
         ///$recipents = array($this->email);
         $mailOptions = PEAR::getStaticProperty('Mail','options');
         $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;
         $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;
@@ -203,11 +210,11 @@ class Pman_Core_Mailer {
         $imgs= $dom->getElementsByTagName('img');
         
         foreach ($imgs as $i=>$img) {
-            $url  = $dom->getAttribute('src');
+            $url  = $img->getAttribute('src');
             $conv = $this->fetchImage($url);
             $this->images[$conv['contentid']] = $conv;
             
-            $url->setAttribute('src', 'cid:' . $conv['contentid']);
+            $img->setAttribute('src', 'cid:' . $conv['contentid']);
             
             
         }
@@ -218,6 +225,44 @@ class Pman_Core_Mailer {
     }
     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);
@@ -230,10 +275,10 @@ class Pman_Core_Mailer {
             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());
         
         $mt = $a->getResponseHeader('Content-Type');
         
@@ -249,9 +294,9 @@ class Pman_Core_Mailer {
         );
         
         file_put_contents($cache, json_encode($ret));
-         $ret['file'] = $cache . '.data';
+        $ret['file'] = $cache . '.data';
         return $ret;
-        
+    }