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)
19 * $x= Pman_Core_Mailer(array(
23 html_locale => 'en' == always use the 'english translated verison'
24 cache_images => true -- defaults to caching images - set to false to disable.
25 replaceImages => true|false,
26 locale => 'en' .... or zh_hk....
27 rcpts => array() // override recipients..
31 name : (optional) - uses basename of file
35 mail_method : (SMTP or SMTPMX)
39 * recipents is gathered from the resulting template
47 * $x->toData(); // returns data needed for notify?? - notify should really
48 * // just use this to pass around later..
54 class Pman_Core_Mailer {
56 var $page = false; /* usually a html_flexyframework_page */
57 var $contents = false; /* object or array */
58 var $template = false; /* string */
59 var $replaceImages = false; /* boolean */
61 var $templateDir = false;
62 var $locale = false; // eg. 'en' or 'zh_HK'
65 var $html_locale = false; // eg. 'en' or 'zh_HK'
66 var $images = array(); // generated list of cid images for sending
67 var $attachments = false;
68 var $css_inline = false; // not supported
69 var $css_embed = false; // put the css tags into the body.
71 var $mail_method = 'SMTP';
73 var $cache_images = true;
75 function Pman_Core_Mailer($args) {
76 foreach($args as $k=>$v) {
80 // allow core mailer debug setting.
81 $ff = HTML_FlexyFramework::get();
83 if (!empty($ff->Core_Mailer['debug'])) {
84 $this->debug = $ff->Core_Mailer['debug'];
91 * ---------------- Global Tools ---------------
97 $templateFile = $this->template;
98 $args = $this->contents;
100 $content = clone($this->page);
102 foreach((array)$args as $k=>$v) {
106 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
108 $ff = HTML_FlexyFramework::get();
109 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
110 if (isset($ff->Pman['HTTP_HOST'])) {
111 $http_host = $ff->Pman['HTTP_HOST'];
115 $content->HTTP_HOST = $http_host;
120 // this should be done by having multiple template sources...!!!
122 require_once 'HTML/Template/Flexy.php';
125 // 'forceCompile' => true,
126 'site_prefix' => false,
128 if (!empty($this->templateDir)) {
129 $tmp_opts['templateDir'] = $this->templateDir;
131 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
132 if (!empty($fopts['DB_DataObject_translator'])) {
133 $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
135 if (!empty($fopts['locale'])) {
136 $tmp_opts['locale'] = $fopts['locale'];
139 // local opt's overwrite
140 if (!empty($this->locale)) {
141 $tmp_opts['locale'] = $this->locale;
145 $html_tmp_opts = $tmp_opts;
146 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
147 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
148 // then we have a multi-part email...
150 if (!empty($this->html_locale)) {
151 $html_tmp_opts['locale'] = $this->html_locale;
153 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
155 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
156 $htmlbody = $htmltemplate->bufferedOutputObject($content);
158 $this->htmlbody = $htmlbody;
160 // for the html body, we may want to convert the attachments to images.
161 // var_dump($htmlbody);exit;
162 if ($this->replaceImages) {
163 $htmlbody = $this->htmlbodytoCID($htmlbody);
165 if ($this->css_embed) {
166 $htmlbody = $this->htmlbodyCssEmbed($htmlbody);
170 $tmp_opts['nonHTML'] = true;
173 //print_R($tmp_opts);
174 // $tmp_opts['force'] = true;
175 $template = new HTML_Template_Flexy( $tmp_opts );
177 $template->compile('mail/'. $templateFile.'.txt');
179 /* use variables from this object to ouput data. */
180 $mailtext = $template->bufferedOutputObject($content);
181 //print_r($mailtext);exit;
185 //echo "<PRE>";print_R($mailtext);
187 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
188 require_once 'Mail/mimeDecode.php';
189 require_once 'Mail.php';
191 $decoder = new Mail_mimeDecode($mailtext);
192 $parts = $decoder->getSendArray();
193 if (PEAR::isError($parts)) {
195 //echo "PROBLEM: {$parts->message}";
201 require_once 'Mail/mime.php';
202 $mime = new Mail_mime(array(
204 //'html_encoding' => 'base64',
205 'html_charset' => 'utf-8',
206 'text_charset' => 'utf-8',
207 'head_charset' => 'utf-8',
209 // clean up the headers...
212 $parts[1]['Message-Id'] = '<' . $content->msgid .
213 '@' . $content->HTTP_HOST .'>';
216 if ($htmlbody !== false) {
217 // got a html headers...
219 if (isset($parts[1]['Content-Type'])) {
220 unset($parts[1]['Content-Type']);
222 $mime->setTXTBody($parts[2]);
223 $mime->setHTMLBody($htmlbody);
224 // var_dump($mime);exit;
225 foreach($this->images as $cid=>$cdata) {
230 $cid.'.'.$cdata['ext'],
238 if(!empty($this->attachments)){
239 //if got a attachments
240 $header = $mime->headers($parts[1]);
242 if (isset($parts[1]['Content-Type'])) {
243 unset($parts[1]['Content-Type']);
248 if(preg_match('/text\/html/', $header['Content-Type'])){
249 $mime->setHTMLBody($parts[2]);
250 $mime->setTXTBody('This message is in HTML only');
252 $mime->setTXTBody($parts[2]);
253 $mime->setHTMLBody('<PRE>'.htmlspecialchars($parts[2]).'</PRE>');
256 foreach($this->attachments as $attch){
257 $mime->addAttachment(
260 (!empty($attch['name'])) ? $attch['name'] : '',
269 $parts[2] = $mime->get();
270 $parts[1] = $mime->headers($parts[1]);
274 // print_r($parts[2]);
276 // list($recipents,$headers,$body) = $parts;
278 'recipents' => $parts[0],
279 'headers' => $parts[1],
284 function send($email = false)
287 $pg = HTML_FlexyFramework::get()->page;
290 $email = is_array($email) ? $email : $this->toData();
291 if (is_a($email, 'PEAR_Error')) {
292 $pg->addEvent("COREMAILER-FAIL", false, "email toData failed");
298 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
300 ///$recipents = array($this->email);
301 $mailOptions = PEAR::getStaticProperty('Mail','options');
302 //print_R($mailOptions);exit;
304 if ($this->mail_method == 'SMTPMX' && empty($mailOptions['mailname'])) {
305 $pg->jerr("Mail[mailname] is not set - this is required for SMTPMX");
309 $mail = Mail::factory($this->mail_method,$mailOptions);
311 $mail->debug = $this->debug;
314 $email['headers']['Date'] = date('r');
315 if (PEAR::isError($mail)) {
316 $pg->addEvent("COREMAILER-FAIL", false, "mail factory failed");
321 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
323 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
324 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
327 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
328 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
329 error_reporting($oe);
331 $pg->addEvent("COREMAILER-SENT", false,
332 'To: ' . ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
333 'Subject: ' . @$email['headers']['Subject']
336 $pg->addEvent("COREMAILER-FAIL", false, $ret->toString());
342 function htmlbodytoCID($html)
344 $dom = new DOMDocument();
345 // this may raise parse errors as some html may be a component..
346 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
347 $imgs= $dom->getElementsByTagName('img');
349 foreach ($imgs as $i=>$img) {
350 $url = $img->getAttribute('src');
351 if (preg_match('#^cid:#', $url)) {
354 $me = $img->getAttribute('mailembed');
359 $conv = $this->fetchImage($url);
360 $this->images[$conv['contentid']] = $conv;
362 $img->setAttribute('src', 'cid:' . $conv['contentid']);
366 return $dom->saveHTML();
371 function htmlbodyCssEmbed($html)
373 $ff = HTML_FlexyFramework::get();
374 $dom = new DOMDocument();
376 // this may raise parse errors as some html may be a component..
377 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
378 $links = $dom->getElementsByTagName('link');
380 foreach ($links as $link) { // duplicate as links is dynamic and we change it..!
383 //<link rel="stylesheet" type="text/css" href="{rootURL}/roojs1/css-mailer/mailer.css">
385 foreach ($lc as $i=>$link) {
386 //var_dump($link->getAttribute('href'));
388 if ($link->getAttribute('rel') != 'stylesheet') {
391 $url = $link->getAttribute('href');
392 $file = $ff->rootDir . $url;
394 if (!preg_match('#^http://#', $url)) {
395 $file = $ff->rootDir . $url;
397 if (!file_exists($file)) {
399 $link->setAttribute('href', 'missing:' . $file);
406 $par = $link->parentNode;
407 $par->removeChild($link);
408 $s = $dom->createElement('style');
409 $e = $dom->createTextNode(file_get_contents($file));
411 $par->appendChild($s);
414 return $dom->saveHTML();
421 function fetchImage($url)
424 echo "FETCH : $url\n";
426 if ($url[0] == '/') {
427 $ff = HTML_FlexyFramework::get();
428 $file = $ff->rootDir . $url;
429 require_once 'File/MimeType.php';
430 $m = new File_MimeType();
431 $mt = $m->fromFilename($file);
432 $ext = $m->toExt($mt);
437 'contentid' => md5($file),
445 //print_R($url); exit;
448 if (preg_match('#^file:///#', $url)) {
449 $file = preg_replace('#^file://#', '', $url);
450 require_once 'File/MimeType.php';
451 $m = new File_MimeType();
452 $mt = $m->fromFilename($file);
453 $ext = $m->toExt($mt);
458 'contentid' => md5($file),
465 // 2 files --- the info file.. and the actual file...
468 $uinfo = posix_getpwuid( posix_getuid () );
469 $user = $uinfo['name'];
471 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
472 if ($this->cache_images &&
473 file_exists($cache) &&
474 filemtime($cache) > strtotime('NOW - 1 WEEK')
476 $ret = json_decode(file_get_contents($cache), true);
477 $ret['file'] = $cache . '.data';
480 if (!file_exists(dirname($cache))) {
481 mkdir(dirname($cache),0700, true);
484 require_once 'HTTP/Request.php';
485 $a = new HTTP_Request($url);
487 file_put_contents($cache .'.data', $a->getResponseBody());
489 $mt = $a->getResponseHeader('Content-Type');
491 require_once 'File/MimeType.php';
492 $m = new File_MimeType();
493 $ext = $m->toExt($mt);
498 'contentid' => md5($url)
502 file_put_contents($cache, json_encode($ret));
503 $ret['file'] = $cache . '.data';