-
Notifications
You must be signed in to change notification settings - Fork 204
/
Mailer.php
98 lines (81 loc) · 2.35 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
<?php
require 'phpmailer/PHPMailerAutoload.php';
require_once 'Flux/LogFile.php';
class Flux_Mailer {
protected $pm;
protected static $errLog;
protected static $log;
public function __construct()
{
if (!self::$errLog) {
self::$errLog = new Flux_LogFile(FLUX_DATA_DIR.'/logs/errors/mail/'.date('Ymd').'.log');
}
if (!self::$log) {
self::$log = new Flux_LogFile(FLUX_DATA_DIR.'/logs/mail/'.date('Ymd').'.log');
}
$this->pm = $pm = new PHPMailer();
$this->errLog = self::$errLog;
$this->log = self::$log;
if (Flux::config('MailerUseSMTP')) {
$pm->IsSMTP();
if (is_array($hosts=Flux::config('MailerSMTPHosts'))) {
$hosts = implode(';', $hosts);
}
$pm->Host = $hosts;
if ($user=Flux::config('MailerSMTPUsername')) {
$pm->SMTPAuth = true;
if (Flux::config('MailerSMTPUseTLS')) {
$pm->SMTPSecure = 'tls';
}
if (Flux::config('MailerSMTPUseSSL')) {
$pm->SMTPSecure = 'ssl';
}
if ($port=Flux::config('MailerSMTPPort')) {
$pm->Port = (int)$port;
}
$pm->Username = $user;
if ($pass=Flux::config('MailerSMTPPassword')) {
$pm->Password = $pass;
}
}
}
// From address.
$pm->setFrom(Flux::config('MailerFromAddress'), Flux::config('MailerFromName'));
}
public function send($recipient, $subject, $template, array $templateVars = array())
{
if (array_key_exists('_ignoreTemplate', $templateVars) && $templateVars['_ignoreTemplate']) {
$content = $template;
} else {
$templatePath = FLUX_DATA_DIR."/templates/$template.php";
if (!file_exists($templatePath)) {
return false;
}
$find = array();
$repl = array();
foreach ($templateVars as $key => $value) {
$find[] = '{'.$key.'}';
$repl[] = $value;
}
ob_start();
include $templatePath;
$content = ob_get_clean();
if (!empty($find) && !empty($repl)) {
$content = str_replace($find, $repl, $content);
}
}
$this->pm->isHTML(true);
$this->pm->CharSet = 'UTF-8';
$this->pm->AddAddress($recipient);
$this->pm->Subject = $subject;
$this->pm->msgHTML($content);
if ($sent=$this->pm->Send()) {
self::$log->puts("sent e-mail -- Recipient: $recipient, Subject: $subject");
}
else {
self::$errLog->puts("{$this->pm->ErrorInfo} (while attempting -- Recipient: $recipient, Subject: $subject)");
}
return $sent;
}
}
?>