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.
111 // var_dump($htmlbody);exit;
112 $htmlbody = $this->htmlbodytoCID($htmlbody);
116 $tmp_opts['nonHTML'] = true;
117 // $tmp_opts['force'] = true;
118 $template = new HTML_Template_Flexy( $tmp_opts );
120 $template->compile('mail/'. $templateFile.'.txt');
122 /* use variables from this object to ouput data. */
123 $mailtext = $template->bufferedOutputObject($content);
124 //print_r($mailtext);exit;
128 //echo "<PRE>";print_R($mailtext);
130 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
131 require_once 'Mail/mimeDecode.php';
132 require_once 'Mail.php';
134 $decoder = new Mail_mimeDecode($mailtext);
135 $parts = $decoder->getSendArray();
136 if (PEAR::isError($parts)) {
138 //echo "PROBLEM: {$parts->message}";
145 $parts[1]['Message-Id'] = '<' . $content->msgid .
146 '@' . $content->HTTP_HOST .'>';
151 if ($htmlbody !== false) {
152 require_once 'Mail/mime.php';
153 $mime = new Mail_mime(array('eol' => "\n",
154 'html_charset' => 'utf-8',
155 'text_charset' => 'utf-8',
156 // 'head_charset' => 'utf-8'
159 $mime->setTXTBody($parts[2]);
160 $mime->setHTMLBody($htmlbody);
161 // var_dump($mime);exit;
162 foreach($this->images as $cid=>$cdata) {
167 $cid.'.'.$cdata['ext'],
172 $parts[2] = $mime->get();
173 $parts[1] = $mime->headers($parts[1]);
177 // list($recipents,$headers,$body) = $parts;
179 'recipents' => $parts[0],
180 'headers' => $parts[1],
187 $email = $this->toData();
188 if (is_a($email, 'PEAR_Error')) {
192 echo '<PRE>';echo htmlspecialchars(print_r($email,true));
194 ///$recipents = array($this->email);
195 $mailOptions = PEAR::getStaticProperty('Mail','options');
196 //print_R($mailOptions);exit;
197 $mail = Mail::factory("SMTP",$mailOptions);
198 $headers['Date'] = date('r');
199 if (PEAR::isError($mail)) {
202 $rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
204 if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
205 $rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
208 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
209 $ret = $mail->send($rcpts,$email['headers'],$email['body']);
210 error_reporting($oe);
215 function htmlbodytoCID($html)
217 $dom = new DOMDocument;
218 $dom->loadHTML($html);
219 $imgs= $dom->getElementsByTagName('img');
221 foreach ($imgs as $i=>$img) {
222 $url = $img->getAttribute('src');
223 $conv = $this->fetchImage($url);
224 $this->images[$conv['contentid']] = $conv;
226 $img->setAttribute('src', 'cid:' . $conv['contentid']);
230 return $dom->saveHTML();
235 function fetchImage($url)
238 if ($url[0] == '/') {
239 $file = $this->page->rootDir . $url;
240 require_once 'File/MimeType.php';
241 $m = new File_MimeType();
242 $mt = $m->fromFilename($file);
243 $ext = $m->toExt($mt);
248 'contentid' => md5($file),
256 //print_R($url); exit;
259 if (preg_match('#^file:///#', $url)) {
260 $file = preg_replace('#^file://#', '', $url);
261 require_once 'File/MimeType.php';
262 $m = new File_MimeType();
263 $mt = $m->fromFilename($file);
264 $ext = $m->toExt($mt);
269 'contentid' => md5($file),
276 // 2 files --- the info file.. and the actual file...
278 $cache = ini_get('session.save_path').'/Pman_Core_Mailer/' . md5($url);
279 if (file_exists($cache) and filemtime($cache) > strtotime('NOW - 1 WEEK')) {
280 $ret = json_decode($cache);
281 $ret['file'] = $cache . '.data';
284 if (!file_exists(dirname($cache))) {
285 mkdir(dirname($cache),0666, true);
288 require_once 'HTTP/Request.php';
289 $a = new HTTP_Request($url);
291 file_put_contents($cache .'.data', $a->getResponseBody());
293 $mt = $a->getResponseHeader('Content-Type');
295 require_once 'File/MimeType.php';
296 $m = new File_MimeType();
297 $ext = $m->toExt($mt);
302 'contentid' => md5($url)
306 file_put_contents($cache, json_encode($ret));
307 $ret['file'] = $cache . '.data';