forked from idno/known
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Email.php
194 lines (168 loc) · 6.72 KB
/
Email.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
<?php
namespace Idno\Core {
class Email extends \Idno\Common\Component
{
public $message;
function init()
{
// Using SwiftMailer to establish a message
try {
require_once site()->config()->path . '/external/swiftmailer/lib/swift_required.php';
$this->message = \Swift_Message::newInstance();
} catch (\Exception $e) {
site()->session()->addErrorMessage("Something went wrong and we couldn't create the email message to send.");
}
}
/**
* Set the subject of the message
* @param $subject
*/
function setSubject($subject)
{
return $this->message->setSubject($subject);
}
/**
* Set the "From" address of the message
* @param $email The email address of the account
* @param $name The name of the account
* @return mixed
*/
function setFrom($email, $name = '')
{
if (!empty($name)) {
return $this->message->addFrom(array($name => $email));
}
return $this->message->addFrom($email);
}
/**
* Add a recipient
* @param string $email The email address of the recipient
* @param string $name The name of the recipient (optional)
* @return mixed
*/
function addTo($email, $name = '')
{
if (!empty($name)) {
return $this->message->addTo(array($name => $email));
}
return $this->message->addTo($email);
}
/**
* Adds an email to the BCC list
* @param $email
* @return mixed
*/
function addBcc($email)
{
return $this->message->addBcc($email);
}
/**
* Add a "reply to" message
* @param $email
* @param string $name
* @return mixed
*/
function setReplyTo($email, $name = '')
{
if (!empty($name)) {
return $this->message->addReplyTo(array($name => $email));
}
return $this->message->addReplyTo($email);
}
/**
* Sets the HTML body of the message (optionally setting it inside the email pageshell as defined by the email template)
* @param $body The formatted HTML body text of the message
* @param bool $shell Should the message be placed inside the pageshell? Default: true
* @return mixed
*/
function setHTMLBody($body, $shell = true)
{
if ($shell) {
$t = clone site()->template();
$t->setTemplateType('email');
$message = $t->__(array('body' => $body))->draw('shell');
} else {
$message = $body;
}
return $this->message->setBody($message, 'text/html');
}
/**
* Given the name of a template and a set of variables to include, generates an HTML body and adds it to the message
* @param $template_name
* @param array $vars
* @return mixed
*/
function setHTMLBodyFromTemplate($template_name, $vars = array())
{
$t = clone site()->template();
$t->setTemplateType('email');
$body = $t->__($vars)->draw($template_name);
return $this->setHTMLBody($body);
}
/**
* Set the text only component of an email.
* @param type $template_name
* @param type $vars
* @return mixed
*/
function setTextBodyFromTemplate($template_name, $vars = array())
{
$t = clone site()->template();
$t->setTemplateType('email-text');
$body = $t->__($vars)->draw($template_name);
return $this->setTextBody($body);
}
/**
* Sets the plain text body of the message
* @param string $body The body of the message
* @return mixed
*/
function setTextBody($body)
{
return $this->message->addPart($body, 'text/plain');
}
/**
* Send the message
* @return int
*/
function send()
{
try {
if ($smtp_host = site()->config()->smtp_host) {
$transport = \Swift_SmtpTransport::newInstance($smtp_host);
if ($smtp_username = site()->config()->smtp_username) {
$transport->setUsername($smtp_username);
if ($smtp_password = site()->config()->smtp_password) {
$transport->setPassword($smtp_password);
}
}
} else {
$transport = \Swift_SmtpTransport::newInstance();
}
if (!empty(site()->config()->smtp_port)) {
$transport->setPort(site()->config()->smtp_port);
}
if (!empty(site()->config()->smtp_secure)) {
switch (site()->config()->smtp_secure) {
case 'tls':
$transport->setEncryption('tls');
break;
case 'ssl':
$transport->setEncryption('ssl');
break;
}
}
$mailer = \Swift_Mailer::newInstance($transport);
// Set the "from" address
if ($from_email = site()->config()->from_email) {
$this->message->setFrom($from_email, site()->config()->title);
}
return $mailer->send(site()->triggerEvent('email/send', ['email' => $this], $this->message));
} catch (\Exception $e) {
// Lets log errors rather than silently drop them
\Idno\Core\site()->logging()->log($e->getMessage(), LOGLEVEL_ERROR);
}
return 0;
}
}
}