5 * code that used to be in Pman (sendTemplate / emailTemplate)
10 * $x= new Pman_Core_Mailer($opts)
16 replaceImages => true|false
19 * $x->asData(); // returns data needed for notify?? - notify should really
20 * // just use this to pass around later..
26 class Pman_Core_Mailer {
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 */
34 var $images = array(); // generated list of cid images for sending
37 function Pman_Core_Mailer($args) {
38 foreach($args as $k=>$v) {
45 * ---------------- Global Tools ---------------
51 $templateFile = $this->template;
52 $args = $this->contents;
54 $content = clone($page);
56 foreach((array)$args as $k=>$v) {
60 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
62 $ff = HTML_FlexyFramework::get();
63 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
64 if (isset($ff->Pman['HTTP_HOST'])) {
65 $http_host = $ff->Pman['HTTP_HOST'];
69 $content->HTTP_HOST = $http_host;
73 // this should be done by having multiple template sources...!!!
75 require_once 'HTML/Template/Flexy.php';
79 if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
80 // then we have a multi-part email...
83 $htmltemplate = new HTML_Template_Flexy( );
84 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
85 $htmlbody = $template->bufferedOutputObject($content);
87 // for the html body, we may want to convert the attachments to images.
89 $this->htmlbodytoCID($htmlbody);
96 $template = new HTML_Template_Flexy( array(
100 $template->compile('mail/'. $templateFile.'.txt');
102 /* use variables from this object to ouput data. */
103 $mailtext = $template->bufferedOutputObject($content);
114 //echo "<PRE>";print_R($mailtext);
116 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
117 require_once 'Mail/mimeDecode.php';
118 require_once 'Mail.php';
120 $decoder = new Mail_mimeDecode($mailtext);
121 $parts = $decoder->getSendArray();
122 if (PEAR::isError($parts)) {
124 //echo "PROBLEM: {$parts->message}";
131 $parts[1]['Message-Id'] = '<' . $content->msgid .
132 '@' . $content->HTTP_HOST .'>';
137 if ($htmlbody !== false) {
138 require_once 'Mail/mime.php';
139 $mime = new Mail_mime(array('eol' => "\n"));
141 $mime->setTXTBody($parts[2]);
142 $mime->setHTMLBody($htmlbody);
144 foreach($this->images as $cid=>$cdata) {
146 $mime->addAttachment($cdata['file'], 'text/plain');
148 $parts[2] = $mime->get();
149 $parts[1] = $mime->headers($parts[1]);
158 // list($recipents,$headers,$body) = $parts;
160 'recipents' => $parts[0],
161 'headers' => $parts[1],
168 $email = $this->toData();
169 if (is_a($email, 'PEAR_Error')) {
172 ///$recipents = array($this->email);
173 $mailOptions = PEAR::getStaticProperty('Mail','options');
174 $mail = Mail::factory("SMTP",$mailOptions);
175 $headers['Date'] = date('r');
176 if (PEAR::isError($mail)) {
179 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
180 $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
181 error_reporting($oe);
186 function htmlbodytoCID($html)
188 $dom = new DOMDocument;
189 $dom->loadHTML($html);
190 $imgs= $dom->getElementsByTagName('img');
192 foreach ($imgs as $i=>$img) {
193 $url = $dom->getAttribute('src');
194 $conv = $this->fetchImage($url);
195 $this->images[$conv['contentid']] = $conv;
197 $url->setAttribute('src', 'cid:' . $conv['contentid']);
201 return $dom->saveHTML();
206 function fetchImage($url)
209 // 2 files --- the info file.. and the actual file...
210 $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
211 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
212 $ret = json_decode($cache);
213 $ret['file'] = $cache . '.data';
216 if (!file_exists(dirname($cache))) {
217 mkdir(dirname($cache),0666, true);
221 $a = &new HTTP_Request($url);
223 file_put_contents($cache .'.data', base64_encode( $a->getResponseBody()));
225 $mt = $a->getResponseHeader('Content-Type');
227 require_once 'File/MimeType.php';
228 $m = new File_MimeType();
229 $ext = $m->toExt($mt);
234 'contentid' => md5($url)
238 file_put_contents($cache, json_encode($ret));
239 $ret['file'] = $cache . '.data';