-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamDataMailer.php
336 lines (308 loc) · 11.9 KB
/
TeamDataMailer.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
<?php
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
/**
* TeamData plugin Mailer class for sending email.
* Uses PHPMailer for actual mailing purposes <https://github.com/PHPMailer/PHPMailer>
*/
class TeamDataMailer extends TeamDataBase {
/**
* Function to send email to one or more lists.
* @param array $list_ids An array of the list IDs to send mail to. (The IDs are from the team_data_list table.) Specifying -1 will send to all active members with valid email addresses.
* @param string $subject Required subject for the email
* @param string $message_content Required plain text to use as the body of the message. This will be HTML-escaped using wp_kses() before inclusion in the email to avoid any injection.
* @param array $options Optional array of options. Valid keys include:
* - ReplyTo: email address(es) to specify in ReplyTo
* - From: email address for From
* - FromName: Name to display in From
*/
public function send_mail($list_ids = null,$subject = '',$message_content = '',$options = null) {
$email_enabled = $this->get_option('email_enabled');
if (empty($email_enabled)) return false;
$message_content = trim($message_content);
$subject = trim($subject);
if (($list_ids == null) || ($subject == '') || ($message_content == '')) {
return false;
}
$list_names = array();
$to = $this->build_to($list_ids, $list_names);
if ($this->debug_flag) $this->debug('To List: ' . json_encode($to));
$mail_config = $this->get_mail_config();
if ($this->debug_flag) $this->debug('Mail Config: ' . json_encode($mail_config));
$email_list_name_custom_header = $this->get_option('email_list_name_custom_header');
if ((!empty($email_list_name_custom_header)) && (count($list_names) > 0)) {
$mail_config[$email_list_name_custom_header] = implode($list_names,',');
}
$html_content = $this->build_html_content($message_content);
$text_footer = $this->get_option('text_footer');
if (empty($text_footer)) {
$text_footer = '';
}
else {
$text_footer = "\n\n" . $text_footer;
}
// ensure that <li> elements are transformed to something like a plain text list
// note that ordered lists are converted to a "bulleted" list
$text_content = str_replace('<li>',' - ', $message_content);
$text_content = wp_kses( $text_content, array() ) . $text_footer;
$errors = array();
$ok_count = 0;
$mailer = null;
foreach ($to as $to_pos => $email_data) {
$mailer = $this->build_mailer($mail_config,$mailer);
$mailer->Subject = $subject;
if ( !empty($email_data['email']) ) {
$mailer->AddAddress($email_data['email']);
}
if (!empty($options['ReplyTo'])) {
$mailer->AddReplyTo($options['ReplyTo']);
}
if ( !empty($email_data['from_email']) ) {
$mailer->AddReplyTo($email_data['from_email']);
$mailer->From = $email_data['from_email'];
$mailer->FromName = ( !empty($email_data['from_name']) ? $email_data['from_name'] : $email_data['from_email'] );
}
elseif ( !empty($options['From']) ) {
$mailer->From = $options['From'];
$mailer->FromName = ( !empty($options['FromName']) ? $options['FromName'] : $options['From'] );
}
if ($html_content == '') {
$mailer->Body = $text_content;
}
else {
$mailer->MsgHTML($html_content);
$mailer->AltBody = $text_content;
}
if ($this->debug_flag) {
$debug_data = array('$subject' => $subject, '$mail_config' => $mail_config, '$options' => $options, '$mailer' => $mailer );
$this->debug('Sending email; debug data = ' . json_encode($debug_data) );
}
$ok = $mailer->Send();
if ($ok) {
$ok_count++;
}
else {
$errors[] = array( 'email' => $email, 'error' => $mailer->ErrorInfo );
}
}
if ($this->debug_flag) {
$this->debug('$errors = ' . json_encode($errors));
}
$summary_to = $this->get_option('email_summary_to');
if ( !empty($summary_to) ) {
$mailer = $this->build_mailer($mail_config,$mailer);
$mailer->Subject = sprintf( __( 'Summary: %1$s' , 'team_data' ), $subject );
if ( !empty($options['From']) ) {
$mailer->AddReplyTo($options['From']);
$mailer->From = $options['From'];
$mailer->FromName = ( !empty($options['FromName']) ? $options['FromName'] : $options['From'] );
}
$summary_addresses = explode( ';', $summary_to );
foreach ($summary_addresses as $summary_address) {
$mailer->AddAddress($summary_address);
}
if (!empty($options['ReplyTo'])) {
$mailer->AddReplyTo($options['ReplyTo']);
}
$username = '?';
$current_user = wp_get_current_user();
if (!empty($current_user)) $username = $current_user->user_login;
$summary_footer = "\r\n\r\n__________________________________\r\n" . __('Summary', 'team_data') . "\r\n";
$summary_footer .= sprintf(__('User %1$s sent email to %2$d out of %3$d recipients in the following lists: %4$s', 'team_data'),$username,$ok_count,count($to),implode($list_names,", "));
$summary_text = $text_content . $summary_footer;
if ($html_content == '') {
$mailer->Body = $summary_text;
}
else {
$summary_html = $this->build_html_content($message_content . $summary_footer);
$mailer->MsgHTML($summary_html);
$mailer->AltBody = $summary_text;
}
$mailer->Send();
}
return true;
}
/**
* Function to get an initialized PHPMailer object.
* @return PHPMailer
*/
public function get_mailer() {
$mail_config = $this->get_mail_config();
if ($this->debug_flag) $this->debug('Mail Config: ' . json_encode($mail_config));
return $this->build_mailer($mail_config);
}
protected function build_mailer($options,$mailer = null) {
if (!$mailer) {
$mailer = new PHPMailer();
}
else { // make sure we clean up the existing $mailer
$mailer->ClearAllRecipients();
$mailer->ClearCustomHeaders();
$mailer->ClearReplyTos();
}
foreach ($options as $prop_name => $prop_value) {
if ($prop_name == 'Unsubscribe') {
$mailer->AddCustomHeader('List-Unsubscribe', '<mailto:' . $prop_value . '>');
}
elseif ($prop_name == 'ReplyTo') {
$mailer->AddReplyTo($prop_value);
}
elseif (substr($prop_name,0,2) == 'X-') {
$mailer->AddCustomHeader($prop_name, $prop_value);
}
else {
$mailer->{$prop_name} = $prop_value;
}
}
return $mailer;
}
protected function build_html_content($message) {
$html = '';
$template = $this->get_option('html_template');
if (($template != false) && ($template != '')) {
// strip most HTML tags except for this whitelist
$permitted_tags = array(
'b' => array(),
'em' => array(),
'i' => array(),
'strong' => array(),
'u' => array(),
'img' => array(
'src' => array(),
'alt' => array(),
'style' => array(),
'class' => array(),
'height' => array(),
'width' => array(),
),
'ul' => array(
'style' => array(),
),
'li' => array(),
'ol' => array(
'style' => array(),
),
);
$msg_stripped = wp_kses($message, $permitted_tags);
// remove all carriage returns
$msg_stripped = str_replace("\r", "", $msg_stripped);
// convert multiple newlines to <p>
$msg_stripped = preg_replace("/\n\n+/", "</p><p>", $msg_stripped);
// remove any trailing whitespace
$msg_stripped = trim($msg_stripped);
// convert any remaining newlines to <br/> and wrap in <p>
$msg_stripped = '<p>' . str_replace("\n", '<br/>', $msg_stripped) . '</p>';
// if we have multiple <br/> tags, switch to <p>
$msg_stripped = str_replace('<br/><br/>', '</p><p>', $msg_stripped);
// remove any empty <p> tags
$msg_stripped = str_replace('<p></p>', '', $msg_stripped);
// remove <br/> from between <ul>, <ol> and <li> elements
// NOTE: the current logic doesn't handle ul and ol elements with style attributes
$msg_stripped = str_replace('<ul><br/><li>', '<ul><li>', $msg_stripped);
$msg_stripped = str_replace('</li><br/></ul>', '</li></ul>', $msg_stripped);
$msg_stripped = str_replace('<ol><br/><li>', '<ol><li>', $msg_stripped);
$msg_stripped = str_replace('</li><br/></ol>', '</li></ol>', $msg_stripped);
$msg_stripped = str_replace('</li><br/><li>', '</li><li>', $msg_stripped);
// add manual styles for <p> elements
$msg_stripped = str_replace('<p>', '<p style="margin: 1em 0em;">', $msg_stripped);
// put stripped content in template
$html = str_replace('[[CONTENT]]', $msg_stripped, $template);
if ( strpos($html,'[[SENDTIME]]') !== false) {
$timezone = $this->get_option('email_timezone');
if ( empty( $timezone ) ) {
$timezone = 'America/New_York';
}
$curr_time = new DateTime(null, new DateTimezone($timezone));
$send_time = sprintf( __('Email sent on %1$s at %2$s.', 'team_data' ), $curr_time->format( __('n/j/Y', 'team_data') ), $curr_time->format( __('g:i:s A', 'team_data' ) ) );
$html = str_replace('[[SENDTIME]]', $send_time ,$html);
}
}
return wordwrap($html,78);
}
protected function get_mail_config() {
$options = array();
$use_smtp = ('1' == $this->get_option('use_smtp'));
$options['CharSet'] = 'UTF-8';
$options['WordWrap'] = 78;
$default_from = $this->get_option('email_from');
if ( !empty($default_from) ) {
$options['From'] = $default_from;
$options['Sender'] = $default_from;
$options['ReplyTo'] = $default_from;
}
$default_from_name = $this->get_option('email_from_name');
if ( !empty($default_from_name) ) {
$options['FromName'] = $default_from_name;
}
$unsubscribe_address = $this->get_option('email_unsubscribe_address');
if ( !empty($unsubscribe_address) ) {
$options['Unsubscribe'] = $unsubscribe_address;
}
if ($use_smtp) {
$options['Mailer'] = 'smtp';
$options['Timeout'] = 5;
$server = $this->get_option('smtp_server');
if (!empty($server)) {
$options['Host'] = $server;
}
$port = $this->get_option('smtp_port');
if (!empty($port)) {
$options['Port'] = $port;
}
$conn_sec = $this->get_option('smtp_conn_sec');
if ( !empty($conn_sec) ) {
$options['SMTPSecure'] = $conn_sec;
}
$user = $this->get_option('smtp_user');
$pass = $this->get_option('smtp_password');
if ((!empty($user)) && (!empty($pass))) {
$options['Username'] = $user;
$options['Password'] = $pass;
$options['SMTPAuth'] = true;
}
}
return $options;
}
protected function build_to($list_ids = null, &$list_names) {
global $wpdb;
$to = array();
$list_names = array();
if ($list_ids == null) return array();
// use $done to track emails that have already been added
$done = array();
$show_errors = $wpdb->hide_errors();
if ($list_ids == -1) {
if ($this->get_option('allow_all_member_mail') == '1') {
$list_names[] = __('All Members', 'team_data');
$emails = $wpdb->get_results('SELECT email FROM ' . $this->tables->member . " WHERE active = 1 AND email <> ''");
foreach ($emails as $email_result) {
if ( !isset($done[$email_result->email]) ) {
$to[] = array('email' => $email_result->email, );
$done[$email_result->email] = true;
}
}
}
}
else {
// force IDs to be positive integers
array_map( 'absint', $list_ids );
$name_sql_base = 'SELECT name, from_email, from_name FROM ' . $this->tables->list . ' WHERE id = %d';
$member_sql_base = 'SELECT m.email FROM ' . $this->tables->member . ' m, ' . $this->tables->member_list . " ml WHERE ml.member_id = m.id AND m.active = 1 AND m.email <> '' AND ml.list_id = %d";
foreach ($list_ids as $pos => $list_id) {
$name_sql = $wpdb->prepare($name_sql_base, $list_id);
$list_data = $wpdb->get_row( $name_sql );
$list_names[] = (empty($list_data->name) ? $list_id : $list_data->name);
$member_sql = $wpdb->prepare($member_sql_base, $list_id);
$emails = $wpdb->get_results($member_sql);
foreach ($emails as $email_result) {
if ( !isset($done[$email_result->email]) ) {
$to[] = array( 'email' => $email_result->email, 'from_email' => $list_data->from_email, 'from_name' => $list_data->from_name, );
$done[$email_result->email] = true;
}
}
}
}
if ($show_errors) $wpdb->show_errors();
return $to;
}
}