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             $this->htmlbodytoCID($htmlbody);
86             
87              
88         }
89         
90         
91         
92         $template = new HTML_Template_Flexy( array(
93                 'nonHTML' => true,
94         ));
95         
96         $template->compile('mail/'. $templateFile.'.txt');
97         
98         /* use variables from this object to ouput data. */
99         $mailtext = $template->bufferedOutputObject($content);
100         
101         
102         
103         
104         
105         
106         
107         
108         
109         
110         //echo "<PRE>";print_R($mailtext);
111         
112         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
113         require_once 'Mail/mimeDecode.php';
114         require_once 'Mail.php';
115         
116         $decoder = new Mail_mimeDecode($mailtext);
117         $parts = $decoder->getSendArray();
118         if (PEAR::isError($parts)) {
119             return $parts;
120             //echo "PROBLEM: {$parts->message}";
121             //exit;
122         } 
123         
124         
125         
126         
127         $parts[1]['Message-Id'] = '<' .   $content->msgid   .
128                                      '@' . $content->HTTP_HOST .'>';
129         
130         
131         
132         
133         if ($htmlbody !== false) {
134             require_once 'Mail/mime.php';
135             $mime = new Mail_mime(array('eol' => "\n"));
136             
137             $mime->setTXTBody($parts[2]);
138             $mime->setHTMLBody($htmlbody);
139             
140             foreach($)
141             
142                 $mime->addAttachment($file, 'text/plain');
143
144                 $parts[2] = $mime->get();
145                 $parts[1]= $mime->headers($parts[1]);
146             
147
148         
149         
150         
151         
152         
153         
154        // list($recipents,$headers,$body) = $parts;
155         return array(
156             'recipents' => $parts[0],
157             'headers' => $parts[1],
158             'body' => $parts[2]
159         );
160     }
161     function send()
162     {
163         
164         $email = $this->toData();
165         if (is_a($email, 'PEAR_Error')) {
166             return $email;
167         }
168         ///$recipents = array($this->email);
169         $mailOptions = PEAR::getStaticProperty('Mail','options');
170         $mail = Mail::factory("SMTP",$mailOptions);
171         $headers['Date'] = date('r');
172         if (PEAR::isError($mail)) {
173             return $mail;
174         } 
175         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
176         $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
177         error_reporting($oe);
178        
179         return $ret;
180     }
181     
182     function htmlbodytoCID($html)
183     {
184         $dom = new DOMDocument;
185         $dom->loadHTML($html);
186         $imgs= $dom->getElementsByTagName('img');
187         
188         foreach ($imgs as $i=>$img) {
189             $url  = $dom->getAttribute('src');
190             $conv = $this->fetchImage($url);
191             $this->images[$conv['contentid']] = $conv;
192             
193             $url->setAttribute('src', 'cid:' . $conv['contentid']);
194             
195             
196         }
197         return $dom->saveHTML();
198         
199         
200         
201     }
202     function fetchImage($url)
203     {
204         // CACHE???
205         // 2 files --- the info file.. and the actual file...
206         $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
207         if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
208             $ret =  json_decode($cache);
209             $ret['file'] = $cache . '.data';
210         }
211         if (!file_exists(dirname($cache))) {
212             mkdir(dirname($cache),0666, true);
213         }
214         
215         
216         $a = &new HTTP_Request($url);
217         $a->sendRequest();
218         file_put_contents($cache .'.data', base64_encode( $a->getResponseBody()));
219         
220         $mt = $a->getResponseHeader('Content-Type');
221         
222         require_once 'File/MimeType.php';
223         $m  = new File_MimeType();
224         $ext = $m->toExt($mt);
225         
226         file_put_contents($cache, json_encode(array(
227             'mimetype' => $mt,
228             'ext' => $ext,
229             'contentid' => md5($url);
230             
231         )));
232         
233     
234     
235     
236 }