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 */
33 function Pman_Core_Mailer($args) {
34 foreach($args as $k=>$v) {
41 * ---------------- Global Tools ---------------
47 $templateFile = $this->template;
48 $args = $this->contents;
50 $content = clone($page);
52 foreach((array)$args as $k=>$v) {
56 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
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'];
65 $content->HTTP_HOST = $http_host;
69 // this should be done by having multiple template sources...!!!
71 require_once 'HTML/Template/Flexy.php';
75 if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
76 // then we have a multi-part email...
79 $htmltemplate = new HTML_Template_Flexy( );
80 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
81 $htmlbody = $template->bufferedOutputObject($content);
83 // for the html body, we may want to convert the attachments to images.
85 $this->htmlbodytoCID($htmlbody);
92 $template = new HTML_Template_Flexy( array(
96 $template->compile('mail/'. $templateFile.'.txt');
98 /* use variables from this object to ouput data. */
99 $mailtext = $template->bufferedOutputObject($content);
110 //echo "<PRE>";print_R($mailtext);
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';
116 $decoder = new Mail_mimeDecode($mailtext);
117 $parts = $decoder->getSendArray();
118 if (PEAR::isError($parts)) {
120 //echo "PROBLEM: {$parts->message}";
127 $parts[1]['Message-Id'] = '<' . $content->msgid .
128 '@' . $content->HTTP_HOST .'>';
133 if ($htmlbody !== false) {
134 require_once 'Mail/mime.php';
135 $mime = new Mail_mime(array('eol' => "\n"));
137 $mime->setTXTBody($parts[2]);
138 $mime->setHTMLBody($htmlbody);
142 $mime->addAttachment($file, 'text/plain');
144 $parts[2] = $mime->get();
145 $parts[1]= $mime->headers($parts[1]);
154 // list($recipents,$headers,$body) = $parts;
156 'recipents' => $parts[0],
157 'headers' => $parts[1],
164 $email = $this->toData();
165 if (is_a($email, 'PEAR_Error')) {
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)) {
175 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
176 $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
177 error_reporting($oe);
182 function htmlbodytoCID($html)
184 $dom = new DOMDocument;
185 $dom->loadHTML($html);
186 $imgs= $dom->getElementsByTagName('img');
188 foreach ($imgs as $i=>$img) {
189 $url = $dom->getAttribute('src');
190 $conv = $this->fetchImage($url);
191 $this->images[$conv['contentid']] = $conv;
193 $url->setAttribute('src', 'cid:' . $conv['contentid']);
197 return $dom->saveHTML();
202 function fetchImage($url)
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';
211 if (!file_exists(dirname($cache))) {
212 mkdir(dirname($cache),0666, true);
216 $a = &new HTTP_Request($url);
218 file_put_contents($cache .'.data', base64_encode( $a->getResponseBody()));
220 $mt = $a->getResponseHeader('Content-Type');
222 require_once 'File/MimeType.php';
223 $m = new File_MimeType();
224 $ext = $m->toExt($mt);
226 file_put_contents($cache, json_encode(array(
229 'contentid' => md5($url);