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 locale => 'en' .... or zh_hk....
25 rcpts => array() // override recipients..
28 * recipents is gathered from the resulting template
36 * $x->asData(); // returns data needed for notify?? - notify should really
37 * // just use this to pass around later..
43 class Pman_Core_Mailer {
45 var $page = false; /* usually a html_flexyframework_page */
46 var $contents = false; /* object or array */
47 var $template = false; /* string */
48 var $replaceImages = false; /* boolean */
50 var $templateDir = false;
51 var $locale = false; // eg. 'en' or 'zh_HK'
53 var $html_locale = false; // eg. 'en' or 'zh_HK'
54 var $images = array(); // generated list of cid images for sending
57 function Pman_Core_Mailer($args) {
58 foreach($args as $k=>$v) {
65 * ---------------- Global Tools ---------------
71 $templateFile = $this->template;
72 $args = $this->contents;
74 $content = clone($this->page);
76 foreach((array)$args as $k=>$v) {
80 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
82 $ff = HTML_FlexyFramework::get();
83 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
84 if (isset($ff->Pman['HTTP_HOST'])) {
85 $http_host = $ff->Pman['HTTP_HOST'];
89 $content->HTTP_HOST = $http_host;
94 // this should be done by having multiple template sources...!!!
96 require_once 'HTML/Template/Flexy.php';
99 // 'forceCompile' => true,
100 'site_prefix' => false,
102 if (!empty($this->templateDir)) {
103 $tmp_opts['templateDir'] = $this->templateDir;
105 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
106 if (!empty($fopts['DB_DataObject_translator'])) {
107 $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
109 if (!empty($fopts['locale'])) {
110 $tmp_opts['locale'] = $fopts['locale'];
113 // local opt's overwrite
114 if (!empty($this->locale)) {
115 $tmp_opts['locale'] = $this->locale;
119 $html_tmp_opts = $tmp_opts;
120 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
121 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
122 // then we have a multi-part email...
124 if (!empty($this->html_locale)) {
125 $html_tmp_opts['locale'] = $this->html_locale;
127 $htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
129 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
130 $htmlbody = $htmltemplate->bufferedOutputObject($content);
132 // for the html body, we may want to convert the attachments to images.
133 // var_dump($htmlbody);exit;
134 $htmlbody = $this->htmlbodytoCID($htmlbody);
138 $tmp_opts['nonHTML'] = true;
141 //print_R($tmp_opts);
142 // $tmp_opts['force'] = true;
143 $template = new HTML_Template_Flexy( $tmp_opts );
145 $template->compile('mail/'. $templateFile.'.txt');
147 /* use variables from this object to ouput data. */
148 $mailtext = $template->bufferedOutputObject($content);
149 //print_r($mailtext);exit;
153 //echo "<PRE>";print_R($mailtext);
155 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
156 require_once 'Mail/mimeDecode.php';
157 require_once 'Mail.php';
159 $decoder = new Mail_mimeDecode($mailtext);
160 $parts = $decoder->getSendArray();
161 if (PEAR::isError($parts)) {
163 //echo "PROBLEM: {$parts->message}";
167 require_once 'Mail/mime.php';
168 $mime = new Mail_mime(array(
170 //'html_encoding' => 'base64',
171 'html_charset' => 'utf-8',
172 'text_charset' => 'utf-8',
173 'head_charset' => 'utf-8',
175 // clean up the headers...
178 $parts[1]['Message-Id'] = '<' . $content->msgid .
179 '@' . $content->HTTP_HOST .'>';
182 if ($htmlbody !== false) {
183 // got a html headers...
185 if (isset($parts[1]['Content-Type'])) {
186 unset($parts[1]['Content-Type']);
188 $mime->setTXTBody($parts[2]);
189 $mime->setHTMLBody($htmlbody);
190 // var_dump($mime);exit;
191 foreach($this->images as $cid=>$cdata) {
196 $cid.'.'.$cdata['ext'],
201 $parts[2] = $mime->get();
202 $parts[1] = $mime->headers($parts[1]);
206 // list($recipents,$headers,$body) = $parts;
208 'recipents' => $parts[0],
209 'headers' => $parts[1],
216 $email = $this->toData();
217 if (is_a($email, 'PEAR_Error')) {
221 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
223 ///$recipents = array($this->email);
224 $mailOptions = PEAR::getStaticProperty('Mail','options');
225 //print_R($mailOptions);exit;
226 $mail = Mail::factory("SMTP",$mailOptions);
227 $headers['Date'] = date('r');
228 if (PEAR::isError($mail)) {
231 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
233 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
234 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
237 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
238 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
239 error_reporting($oe);
244 function htmlbodytoCID($html)
246 $dom = new DOMDocument();
247 // this may raise parse errors as some html may be a component..
248 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
249 $imgs= $dom->getElementsByTagName('img');
251 foreach ($imgs as $i=>$img) {
252 $url = $img->getAttribute('src');
253 $conv = $this->fetchImage($url);
254 $this->images[$conv['contentid']] = $conv;
256 $img->setAttribute('src', 'cid:' . $conv['contentid']);
260 return $dom->saveHTML();
265 function fetchImage($url)
268 if ($url[0] == '/') {
269 $ff = HTML_FlexyFramework::get();
270 $file = $ff->rootDir . $url;
271 require_once 'File/MimeType.php';
272 $m = new File_MimeType();
273 $mt = $m->fromFilename($file);
274 $ext = $m->toExt($mt);
279 'contentid' => md5($file),
287 //print_R($url); exit;
290 if (preg_match('#^file:///#', $url)) {
291 $file = preg_replace('#^file://#', '', $url);
292 require_once 'File/MimeType.php';
293 $m = new File_MimeType();
294 $mt = $m->fromFilename($file);
295 $ext = $m->toExt($mt);
300 'contentid' => md5($file),
307 // 2 files --- the info file.. and the actual file...
310 $uinfo = posix_getpwuid( posix_getuid () );
311 $user = $uinfo['name'];
313 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
314 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
315 $ret = json_decode($cache);
316 $ret['file'] = $cache . '.data';
319 if (!file_exists(dirname($cache))) {
320 mkdir(dirname($cache),0700, true);
323 require_once 'HTTP/Request.php';
324 $a = new HTTP_Request($url);
326 file_put_contents($cache .'.data', $a->getResponseBody());
328 $mt = $a->getResponseHeader('Content-Type');
330 require_once 'File/MimeType.php';
331 $m = new File_MimeType();
332 $ext = $m->toExt($mt);
337 'contentid' => md5($url)
341 file_put_contents($cache, json_encode($ret));
342 $ret['file'] = $cache . '.data';