-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDef_MailTools.php
More file actions
executable file
·323 lines (270 loc) · 8.26 KB
/
Def_MailTools.php
File metadata and controls
executable file
·323 lines (270 loc) · 8.26 KB
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
<?php
/**
* A class to provide SMTP-mailing functionality and access to select
* mail services.
*
* @package Def_MailTools
*/
class Def_MailTools extends ContentController
{
protected static $admin_mail = '';
protected static $admin_mail_name = '';
protected static $sendgrid_username = '';
protected static $sendgrid_password = '';
protected static $smtp_username = '';
protected static $smtp_password = '';
/**
* Set the default admin email and name
*
* @param string $mail
* @param string $name
* @return void
*/
public static function set_admin_mail($mail, $name)
{
self::$admin_mail = $mail;
self::$admin_mail_name = $name;
}
/**
* Get the Admin email adress
*
* @return string
*/
public static function get_admin_mail()
{
if (self::$admin_mail == '')
{
return 'noreply@' . parse_url(Director::absoluteBaseURL(), PHP_URL_HOST);
} else {
return self::$admin_mail;
}
}
/**
* Get the Admin name
*
* @return string
*/
public static function get_admin_mail_name()
{
if (self::$admin_mail_name == '')
{
return self::$admin_mail;
} else {
return self::$admin_mail_name;
}
}
/**
* Set the SendGrid (http://sendgrid.com/) credentials
*
* @param string $username
* @param string $password
* @return void
*/
public static function set_sendgrid_credentials($username, $password)
{
self::$sendgrid_username = $username;
self::$sendgrid_password = $password;
}
/**
* Set the SMTP credentials
*
* @param string $username
* @param string $password
* @return void
*/
public static function set_smtp_credentials($username, $password)
{
self::$smtp_username = $username;
self::$smtp_password = $password;
}
/**
* Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID.
*
* @return string
*/
public static function absolute_link_shortcode_handler($arguments, $content = null, $parser = null) {
if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;
if (
!($page = DataObject::get_by_id('SiteTree', $arguments['id'])) // Get the current page by ID.
&& !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version.
&& !($page = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\'')) // Link to 404 page directly.
) {
return; // There were no suitable matches at all.
}
if($content) {
return sprintf('<a href="%s">%s</a>', $page->AbsoluteLink(), $parser->parse($content));
} else {
return $page->AbsoluteLink();
}
}
/**
* Build the different (HTML + PlainText) parts of the email
* Also allows to select header and footer template.
*
* @param array $content
* @param string $plainText
* @param string $mailHeader
* @param string $mailFooter
*
* @return array
*/
function BuildMail($content, $plainText = '', $mailHeader = 'MailHeader', $mailFooter = 'MailFooter')
{
// Change relative links to assets to absolute
$content = str_replace('src="assets', 'src="' . Director::absoluteBaseURL() . 'assets', $content);
$content = str_replace('href="assets', 'href="' . Director::absoluteBaseURL() . 'assets', $content);
// Change internal links ([sitetree_link id=n]) to proper absolute links
$parser = new ShortcodeParser();
$parser->register('sitetree_link', array('Def_MailTools', 'absolute_link_shortcode_handler'));
$content = $parser->parse($content);
// construct the HTML mail
$html = $this->renderWith($mailHeader);
$html .= $content;
$html .= $this->renderWith($mailFooter);
// Make styles inline
$convertedHTML = new CSSToInlineStyles($html, '');
$convertedHTML->setUseInlineStylesBlock('true');
$convertedHTML = $convertedHTML->convert();
// Create a plain text version if one wasn't specified
if($plainText == '') { $plainText = GetPlainTextMail::getPlainText($convertedHTML, true, true); }
// Save both versions in an array and return it
$myMail = array("htmlMail" => $convertedHTML, "textMail" => $plainText);
return $myMail;
}
/**
* Check if the Admin also needs to be mailed. And filter out duplicate mail addresses.
*
* @param array $recipients
* @param boolean $also_send_to_admin
* @param string $admin_mail
*
* @return array
*/
private static function filter_recipients($recipients, $also_send_to_admin, $admin_mail)
{
// Check if there also needs to go a mail to the admin
if($also_send_to_admin) { array_push($recipients, $admin_mail); }
// Filter out duplicate recipients
$recipients = array_unique($recipients);
return $recipients;
}
/**
* A basic function for that sets up the SMTP Mail
*
* @param array $recipients
* @param string $title
* @param array $content
* @param string $server
* @param integer $port
*
* @return PHPMailer Object
*/
public static function setup_basic_smtp_mail($recipients, $title, $content, $server, $port)
{
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = $server . ";" . $port;
$mail->Port = $port;
// Get the admin mail
$adminMail = self::get_admin_mail();
// Get the admin name
$adminName = self::get_admin_mail_name();
// Add sender details
$mail->From = $adminMail;
$mail->FromName = $adminName;
// Set the title
$mail->Subject = $title;
// Set the content
$mail->AltBody = $content["textMail"];
$mail->MsgHTML($content["htmlMail"]);
return $mail;
}
/**
* Send emails through SMTP
*
* @param array $recipients
* @param string $title
* @param array $content
* @param boolean $also_send_to_admin
* @param string $server
* @param integer $port
* @param boolean $use_ssl
* @param boolean $use_auth
*
* @return boolean
*/
public static function send_mail_SMTP($recipients, $title, $content, $also_send_to_admin = false, $server, $port = "25", $use_ssl = false, $use_auth = false)
{
$mail = self::setup_basic_smtp_mail($recipients, $title, $content, $server, $port);
// See if authentitcation is needed
if($use_auth) {
// Return false if no username is set
if(self::$smtp_username == "") { return false; }
$mail->SMTPAuth = 'true';
$mail->Username = self::$smtp_username;
$mail->Password = self::$smtp_password;
}
// Set SSL if needed
if($use_ssl) { $mail->SMTPSecure = 'ssl'; }
$recipients = self::filter_recipients($recipients, $also_send_to_admin, $mail->From);
// Add recipients
foreach($recipients as $recipient) { $mail->AddAddress($recipient); }
// Send the mail
if(!$mail->Send())
{
return false;
} else {
return true;
}
}
/**
* Send emails through Sendgrid
*
* @param array $recipients
* @param string $title
* @param array $content
* @param string $category
* @param boolean $also_send_to_admin
* @param string $server
* @param integer $port
* @param array $extraValues
*
* @return boolean
*/
public static function send_mail_sendgrid($recipients, $title, $content, $category = "Uncategorized", $also_send_to_admin = false, $server ="smtp.sendgrid.com", $port = "465", $extraValues = "")
{
$mail = self::setup_basic_smtp_mail($recipients, $title, $content, $server, $port);
// Fill in SendGrid credentials
if(self::$sendgrid_username == "" || self::$sendgrid_password == "") { return false; }
$mail->SMTPAuth = 'true';
$mail->Username = self::$sendgrid_username;
$mail->Password = self::$sendgrid_password;
$mail->SMTPSecure = 'ssl';
// Prepare the special header for SendGrid
$hdr = new SmtpApiHeader();
// Get recipients and add them to the header
$recipients = self::filter_recipients($recipients, $also_send_to_admin, $mail->From);
if($recipients != '') { $hdr->addTo($recipients); }
// Add a SendGrid category
$hdr->setCategory($category);
/* add ExtraValues to be parsed by SendGrid if needed */
if($extraValues)
{
foreach($extraValues as $key=>$value)
{
$hdr->addSubVal($key, $value);
}
}
// Add the special API- header to the mail
$mail->AddCustomHeader('X-SMTPAPI:' . $hdr->asJSON());
// Add a To address - PHPMailer needs this, but SendGrid won't use it
$mail->AddAddress($mail->From);
// Send the mail
if(!$mail->Send())
{
return false;
} else {
return true;
}
}
}