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 $images = array(); // generated list of cid images for sending
56 function Pman_Core_Mailer($args) {
57 foreach($args as $k=>$v) {
64 * ---------------- Global Tools ---------------
70 $templateFile = $this->template;
71 $args = $this->contents;
73 $content = clone($this->page);
75 foreach((array)$args as $k=>$v) {
79 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
81 $ff = HTML_FlexyFramework::get();
82 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
83 if (isset($ff->Pman['HTTP_HOST'])) {
84 $http_host = $ff->Pman['HTTP_HOST'];
88 $content->HTTP_HOST = $http_host;
93 // this should be done by having multiple template sources...!!!
95 require_once 'HTML/Template/Flexy.php';
98 // 'forceCompile' => true,
99 'site_prefix' => false,
101 if (!empty($this->templateDir)) {
102 $tmp_opts['templateDir'] = $this->templateDir;
104 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
105 if (!empty($fopts['DB_DataObject_translator'])) {
106 $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
108 if (!empty($fopts['locale'])) {
109 $tmp_opts['locale'] = $fopts['locale'];
112 // local opt's overwrite
113 if (!empty($this->locale)) {
114 $tmp_opts['locale'] = $this->locale;
118 $htmltemplate = new HTML_Template_Flexy( $tmp_opts );
120 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
121 // then we have a multi-part email...
124 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
125 $htmlbody = $htmltemplate->bufferedOutputObject($content);
127 // for the html body, we may want to convert the attachments to images.
128 // var_dump($htmlbody);exit;
129 $htmlbody = $this->htmlbodytoCID($htmlbody);
133 $tmp_opts['nonHTML'] = true;
136 //print_R($tmp_opts);
137 // $tmp_opts['force'] = true;
138 $template = new HTML_Template_Flexy( $tmp_opts );
140 $template->compile('mail/'. $templateFile.'.txt');
142 /* use variables from this object to ouput data. */
143 $mailtext = $template->bufferedOutputObject($content);
144 //print_r($mailtext);exit;
148 //echo "<PRE>";print_R($mailtext);
150 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
151 require_once 'Mail/mimeDecode.php';
152 require_once 'Mail.php';
154 $decoder = new Mail_mimeDecode($mailtext);
155 $parts = $decoder->getSendArray();
156 if (PEAR::isError($parts)) {
158 //echo "PROBLEM: {$parts->message}";
162 require_once 'Mail/mime.php';
163 $mime = new Mail_mime(array(
165 //'html_encoding' => 'base64',
166 'html_charset' => 'utf-8',
167 'text_charset' => 'utf-8',
168 'head_charset' => 'utf-8',
170 // clean up the headers...
173 $parts[1]['Message-Id'] = '<' . $content->msgid .
174 '@' . $content->HTTP_HOST .'>';
177 if ($htmlbody !== false) {
178 // got a html headers...
180 if (isset($parts[1]['Content-Type'])) {
181 unset($parts[1]['Content-Type']);
183 $mime->setTXTBody($parts[2]);
184 $mime->setHTMLBody($htmlbody);
185 // var_dump($mime);exit;
186 foreach($this->images as $cid=>$cdata) {
191 $cid.'.'.$cdata['ext'],
196 $parts[2] = $mime->get();
197 $parts[1] = $mime->headers($parts[1]);
201 // list($recipents,$headers,$body) = $parts;
203 'recipents' => $parts[0],
204 'headers' => $parts[1],
211 $email = $this->toData();
212 if (is_a($email, 'PEAR_Error')) {
216 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
218 ///$recipents = array($this->email);
219 $mailOptions = PEAR::getStaticProperty('Mail','options');
220 //print_R($mailOptions);exit;
221 $mail = Mail::factory("SMTP",$mailOptions);
222 $headers['Date'] = date('r');
223 if (PEAR::isError($mail)) {
226 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
228 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
229 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
232 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
233 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
234 error_reporting($oe);
239 function htmlbodytoCID($html)
241 $dom = new DOMDocument();
242 // this may raise parse errors as some html may be a component..
243 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
244 $imgs= $dom->getElementsByTagName('img');
246 foreach ($imgs as $i=>$img) {
247 $url = $img->getAttribute('src');
248 $conv = $this->fetchImage($url);
249 $this->images[$conv['contentid']] = $conv;
251 $img->setAttribute('src', 'cid:' . $conv['contentid']);
255 return $dom->saveHTML();
260 function fetchImage($url)
263 if ($url[0] == '/') {
264 $ff = HTML_FlexyFramework::get();
265 $file = $ff->rootDir . $url;
266 require_once 'File/MimeType.php';
267 $m = new File_MimeType();
268 $mt = $m->fromFilename($file);
269 $ext = $m->toExt($mt);
274 'contentid' => md5($file),
282 //print_R($url); exit;
285 if (preg_match('#^file:///#', $url)) {
286 $file = preg_replace('#^file://#', '', $url);
287 require_once 'File/MimeType.php';
288 $m = new File_MimeType();
289 $mt = $m->fromFilename($file);
290 $ext = $m->toExt($mt);
295 'contentid' => md5($file),
302 // 2 files --- the info file.. and the actual file...
305 $uinfo = posix_getpwuid( posix_getuid () );
306 $user = $uinfo['name'];
308 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
309 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
310 $ret = json_decode($cache);
311 $ret['file'] = $cache . '.data';
314 if (!file_exists(dirname($cache))) {
315 mkdir(dirname($cache),0700, true);
318 require_once 'HTTP/Request.php';
319 $a = new HTTP_Request($url);
321 file_put_contents($cache .'.data', $a->getResponseBody());
323 $mt = $a->getResponseHeader('Content-Type');
325 require_once 'File/MimeType.php';
326 $m = new File_MimeType();
327 $ext = $m->toExt($mt);
332 'contentid' => md5($url)
336 file_put_contents($cache, json_encode($ret));
337 $ret['file'] = $cache . '.data';