5 * code that used to be in Pman (sendTemplate / emailTemplate)
7 * template is in template directory subfolder 'mail'
9 * eg. use 'welcome' as template -> this will use templates/mail/welcome.txt
10 * if you also have templates/mail/welcome.body.html - then that will be used as
17 * $x= new Pman_Core_Mailer($opts)
23 replaceImages => true|false,
24 rcpts => array() // override recipients..
27 * recipents is gathered from the resulting template
35 * $x->asData(); // returns data needed for notify?? - notify should really
36 * // just use this to pass around later..
42 class Pman_Core_Mailer {
44 var $page = false; /* usually a html_flexyframework_page */
45 var $contents = false; /* object or array */
46 var $template = false; /* string */
47 var $replaceImages = false; /* boolean */
50 var $images = array(); // generated list of cid images for sending
53 function Pman_Core_Mailer($args) {
54 foreach($args as $k=>$v) {
61 * ---------------- Global Tools ---------------
67 $templateFile = $this->template;
68 $args = $this->contents;
70 $content = clone($this->page);
72 foreach((array)$args as $k=>$v) {
76 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
78 $ff = HTML_FlexyFramework::get();
79 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
80 if (isset($ff->Pman['HTTP_HOST'])) {
81 $http_host = $ff->Pman['HTTP_HOST'];
85 $content->HTTP_HOST = $http_host;
89 // this should be done by having multiple template sources...!!!
91 require_once 'HTML/Template/Flexy.php';
94 $htmltemplate = new HTML_Template_Flexy( );
96 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
97 // then we have a multi-part email...
100 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
101 $htmlbody = $htmltemplate->bufferedOutputObject($content);
103 // for the html body, we may want to convert the attachments to images.
105 $htmlbody = $this->htmlbodytoCID($htmlbody);
112 $template = new HTML_Template_Flexy( array(
116 $template->compile('mail/'. $templateFile.'.txt');
118 /* use variables from this object to ouput data. */
119 $mailtext = $template->bufferedOutputObject($content);
130 //echo "<PRE>";print_R($mailtext);
132 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
133 require_once 'Mail/mimeDecode.php';
134 require_once 'Mail.php';
136 $decoder = new Mail_mimeDecode($mailtext);
137 $parts = $decoder->getSendArray();
138 if (PEAR::isError($parts)) {
140 //echo "PROBLEM: {$parts->message}";
147 $parts[1]['Message-Id'] = '<' . $content->msgid .
148 '@' . $content->HTTP_HOST .'>';
153 if ($htmlbody !== false) {
154 require_once 'Mail/mime.php';
155 $mime = new Mail_mime(array('eol' => "\n"));
157 $mime->setTXTBody($parts[2]);
158 $mime->setHTMLBody($htmlbody);
160 foreach($this->images as $cid=>$cdata) {
165 $cid.'.'.$cdata['ext'],
170 $parts[2] = $mime->get();
171 $parts[1] = $mime->headers($parts[1]);
177 // list($recipents,$headers,$body) = $parts;
179 'recipents' => $parts[0],
180 'headers' => $parts[1],
187 $email = $this->toData();
188 if (is_a($email, 'PEAR_Error')) {
191 ///$recipents = array($this->email);
192 $mailOptions = PEAR::getStaticProperty('Mail','options');
193 $mail = Mail::factory("SMTP",$mailOptions);
194 $headers['Date'] = date('r');
195 if (PEAR::isError($mail)) {
198 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
199 $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
200 error_reporting($oe);
205 function htmlbodytoCID($html)
207 $dom = new DOMDocument;
208 $dom->loadHTML($html);
209 $imgs= $dom->getElementsByTagName('img');
211 foreach ($imgs as $i=>$img) {
212 $url = $img->getAttribute('src');
213 $conv = $this->fetchImage($url);
214 $this->images[$conv['contentid']] = $conv;
216 $img->setAttribute('src', 'cid:' . $conv['contentid']);
220 return $dom->saveHTML();
225 function fetchImage($url)
228 if ($url[0] == '/') {
229 $file = $this->page->rootDir . $url;
230 require_once 'File/MimeType.php';
231 $m = new File_MimeType();
232 $mt = $m->fromFilename($file);
233 $ext = $m->toExt($mt);
238 'contentid' => md5($file),
246 //print_R($url); exit;
249 if (preg_match('#^file:///#', $url)) {
250 $file = preg_replace('#^file://#', '', $url);
251 require_once 'File/MimeType.php';
252 $m = new File_MimeType();
253 $mt = $m->fromFilename($file);
254 $ext = $m->toExt($mt);
259 'contentid' => md5($file),
266 // 2 files --- the info file.. and the actual file...
267 $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
268 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
269 $ret = json_decode($cache);
270 $ret['file'] = $cache . '.data';
273 if (!file_exists(dirname($cache))) {
274 mkdir(dirname($cache),0666, true);
277 require_once 'HTTP/Request.php';
278 $a = new HTTP_Request($url);
280 file_put_contents($cache .'.data', $a->getResponseBody());
282 $mt = $a->getResponseHeader('Content-Type');
284 require_once 'File/MimeType.php';
285 $m = new File_MimeType();
286 $ext = $m->toExt($mt);
291 'contentid' => md5($url)
295 file_put_contents($cache, json_encode($ret));
296 $ret['file'] = $cache . '.data';