-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.php
678 lines (528 loc) · 21.1 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?php
/**
*
* code that used to be in Pman (sendTemplate / emailTemplate)
*
* template is in template directory subfolder 'mail'
*
* eg. use 'welcome' as template -> this will use templates/mail/welcome.txt
* if you also have templates/mail/welcome.body.html - then that will be used as
* the html body
*
*
* usage:
*
*
* require_once 'Pman/Core/Mailer.php';
* $x= new Pman_Core_Mailer(array(
'page' => $this,
// if bcc is property of this, then it will be used (BAD DESIGN)
'rcpts' => array(),
'template' => 'your_template',
// must be in templates/mail direcotry..
// header and plaintext verison in mail/your_template.txt
// if you want a html body - use mail/your_template.body.html
// 'bcc' => 'xyz@abc.com,abc@xyz.com', // string...
// 'contents' => array(), // << keys must be trusted
// if bcc is property of contents, then it will be used (BAD DESIGN)
// 'html_locale => 'en', // always use the 'english translated verison'
// 'cache_images => true, // -- defaults to caching images - set to false to disable.
// 'replaceImages => false, // should images be replaced.
// 'urlmap => array( // map urls from template to a different location.
// 'https://www.mysite.com/' => 'http://localhost/',
// ),
// 'locale' => 'en', // .... or zh_hk....
// 'attachments' => array(
// array(
// 'file' => '/path/to/file', // file location
// name => 'myfile.pdf', // (optional) - uses basename of file
// mimetype :
// ),
//
// 'mail_method' => 'SMTP', // or SMTPMX
)
*
* recipents is gathered from the resulting template
* -- eg.
* To: <a>,<b>,<c>
*
*
* if the file '
*
*
* $x->toData(); // returns data needed for notify?? - notify should really
* // just use this to pass around later..
*
* $x->send();
*
*/
class Pman_Core_Mailer {
var $debug = 0;
var $page = false; /* usually a html_flexyframework_page */
var $contents = array(); /* object or array */
var $template = false; /* string */
var $replaceImages = false; /* boolean */
var $rcpts = false;
var $templateDir = false;
var $locale = false; // eg. 'en' or 'zh_HK'
var $urlmap = array();
var $htmlbody;
var $textbody;
var $html_locale = false; // eg. 'en' or 'zh_HK'
var $images = array(); // generated list of cid images for sending
var $attachments = false;
var $css_inline = false; // put the css into the html
var $css_embed = false; // put the css tags into the body.
var $mail_method = 'SMTP';
var $cache_images = true;
var $bcc = false;
var $body_cls = false;
function __construct($args) {
foreach($args as $k=>$v) {
// a bit trusting..
$this->$k = $v;
}
// allow core mailer debug setting.
$ff = HTML_FlexyFramework::get();
if (!empty($ff->Core_Mailer['debug'])) {
$this->debug = $ff->Core_Mailer['debug'];
}
//$this->log("URL MAP");
//$this->log($this->urlmap);
}
/**
* ---------------- Global Tools ---------------
*
* applies this variables to a object
* msgid
* HTTP_HOIST
*
*/
function toData()
{
$templateFile = $this->template;
$args = (array)$this->contents;
$content = clone($this->page);
foreach($args as $k=>$v) {
$content->$k = $v;
}
$content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
// content can override this now
$ff = HTML_FlexyFramework::get();
$http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
if (isset($ff->Pman['HTTP_HOST']) && $http_host != 'localhost') {
$http_host = $ff->Pman['HTTP_HOST'];
}
if (empty($content->HTTP_HOST )) {
$content->HTTP_HOST = $http_host;
}
// this should be done by having multiple template sources...!!!
require_once 'HTML/Template/Flexy.php';
$tmp_opts = array(
// 'forceCompile' => true,
'site_prefix' => false,
'multiSource' => true,
);
$fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
//print_R($fopts);exit;
if (!empty($fopts['DB_DataObject_translator'])) {
$tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
}
if (!empty($fopts['locale'])) {
$tmp_opts['locale'] = $fopts['locale'];
}
if (!empty($fopts['templateDir'])) {
$tmp_opts['templateDir'] = $fopts['templateDir'];
}
// override.
if (!empty($this->templateDir)) {
$tmp_opts['templateDir'] = $this->templateDir;
}
// local opt's overwrite
if (!empty($this->locale)) {
$tmp_opts['locale'] = $this->locale;
}
$htmlbody = false;
$html_tmp_opts = $tmp_opts;
$htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
if (is_string($htmltemplate->resolvePath('mail/'.$templateFile.'.body.html')) ) {
// then we have a multi-part email...
if (!empty($this->html_locale)) {
$html_tmp_opts['locale'] = $this->html_locale;
}
$htmltemplate = new HTML_Template_Flexy( $html_tmp_opts );
$htmltemplate->compile('mail/'. $templateFile.'.body.html');
$htmlbody = $htmltemplate->bufferedOutputObject($content);
$this->htmlbody = $htmlbody;
// for the html body, we may want to convert the attachments to images.
// var_dump($htmlbody);exit;
if(!empty($content->body_cls) && strlen($content->body_cls)){
$htmlbody = $this->htmlbodySetClass($htmlbody, $content->body_cls);
}
if ($this->replaceImages) {
$htmlbody = $this->htmlbodytoCID($htmlbody);
}
if ($this->css_embed) {
$htmlbody = $this->htmlbodyCssEmbed($htmlbody);
}
if ($this->css_inline && strlen($this->css_inline)) {
$htmlbody = $this->htmlbodyInlineCss($htmlbody);
}
}
$tmp_opts['nonHTML'] = true;
//$tmp_opts['debug'] = true;
// print_R($tmp_opts);
// $tmp_opts['force'] = true;
$template = new HTML_Template_Flexy( $tmp_opts );
$template->compile('mail/'. $templateFile.'.txt');
/* use variables from this object to ouput data. */
$mailtext = $template->bufferedOutputObject($content);
//print_r($mailtext);exit;
//echo "<PRE>";print_R($mailtext);
/* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
require_once 'Mail/mimeDecode.php';
require_once 'Mail.php';
$decoder = new Mail_mimeDecode($mailtext);
$parts = $decoder->getSendArray();
if (PEAR::isError($parts)) {
return $parts;
//echo "PROBLEM: {$parts->message}";
//exit;
}
$isMime = false;
require_once 'Mail/mime.php';
$mime = new Mail_mime(array(
'eol' => "\n",
//'html_encoding' => 'base64',
'html_charset' => 'utf-8',
'text_charset' => 'utf-8',
'head_charset' => 'utf-8',
));
// clean up the headers...
$parts[1]['Message-Id'] = '<' . $content->msgid .
'@' . $content->HTTP_HOST .'>';
if ($htmlbody !== false) {
// got a html headers...
if (isset($parts[1]['Content-Type'])) {
unset($parts[1]['Content-Type']);
}
$mime->setTXTBody($parts[2]);
$this->textbody = $parts[2];
$mime->setHTMLBody($htmlbody);
// var_dump($mime);exit;
foreach($this->images as $cid=>$cdata) {
$mime->addHTMLImage(
$cdata['file'],
$cdata['mimetype'],
$cid.'.'.$cdata['ext'],
true,
$cdata['contentid']
);
}
$isMime = true;
}
if(!empty($this->attachments)){
//if got a attachments
$header = $mime->headers($parts[1]);
if (isset($parts[1]['Content-Type'])) {
unset($parts[1]['Content-Type']);
}
if (!$isMime) {
if(preg_match('/text\/html/', $header['Content-Type'])){
$mime->setHTMLBody($parts[2]);
$mime->setTXTBody('This message is in HTML only');
$this->textbody = 'This message is in HTML only';
}else{
$mime->setTXTBody($parts[2]);
$this->textbody = $parts[2];
$mime->setHTMLBody('<PRE>'.htmlspecialchars($parts[2]).'</PRE>');
}
}
foreach($this->attachments as $attch){
$mime->addAttachment(
$attch['file'],
$attch['mimetype'],
(!empty($attch['name'])) ? $attch['name'] : '',
true
);
}
$isMime = true;
}
if($isMime){
$parts[2] = $mime->get();
$parts[1] = $mime->headers($parts[1]);
}
$ret = array(
'recipents' => $parts[0],
'headers' => $parts[1],
'body' => $parts[2],
'mailer' => $this
);
if ($this->rcpts !== false) {
$ret['recipents'] = $this->rcpts;
}
// if 'to' is empty, then add the recipents in there... (must be an array?
if (!empty($ret['recipents']) && is_array($ret['recipents']) &&
(empty($ret['headers']['To']) || !strlen(trim($ret['headers']['To'])))) {
$ret['headers']['To'] = implode(',', $ret['recipents']);
}
// add bcc if necessary..
if (!empty($this->bcc)) {
$ret['bcc'] = $this->bcc;
}
return $ret;
}
function send($email = false)
{
$ff = HTML_FlexyFramework::get();
$pg = $ff->page;
$email = is_array($email) ? $email : $this->toData();
if (is_a($email, 'PEAR_Error')) {
$pg->addEvent("COREMAILER-FAIL", false, "email toData failed");
return $email;
}
//$this->log( htmlspecialchars(print_r($email,true)));
///$recipents = array($this->email);
// $mailOptions = PEAR::getStaticProperty('Mail','options');
$mailOptions = isset($ff->Mail) ? $ff->Mail : array();
//print_R($mailOptions);exit;
if ($this->mail_method == 'SMTPMX' && empty($mailOptions['mailname'])) {
$pg->jerr("Mail[mailname] is not set - this is required for SMTPMX");
}
$mail = Mail::factory($this->mail_method,$mailOptions);
if ($this->debug) {
$mail->debug = (bool) $this->debug;
}
$email['headers']['Date'] = date('r');
if (PEAR::isError($mail)) {
$pg->addEvent("COREMAILER-FAIL", false, "mail factory failed");
return $mail;
}
$rcpts = $this->rcpts == false ? $email['recipents'] : $this->rcpts;
// this makes contents untrustable...
if (!empty($this->contents['bcc']) && is_array($this->contents['bcc'])) {
$rcpts =array_merge(is_array($rcpts) ? $rcpts : array($rcpts), $this->contents['bcc']);
}
$oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
if ($this->debug) {
print_r(array(
'rcpts' => $rcpts,
'email' => $email
));
}
$ret = $mail->send($rcpts,$email['headers'],$email['body']);
error_reporting($oe);
if ($ret === true) {
$pg->addEvent("COREMAILER-SENT", false,
'To: ' . ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
'Subject: ' . @$email['headers']['Subject']
);
} else {
$pg->addEvent("COREMAILER-FAIL", false,
"Sending to : " . ( is_array($rcpts) ? implode(', ', $rcpts) : $rcpts ) .
" Error: " . $ret->toString());
}
return $ret;
}
function htmlbodytoCID($html)
{
$dom = new DOMDocument();
// this may raise parse errors as some html may be a component..
@$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
$imgs= $dom->getElementsByTagName('img');
$urls = array();
foreach ($imgs as $i=>$img) {
$url = $img->getAttribute('src');
if (preg_match('#^cid:#', $url)) {
continue;
}
$me = $img->getAttribute('mailembed');
if ($me == 'no') {
continue;
}
if(!array_key_exists($url, $urls)){
$conv = $this->fetchImage($url);
$urls[$url] = $conv;
$this->images[$conv['contentid']] = $conv;
} else {
$conv = $urls[$url];
}
$img->setAttribute('origsrc', $url);
$img->setAttribute('src', 'cid:' . $conv['contentid']);
}
return $dom->saveHTML();
}
function htmlbodyCssEmbed($html)
{
$ff = HTML_FlexyFramework::get();
$dom = new DOMDocument();
// this may raise parse errors as some html may be a component..
@$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
$links = $dom->getElementsByTagName('link');
$lc = array();
foreach ($links as $link) { // duplicate as links is dynamic and we change it..!
$lc[] = $link;
}
//<link rel="stylesheet" type="text/css" href="{rootURL}/roojs1/css-mailer/mailer.css">
foreach ($lc as $i=>$link) {
//var_dump($link->getAttribute('href'));
if ($link->getAttribute('rel') != 'stylesheet') {
continue;
}
$url = $link->getAttribute('href');
$file = $ff->rootDir . $url;
if (!preg_match('#^(http|https)://#', $url)) {
$file = $ff->rootDir . $url;
if (!file_exists($file)) {
// echo $file;
$link->setAttribute('href', 'missing:' . $file);
continue;
}
} else {
$file = $this->mapurl($url);
}
$par = $link->parentNode;
$par->removeChild($link);
$s = $dom->createElement('style');
$e = $dom->createTextNode(file_get_contents($file));
$s->appendChild($e);
$par->appendChild($s);
}
return $dom->saveHTML();
}
function htmlbodyInlineCss($html)
{
$dom = new DOMDocument();
@$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
$html = $dom->getElementsByTagName('html');
$head = $dom->getElementsByTagName('head');
$body = $dom->getElementsByTagName('body');
if(!$head->length){
$head = $dom->createElement('head');
$html->item(0)->insertBefore($head, $body->item(0));
$head = $dom->getElementsByTagName('head');
}
$s = $dom->createElement('style');
$e = $dom->createTextNode($this->css_inline);
$s->appendChild($e);
$head->item(0)->appendChild($s);
return $dom->saveHTML();
/* Inline
require_once 'HTML/CSS/InlineStyle.php';
$doc = new HTML_CSS_InlineStyle($html);
$doc->applyStylesheet($this->css_inline);
$html = $doc->getHTML();
return $html;
*/
}
function htmlbodySetClass($html, $cls)
{
$dom = new DOMDocument();
@$dom->loadHTML('<?xml encoding="UTF-8">' .$html);
$body = $dom->getElementsByTagName('body');
if (!empty($body->length)) {
$body->item(0)->setAttribute('class', $cls);
} else {
$body = $dom->createElement("body");
$body->setAttribute('class', $cls);
$dom->appendChild($body);
}
return $dom->saveHTML();
}
function fetchImage($url)
{
$this->log( "FETCH : $url\n");
if ($url[0] == '/') {
$ff = HTML_FlexyFramework::get();
$file = $ff->rootDir . $url;
require_once 'File/MimeType.php';
$m = new File_MimeType();
$mt = $m->fromFilename($file);
$ext = $m->toExt($mt);
return array(
'mimetype' => $mt,
'ext' => $ext,
'contentid' => md5($file), // mailer makes md5 cid's' -- cid with attachment-** are done by mailer.
'file' => $file
);
}
//print_R($url); exit;
if (preg_match('#^file:///#', $url)) {
$file = preg_replace('#^file://#', '', $url);
require_once 'File/MimeType.php';
$m = new File_MimeType();
$mt = $m->fromFilename($file);
$ext = $m->toExt($mt);
return array(
'mimetype' => $mt,
'ext' => $ext,
'contentid' => md5($file),
'file' => $file
);
}
// CACHE???
// 2 files --- the info file.. and the actual file...
// add user
// unix only...
$uinfo = posix_getpwuid( posix_getuid () );
$user = $uinfo['name'];
$cache = ini_get('session.save_path')."/Pman_Core_Mailer-{$user}/" . md5($url);
if ($this->cache_images &&
file_exists($cache) &&
filemtime($cache) > strtotime('NOW - 1 WEEK')
) {
$ret = json_decode(file_get_contents($cache), true);
$this->log("fetched from cache");
$ret['file'] = $cache . '.data';
return $ret;
}
if (!file_exists(dirname($cache))) {
mkdir(dirname($cache),0700, true);
}
require_once 'HTTP/Request.php';
$real_url = str_replace(' ', '%20', $this->mapurl($url));
$a = new HTTP_Request($real_url);
$a->sendRequest();
$data = $a->getResponseBody();
$this->log("got file of size " . strlen($data));
$this->log("save contentid " . md5($url));
file_put_contents($cache .'.data', $data);
$mt = $a->getResponseHeader('Content-Type');
require_once 'File/MimeType.php';
$m = new File_MimeType();
$ext = $m->toExt($mt);
$ret = array(
'mimetype' => $mt,
'ext' => $ext,
'contentid' => md5($url)
);
file_put_contents($cache, json_encode($ret));
$ret['file'] = $cache . '.data';
return $ret;
}
function mapurl($in)
{
foreach($this->urlmap as $o=>$n) {
if (strpos($in,$o) === 0) {
$ret =$n . substr($in,strlen($o));
$this->log("mapURL in $in = $ret");
return $ret;
}
}
$this->log("mapurl no change - $in");
return $in;
}
function log($val)
{
if (!$this->debug) {
return;
}
if ($this->debug < 2) {
echo '<PRE>' . print_r($val,true). "\n";
return;
}
$fh = fopen('/tmp/core_mailer.log', 'a');
fwrite($fh, date('Y-m-d H:i:s -') . json_encode($val) . "\n");
fclose($fh);
}
}