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 */
49 var $templateDir = false;
51 var $images = array(); // generated list of cid images for sending
54 function Pman_Core_Mailer($args) {
55 foreach($args as $k=>$v) {
62 * ---------------- Global Tools ---------------
68 $templateFile = $this->template;
69 $args = $this->contents;
71 $content = clone($this->page);
73 foreach((array)$args as $k=>$v) {
77 $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
79 $ff = HTML_FlexyFramework::get();
80 $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
81 if (isset($ff->Pman['HTTP_HOST'])) {
82 $http_host = $ff->Pman['HTTP_HOST'];
86 $content->HTTP_HOST = $http_host;
91 // this should be done by having multiple template sources...!!!
93 require_once 'HTML/Template/Flexy.php';
96 // 'forceCompile' => true,
97 'site_prefix' => false,
99 if (!empty($this->templateDir)) {
100 $tmp_opts['templateDir'] = $this->templateDir;
102 $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
103 if (!empty($fopts['DB_DataObject_translator'])) {
104 $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
106 if (!empty($fopts['locale'])) {
107 $tmp_opts['locale'] = $fopts['locale'];
111 $htmltemplate = new HTML_Template_Flexy( $tmp_opts );
113 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
114 // then we have a multi-part email...
117 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
118 $htmlbody = $htmltemplate->bufferedOutputObject($content);
120 // for the html body, we may want to convert the attachments to images.
121 // var_dump($htmlbody);exit;
122 $htmlbody = $this->htmlbodytoCID($htmlbody);
126 $tmp_opts['nonHTML'] = true;
127 // $tmp_opts['force'] = true;
128 $template = new HTML_Template_Flexy( $tmp_opts );
130 $template->compile('mail/'. $templateFile.'.txt');
132 /* use variables from this object to ouput data. */
133 $mailtext = $template->bufferedOutputObject($content);
134 //print_r($mailtext);exit;
138 //echo "<PRE>";print_R($mailtext);
140 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
141 require_once 'Mail/mimeDecode.php';
142 require_once 'Mail.php';
144 $decoder = new Mail_mimeDecode($mailtext);
145 $parts = $decoder->getSendArray();
146 if (PEAR::isError($parts)) {
148 //echo "PROBLEM: {$parts->message}";
155 $parts[1]['Message-Id'] = '<' . $content->msgid .
156 '@' . $content->HTTP_HOST .'>';
161 if ($htmlbody !== false) {
162 require_once 'Mail/mime.php';
163 $mime = new Mail_mime(array('eol' => "\n",
164 // 'html_encoding' => 'base64',
165 'html_charset' => 'utf-8',
166 'text_charset' => 'utf-8',
167 'head_charset' => 'utf-8'
170 $mime->setTXTBody($parts[2]);
171 $mime->setHTMLBody($htmlbody);
172 // var_dump($mime);exit;
173 foreach($this->images as $cid=>$cdata) {
178 $cid.'.'.$cdata['ext'],
183 $parts[2] = $mime->get();
184 $parts[1] = $mime->headers($parts[1]);
188 // list($recipents,$headers,$body) = $parts;
190 'recipents' => $parts[0],
191 'headers' => $parts[1],
198 $email = $this->toData();
199 if (is_a($email, 'PEAR_Error')) {
203 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
205 ///$recipents = array($this->email);
206 $mailOptions = PEAR::getStaticProperty('Mail','options');
207 //print_R($mailOptions);exit;
208 $mail = Mail::factory("SMTP",$mailOptions);
209 $headers['Date'] = date('r');
210 if (PEAR::isError($mail)) {
213 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
215 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
216 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
219 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
220 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
221 error_reporting($oe);
226 function htmlbodytoCID($html)
228 $dom = new DOMDocument();
229 // this may raise parse errors as some html may be a component..
230 @$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
231 $imgs= $dom->getElementsByTagName('img');
233 foreach ($imgs as $i=>$img) {
234 $url = $img->getAttribute('src');
235 $conv = $this->fetchImage($url);
236 $this->images[$conv['contentid']] = $conv;
238 $img->setAttribute('src', 'cid:' . $conv['contentid']);
242 return $dom->saveHTML();
247 function fetchImage($url)
250 if ($url[0] == '/') {
251 $ff = HTML_FlexyFramework::get();
252 $file = $ff->rootDir . $url;
253 require_once 'File/MimeType.php';
254 $m = new File_MimeType();
255 $mt = $m->fromFilename($file);
256 $ext = $m->toExt($mt);
261 'contentid' => md5($file),
269 //print_R($url); exit;
272 if (preg_match('#^file:///#', $url)) {
273 $file = preg_replace('#^file://#', '', $url);
274 require_once 'File/MimeType.php';
275 $m = new File_MimeType();
276 $mt = $m->fromFilename($file);
277 $ext = $m->toExt($mt);
282 'contentid' => md5($file),
289 // 2 files --- the info file.. and the actual file...
292 $uinfo = posix_getpwuid( posix_getuid () );
293 $user = $uinfo['name'];
295 $cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
296 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
297 $ret = json_decode($cache);
298 $ret['file'] = $cache . '.data';
301 if (!file_exists(dirname($cache))) {
302 mkdir(dirname($cache),0700, true);
305 require_once 'HTTP/Request.php';
306 $a = new HTTP_Request($url);
308 file_put_contents($cache .'.data', $a->getResponseBody());
310 $mt = $a->getResponseHeader('Content-Type');
312 require_once 'File/MimeType.php';
313 $m = new File_MimeType();
314 $ext = $m->toExt($mt);
319 'contentid' => md5($url)
323 file_put_contents($cache, json_encode($ret));
324 $ret['file'] = $cache . '.data';