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;
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}";
165 $parts[1]['Message-Id'] = '<' . $content->msgid .
166 '@' . $content->HTTP_HOST .'>';
171 if ($htmlbody !== false) {
172 require_once 'Mail/mime.php';
173 $mime = new Mail_mime(array('eol' => "\n",
174 // 'html_encoding' => 'base64',
175 'html_charset' => 'utf-8',
176 'text_charset' => 'utf-8',
177 'head_charset' => 'utf-8'
180 $mime->setTXTBody($parts[2]);
181 $mime->setHTMLBody($htmlbody);
182 // var_dump($mime);exit;
183 foreach($this->images as $cid=>$cdata) {
188 $cid.'.'.$cdata['ext'],
193 $parts[2] = $mime->get();
194 $parts[1] = $mime->headers($parts[1]);
198 // list($recipents,$headers,$body) = $parts;
200 'recipents' => $parts[0],
201 'headers' => $parts[1],
208 $email = $this->toData();
209 if (is_a($email, 'PEAR_Error')) {
213 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
215 ///$recipents = array($this->email);
216 $mailOptions = PEAR::getStaticProperty('Mail','options');
217 //print_R($mailOptions);exit;
218 $mail = Mail::factory("SMTP",$mailOptions);
219 $headers['Date'] = date('r');
220 if (PEAR::isError($mail)) {
223 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
225 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
226 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
229 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
230 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
231 error_reporting($oe);
236 function htmlbodytoCID($html)
238 $dom = new DOMDocument();
239 // this may raise parse errors as some html may be a component..
240 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
241 $imgs= $dom->getElementsByTagName('img');
243 foreach ($imgs as $i=>$img) {
244 $url = $img->getAttribute('src');
245 $conv = $this->fetchImage($url);
246 $this->images[$conv['contentid']] = $conv;
248 $img->setAttribute('src', 'cid:' . $conv['contentid']);
252 return $dom->saveHTML();
257 function fetchImage($url)
260 if ($url[0] == '/') {
261 $ff = HTML_FlexyFramework::get();
262 $file = $ff->rootDir . $url;
263 require_once 'File/MimeType.php';
264 $m = new File_MimeType();
265 $mt = $m->fromFilename($file);
266 $ext = $m->toExt($mt);
271 'contentid' => md5($file),
279 //print_R($url); exit;
282 if (preg_match('#^file:///#', $url)) {
283 $file = preg_replace('#^file://#', '', $url);
284 require_once 'File/MimeType.php';
285 $m = new File_MimeType();
286 $mt = $m->fromFilename($file);
287 $ext = $m->toExt($mt);
292 'contentid' => md5($file),
299 // 2 files --- the info file.. and the actual file...
302 $uinfo = posix_getpwuid( posix_getuid () );
303 $user = $uinfo['name'];
305 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
306 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
307 $ret = json_decode($cache);
308 $ret['file'] = $cache . '.data';
311 if (!file_exists(dirname($cache))) {
312 mkdir(dirname($cache),0700, true);
315 require_once 'HTTP/Request.php';
316 $a = new HTTP_Request($url);
318 file_put_contents($cache .'.data', $a->getResponseBody());
320 $mt = $a->getResponseHeader('Content-Type');
322 require_once 'File/MimeType.php';
323 $m = new File_MimeType();
324 $ext = $m->toExt($mt);
329 'contentid' => md5($url)
333 file_put_contents($cache, json_encode($ret));
334 $ret['file'] = $cache . '.data';