Mailer.php
[Pman.Core] / Mailer.php
1 <?php
2
3 /**
4  *
5  *  code that used to be in Pman (sendTemplate / emailTemplate)
6  *
7  *  usage:
8  *
9  *
10  *  $x= new Pman_Core_Mailer($opts)
11  *
12  *  $opts[
13        page => 
14        contents
15        template
16        replaceImages => true|false
17     ]
18  *
19  *  $x->asData(); // returns data needed for notify?? - notify should really
20  *                  // just use this to pass around later..
21  *
22  *  $x->send();
23  *
24  */
25
26 class Pman_Core_Mailer {
27     
28     var $page           = false; /* usually a html_flexyframework_page */
29     var $contents       = false; /* object or array */
30     var $template       = false; /* string */
31     var $replaceImages  = false; /* boolean */
32     
33     function Pman_Core_Mailer($args) {
34         foreach($args as $k=>$v) {
35             // a bit trusting..
36             $this->$k =  $v;
37         }
38     }
39      
40     /**
41      * ---------------- Global Tools ---------------   
42      */
43     
44     function toData()
45     {
46     
47         $templateFile = $this->template;
48         $args = $this->contents;
49         
50         $content  = clone($page);
51         
52         foreach((array)$args as $k=>$v) {
53             $content->$k = $v;
54         }
55         
56         $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
57         
58         $ff = HTML_FlexyFramework::get();
59         $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
60         if (isset($ff->Pman['HTTP_HOST'])) {
61             $http_host  = $ff->Pman['HTTP_HOST'];
62         }
63         
64         
65         $content->HTTP_HOST = $http_host;
66         
67         
68         
69         // this should be done by having multiple template sources...!!!
70         
71         require_once 'HTML/Template/Flexy.php';
72         
73         $htmlbody = false;
74         
75         if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
76             // then we have a multi-part email...
77             
78             
79             $htmltemplate = new HTML_Template_Flexy(  );
80             $htmltemplate->compile('mail/'. $templateFile.'.body.html');
81             $htmlbody =  $template->bufferedOutputObject($content);
82             
83             // for the html body, we may want to convert the attachments to images.
84             
85             
86              
87         } 
88         $template = new HTML_Template_Flexy( array(
89                 'nonHTML' => true,
90         ));
91         
92         $template->compile('mail/'. $templateFile.'.txt');
93         
94         /* use variables from this object to ouput data. */
95         $mailtext = $template->bufferedOutputObject($content);
96         
97         
98         
99         
100         
101         
102         
103         
104         
105         
106         //echo "<PRE>";print_R($mailtext);
107         
108         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
109         require_once 'Mail/mimeDecode.php';
110         require_once 'Mail.php';
111         
112         $decoder = new Mail_mimeDecode($mailtext);
113         $parts = $decoder->getSendArray();
114         if (PEAR::isError($parts)) {
115             return $parts;
116             //echo "PROBLEM: {$parts->message}";
117             //exit;
118         } 
119         
120         
121         
122         
123         $parts[1]['Message-Id'] = '<' .   $content->msgid   .
124                                      '@' . $content->HTTP_HOST .'>';
125         
126         
127        // list($recipents,$headers,$body) = $parts;
128         return array(
129             'recipents' => $parts[0],
130             'headers' => $parts[1],
131             'body' => $parts[2]
132         );
133     }
134     function send()
135     {
136         
137         $email = $this->toData();
138         if (is_a($email, 'PEAR_Error')) {
139             return $email;
140         }
141         ///$recipents = array($this->email);
142         $mailOptions = PEAR::getStaticProperty('Mail','options');
143         $mail = Mail::factory("SMTP",$mailOptions);
144         $headers['Date'] = date('r');
145         if (PEAR::isError($mail)) {
146             return $mail;
147         } 
148         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
149         $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
150         error_reporting($oe);
151        
152         return $ret;
153     }
154     
155     function htmlbodytoCID($html)
156     {
157         $dom = new DOMDocument;
158         $dom->loadHTML($html);
159         $imgs= $dom->getElementsByTagName('img');
160         
161         foreach ($imgs as $i=>$img) {
162             $url  = $dom->getAttribute('src');
163             $conv = $this->fetchImage($url);
164             $this->imga
165             $url
166             
167             
168         }
169         
170         
171     }
172     function fetchImage($url)
173     {
174         // CACHE???
175         // 2 files --- the info file.. and the actual file...
176         $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
177         if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
178             $ret =  json_decode($cache);
179             $ret['file'] = $cache . '.data';
180         }
181         if (!file_exists(dirname($cache))) {
182             mkdir(dirname($cache),0666, true);
183         }
184         
185         
186         $a = &new HTTP_Request($url);
187         $a->sendRequest();
188         file_put_contents($cache .'.data', base64_encode( $a->getResponseBody()));
189         
190         $mt = $a->getResponseHeader('Content-Type');
191         
192         require_once 'File/MimeType.php';
193         $m  = new File_MimeType();
194         $ext = $m->toExt($mt);
195         
196         file_put_contents($cache, json_encode(array(
197             'mimetype' => $mt,
198             'ext' => $ext,
199             'contentid' => md5($url);
200             
201         )));
202         
203     
204     
205     
206 }