class.DisableEmailsPHPMailerMock.php
8.44 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
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
<?php
namespace uncanny_learndash_toolkit;
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\phpmailerException;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* mock of PHPMailer, to support hookers that need to access PHPMailer properties
* uses a private instance of PHPMailer, but doesn't permit sending emails through it
*/
class DisableEmailsPHPMailerMock {
private $phpmailer;
/**
* initialise mock object, creating private PHPMailer instance to handle allowed calls and properties
*/
public function __construct() {
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
$this->phpmailer = new PHPMailer( true );
// build map of allowed function calls
$this->allowed_calls = array_flip( [
'isHTML',
'addAddress',
'addCC',
'addBCC',
'addReplyTo',
'setFrom',
'addrAppend',
'addrFormat',
'wrapText',
'utf8CharBoundary',
'setWordWrap',
'createHeader',
'getMailMIME',
'getSentMIMEMessage',
'createBody',
'headerLine',
'textLine',
'addAttachment',
'getAttachments',
'encodeString',
'encodeHeader',
'hasMultiBytes',
'base64EncodeWrapMB',
'encodeQP',
'encodeQ',
'addStringAttachment',
'addEmbeddedImage',
'addStringEmbeddedImage',
'inlineImageExists',
'attachmentExists',
'alternativeExists',
'clearAddresses',
'clearCCs',
'clearBCCs',
'clearReplyTos',
'clearAllRecipients',
'clearAttachments',
'clearCustomHeaders',
'setError',
'rfcDate',
'isError',
'fixEOL',
'addCustomHeader',
'msgHTML',
'html2text',
'filenameToType',
'mb_pathinfo',
'set',
'secureHeader',
'normalizeBreaks',
'sign',
'DKIM_QP',
'DKIM_Sign',
'DKIM_HeaderC',
'DKIM_BodyC',
'DKIM_Add',
'validateAddress',
] );
}
/**
* simulate WordPress call to PHPMailer
*
* @param string|array $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject
* @param string $message Message contents
* @param string|array $headers Optional. Additional headers.
* @param string|array $attachments Optional. Files to attach.
*
* @return bool
*/
public function wpmail( $to, $subject, $message, $headers, $attachments ) {
$plugin = DisableEmailsPlugin::getInstance();
// get the site domain and get rid of www.
if ( isset( $_SERVER['SERVER_NAME'] ) ) {
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $sitename, 0, 4 ) === 'www.' ) {
$sitename = substr( $sitename, 4 );
}
} else {
// likely running from WP-CLI with no hostname set, so fake it
$sitename = 'server-name.invalid';
}
// set default From name and address
$this->phpmailer->FromName = 'WordPress';
$this->phpmailer->From = 'wordpress@' . $sitename;
// let hookers change the function arguments if settings allow
if ( $plugin->options['wp_mail'] ) {
extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ), EXTR_IF_EXISTS );
}
// set mail's subject and body
$this->phpmailer->Subject = $subject;
$this->phpmailer->Body = $message;
// headers
if ( ! empty( $headers ) ) {
if ( ! is_array( $headers ) ) {
// Explode the headers out, so this function can take both
// string headers and an array of headers.
$headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
}
foreach ( $headers as $header ) {
// check for pseudo-headers
if ( strpos( $header, ':' ) === false ) {
// TODO: handle multipart boundaries
continue;
}
list( $name, $content ) = explode( ':', trim( $header ), 2 );
$name = trim( $name );
$content = trim( $content );
switch ( strtolower( $name ) ) {
// Mainly for legacy -- process a From: header if it's there
case 'from':
$this->_setFrom( $content );
break;
case 'cc':
$this->_addCC( explode( ',', $content ) );
break;
case 'bcc':
$this->_addBCC( explode( ',', $content ) );
break;
case 'content-type':
$this->_setContentType( $content );
break;
default:
$this->phpmailer->AddCustomHeader( "$name: $content" );
break;
}
}
}
// attachments
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) {
try {
$this->phpmailer->AddAttachment( $attachment );
} catch ( phpmailerException $e ) {
continue;
}
}
}
if ( $plugin->options['wp_mail_from'] ) {
$this->phpmailer->From = apply_filters( 'wp_mail_from', $this->phpmailer->From );
}
if ( $plugin->options['wp_mail_from_name'] ) {
$this->phpmailer->FromName = apply_filters( 'wp_mail_from_name', $this->phpmailer->FromName );
}
if ( $plugin->options['wp_mail_content_type'] ) {
$this->phpmailer->ContentType = apply_filters( 'wp_mail_content_type', $this->phpmailer->ContentType );
}
if ( $plugin->options['wp_mail_charset'] ) {
$this->phpmailer->CharSet = apply_filters( 'wp_mail_charset', $this->phpmailer->CharSet );
}
if ( $plugin->options['phpmailer_init'] ) {
do_action( 'phpmailer_init', $this->phpmailer );
}
return true;
}
/**
* set a different From address and potentially, name
*
* @param string $from
*/
protected function _setFrom( $from ) {
// check for address in format "Some Name <address@example.com>"
if ( preg_match( '/(.*)<(.+)>/', $from, $matches ) ) {
$this->phpmailer->FromName = trim( $matches[1] );
$this->phpmailer->From = $matches[2];
} else {
$this->phpmailer->From = trim( $from );
}
}
/**
* add CC address(es)
*
* @param array $addresses
*/
protected function _addCC( $addresses ) {
foreach ( $addresses as $address ) {
if ( empty( $address ) ) {
continue;
}
try {
// check for address in format "Some Name <address@example.com>"
if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
$name = trim( $matches[1] );
$address = trim( $matches[2] );
$this->phpmailer->addCC( $address, $name );
} else {
$this->phpmailer->addCC( $address );
}
} catch ( phpmailerException $e ) {
continue;
}
}
}
/**
* add BCC addresses
*
* @param array $addresses
*/
protected function _addBCC( $addresses ) {
foreach ( $addresses as $address ) {
if ( empty( $address ) ) {
continue;
}
try {
// check for address in format "Some Name <address@example.com>"
if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
$name = trim( $matches[1] );
$address = trim( $matches[2] );
$this->phpmailer->addBCC( $address, $name );
} else {
$this->phpmailer->addBCC( $address );
}
} catch ( phpmailerException $e ) {
continue;
}
}
}
/**
* set content type
*
* @param string $content_type
*/
protected function _setContentType( $content_type ) {
if ( strpos( $content_type, ';' ) !== false ) {
list( $type, $charset ) = explode( ';', $content_type );
$this->phpmailer->ContentType = trim( $type );
if ( false !== stripos( $charset, 'charset=' ) ) {
$this->phpmailer->CharSet = trim( str_ireplace( [ 'charset=', '"' ], '', $charset ) );
}
} else {
$this->phpmailer->ContentType = trim( $content_type );
}
}
/**
* passthrough for setting PHPMailer properties
*
* @param string $name
* @param mixed $value
*/
public function __set( $name, $value ) {
$this->phpmailer->$name = $value;
}
/**
* passthrough for getting PHPMailer properties
*
* @param string $name
*
* @return mixed
*/
public function __get( $name ) {
return $this->phpmailer->$name;
}
/**
* catchall for methods we just want to ignore
*/
public function __call( $name, $args ) {
if ( isset( $this->allowed_calls[ $name ] ) ) {
switch ( count( $args ) ) {
case 1:
return $this->phpmailer->$name( $args[0] );
case 2:
return $this->phpmailer->$name( $args[0], $args[1] );
case 3:
return $this->phpmailer->$name( $args[0], $args[1], $args[2] );
case 4:
return $this->phpmailer->$name( $args[0], $args[1], $args[2], $args[3] );
case 5:
return $this->phpmailer->$name( $args[0], $args[1], $args[2], $args[3], $args[4] );
case 6:
return $this->phpmailer->$name( $args[0], $args[1], $args[2], $args[3], $args[4], $args[5] );
default:
return $this->phpmailer->$name();
}
}
return false;
}
/**
* catchall for methods we just want to ignore
*/
public static function __callStatic( $name, $args ) {
return false;
}
}