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;
90 // this should be done by having multiple template sources...!!!
92 require_once 'HTML/Template/Flexy.php';
95 if (!empty($this->templateDir)) {
96 $tmp_opts['templateDir'] = $this->templateDir;
101 $htmltemplate = new HTML_Template_Flexy( $tmp_opts );
103 if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
104 // then we have a multi-part email...
107 $htmltemplate->compile('mail/'. $templateFile.'.body.html');
108 $htmlbody = $htmltemplate->bufferedOutputObject($content);
110 // for the html body, we may want to convert the attachments to images.
112 $htmlbody = $this->htmlbodytoCID($htmlbody);
117 $tmp_opts['nonHTML'] = true;
119 $template = new HTML_Template_Flexy( $tmp_opts );
121 $template->compile('mail/'. $templateFile.'.txt');
123 /* use variables from this object to ouput data. */
124 $mailtext = $template->bufferedOutputObject($content);
135 //echo "<PRE>";print_R($mailtext);
137 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
138 require_once 'Mail/mimeDecode.php';
139 require_once 'Mail.php';
141 $decoder = new Mail_mimeDecode($mailtext);
142 $parts = $decoder->getSendArray();
143 if (PEAR::isError($parts)) {
145 //echo "PROBLEM: {$parts->message}";
152 $parts[1]['Message-Id'] = '<' . $content->msgid .
153 '@' . $content->HTTP_HOST .'>';
158 if ($htmlbody !== false) {
159 require_once 'Mail/mime.php';
160 $mime = new Mail_mime(array('eol' => "\n"));
162 $mime->setTXTBody($parts[2]);
163 $mime->setHTMLBody($htmlbody);
165 foreach($this->images as $cid=>$cdata) {
170 $cid.'.'.$cdata['ext'],
175 $parts[2] = $mime->get();
176 $parts[1] = $mime->headers($parts[1]);
182 // list($recipents,$headers,$body) = $parts;
184 'recipents' => $parts[0],
185 'headers' => $parts[1],
192 $email = $this->toData();
193 if (is_a($email, 'PEAR_Error')) {
196 ///$recipents = array($this->email);
197 $mailOptions = PEAR::getStaticProperty('Mail','options');
198 print_R($mailOptions);exit;
199 $mail = Mail::factory("SMTP",$mailOptions);
200 $headers['Date'] = date('r');
201 if (PEAR::isError($mail)) {
204 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
206 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
207 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
210 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
211 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
212 error_reporting($oe);
217 function htmlbodytoCID($html)
219 $dom = new DOMDocument;
220 $dom->loadHTML($html);
221 $imgs= $dom->getElementsByTagName('img');
223 foreach ($imgs as $i=>$img) {
224 $url = $img->getAttribute('src');
225 $conv = $this->fetchImage($url);
226 $this->images[$conv['contentid']] = $conv;
228 $img->setAttribute('src', 'cid:' . $conv['contentid']);
232 return $dom->saveHTML();
237 function fetchImage($url)
240 if ($url[0] == '/') {
241 $file = $this->page->rootDir . $url;
242 require_once 'File/MimeType.php';
243 $m = new File_MimeType();
244 $mt = $m->fromFilename($file);
245 $ext = $m->toExt($mt);
250 'contentid' => md5($file),
258 //print_R($url); exit;
261 if (preg_match('#^file:///#', $url)) {
262 $file = preg_replace('#^file://#', '', $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),
278 // 2 files --- the info file.. and the actual file...
279 $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
280 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
281 $ret = json_decode($cache);
282 $ret['file'] = $cache . '.data';
285 if (!file_exists(dirname($cache))) {
286 mkdir(dirname($cache),0666, true);
289 require_once 'HTTP/Request.php';
290 $a = new HTTP_Request($url);
292 file_put_contents($cache .'.data', $a->getResponseBody());
294 $mt = $a->getResponseHeader('Content-Type');
296 require_once 'File/MimeType.php';
297 $m = new File_MimeType();
298 $ext = $m->toExt($mt);
303 'contentid' => md5($url)
307 file_put_contents($cache, json_encode($ret));
308 $ret['file'] = $cache . '.data';