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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
<?php
namespace WPForms\Emails;
use WPForms\Emails\Templates\General;
/** * Mailer class to wrap wp_mail(). * * @since 1.5.4 */ class Mailer {
/** * Email address to send to. * * @since 1.5.4 * * @var string */ private $to_email;
/** * CC addresses (comma delimited). * * @since 1.5.4 * * @var string */ private $cc;
/** * From address. * * @since 1.5.4 * * @var string */ private $from_address;
/** * From name. * * @since 1.5.4 * * @var string */ private $from_name;
/** * Reply to address. * * @since 1.5.4 * * @var string */ private $reply_to;
/** * Email headers. * * @since 1.5.4 * * @var string */ private $headers;
/** * Email content type. * * @since 1.5.4 * * @var string */ private $content_type;
/** * Email attachments. * * @since 1.5.4 * * @var string */ private $attachments;
/** * Email subject. * * @since 1.5.4 * * @var string */ private $subject;
/** * Email message. * * @since 1.5.4 * * @var string */ private $message;
/** * Email template. * * @since 1.5.4 * * @var General */ private $template;
/** * Set a property. * * @since 1.5.4 * * @param string $key Property name. * @param string $value Property value. */ public function __set( $key, $value ) {
$this->$key = $value; }
/** * Get a property. * * @since 1.5.4 * * @param string $key Property name. * * @return string */ public function __get( $key ) {
return $this->$key; }
/** * Check if a property exists. * * @since 1.5.4 * * @param string $key Property name. * * @return bool */ public function __isset( $key ) {
return isset( $this->key ); }
/** * Unset a property. * * @since 1.5.4 * * @param string $key Property name. */ public function __unset( $key ) {
unset( $this->key ); }
/** * Email kill switch if needed. * * @since 1.5.4 * * @return bool */ public function is_email_disabled() {
return (bool) \apply_filters( 'wpforms_emails_mailer_is_email_disabled', false, $this ); }
/** * Sanitize the string. * * @uses \wpforms_decode_string() * * @since 1.5.4 * @since 1.6.0 Deprecated param: $linebreaks. This is handled by wpforms_decode_string(). * * @param string $string String that may contain tags. * * @return string */ public function sanitize( $string = '' ) {
return \wpforms_decode_string( $string ); }
/** * Get the email from name. * * @since 1.5.4 * * @return string */ public function get_from_name() {
$this->from_name = $this->from_name ? $this->sanitize( $this->from_name ) : \get_bloginfo( 'name' );
return \apply_filters( 'wpforms_emails_mailer_get_from_name', $this->from_name, $this ); }
/** * Get the email from address. * * @since 1.5.4 * * @return string */ public function get_from_address() {
$this->from_address = $this->from_address ? $this->sanitize( $this->from_address ) : \get_option( 'admin_email' );
return \apply_filters( 'wpforms_emails_mailer_get_from_address', $this->from_address, $this ); }
/** * Get the email reply to address. * * @since 1.5.4 * * @return string */ public function get_reply_to_address() {
if ( empty( $this->reply_to ) || ! \is_email( $this->reply_to ) ) { $this->reply_to = $this->from_address; }
$this->reply_to = $this->sanitize( $this->reply_to );
if ( empty( $this->reply_to ) || ! \is_email( $this->reply_to ) ) { $this->reply_to = \get_option( 'admin_email' ); }
return \apply_filters( 'wpforms_emails_mailer_get_reply_to_address', $this->reply_to, $this ); }
/** * Get the email carbon copy addresses. * * @since 1.5.4 * * @return string The email carbon copy addresses. */ public function get_cc_address() {
if ( empty( $this->cc ) ) { return \apply_filters( 'wpforms_emails_mailer_get_cc_address', $this->cc, $this ); }
$this->cc = $this->sanitize( $this->cc );
$addresses = \array_map( 'trim', \explode( ',', $this->cc ) );
foreach ( $addresses as $key => $address ) { if ( ! \is_email( $address ) ) { unset( $addresses[ $key ] ); } }
$this->cc = \implode( ',', $addresses );
return \apply_filters( 'wpforms_emails_mailer_get_cc_address', $this->cc, $this ); }
/** * Get the email content type. * * @since 1.5.4 * * @return string The email content type. */ public function get_content_type() {
$is_html = 'default' === \wpforms_setting( 'email-template', 'default' );
if ( ! $this->content_type && $is_html ) { $this->content_type = \apply_filters( 'wpforms_emails_mailer_get_content_type_default', 'text/html', $this ); } elseif ( ! $is_html ) { $this->content_type = 'text/plain'; }
return \apply_filters( 'wpforms_emails_mailer_get_content_type', $this->content_type, $this ); }
/** * Get the email message. * * @since 1.5.4 * * @return string The email message. */ public function get_message() {
if ( empty( $this->message ) && ! empty( $this->template ) ) { $this->message = $this->template->get(); }
return \apply_filters( 'wpforms_emails_mailer_get_message', $this->message, $this ); }
/** * Get the email headers. * * @since 1.5.4 * * @return string The email headers. */ public function get_headers() {
if ( $this->headers ) { return \apply_filters( 'wpforms_emails_mailer_get_headers', $this->headers, $this ); }
$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
if ( $this->get_reply_to_address() ) { $this->headers .= "Reply-To: {$this->get_reply_to_address()}\r\n"; }
if ( $this->get_cc_address() ) { $this->headers .= "Cc: {$this->get_cc_address()}\r\n"; }
$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
return \apply_filters( 'wpforms_emails_mailer_get_headers', $this->headers, $this ); }
/** * Get the email attachments. * * @since 1.5.4 * * @return string */ public function get_attachments() {
return \apply_filters( 'wpforms_emails_mailer_get_attachments', $this->attachments, $this ); }
/** * Set email address to send to. * * @since 1.5.4 * * @param string $email Email address. * * @return Mailer */ public function to_email( $email ) {
$this->to_email = \apply_filters( 'wpforms_emails_mailer_to_email', $email, $this );
return $this; }
/** * Set email subject. * * @since 1.5.4 * * @param string $subject Email subject. * * @return Mailer */ public function subject( $subject ) {
$subject = $this->sanitize( $subject );
$this->subject = \apply_filters( 'wpforms_emails_mailer_subject', $subject, $this );
return $this; }
/** * Set email message (body). * * @since 1.5.4 * * @param string $message Email message. * * @return Mailer */ public function message( $message ) {
$this->message = \apply_filters( 'wpforms_emails_mailer_message', $message, $this );
return $this; }
/** * Set email template. * * @since 1.5.4 * * @param General $template Email template. * * @return Mailer */ public function template( General $template ) {
$this->template = \apply_filters( 'wpforms_emails_mailer_template', $template, $this );
return $this; }
/** * Get email errors. * * @since 1.5.4 * * @return array */ protected function get_errors() {
$errors = array();
if ( ! \is_email( $this->to_email ) ) { $errors[] = \esc_html__( '[WPForms\Emails\Mailer] Invalid email address.', 'wpforms-lite' ); }
if ( empty( $this->subject ) ) { $errors[] = \esc_html__( '[WPForms\Emails\Mailer] Empty subject line.', 'wpforms-lite' ); }
if ( empty( $this->get_message() ) ) { $errors[] = \esc_html__( '[WPForms\Emails\Mailer] Empty message.', 'wpforms-lite' ); }
return $errors; }
/** * Log given email errors. * * @since 1.5.4 * * @param array $errors Errors to log. */ protected function log_errors( $errors ) {
if ( empty( $errors ) || ! \is_array( $errors ) ) { return; }
foreach ( $errors as $error ) { \wpforms_log( $error, array( 'to_email' => $this->to_email, 'subject' => $this->subject, 'message' => \wp_trim_words( $this->get_message() ), ), array( 'type' => 'error', ) ); } }
/** * Send the email. * * @since 1.5.4 * * @return bool */ public function send() {
if ( ! \did_action( 'init' ) && ! \did_action( 'admin_init' ) ) { \_doing_it_wrong( __FUNCTION__, \esc_html__( 'You cannot send emails with WPForms\Emails\Mailer until init/admin_init has been reached.', 'wpforms-lite' ), null );
return false; }
// Don't send anything if emails have been disabled. if ( $this->is_email_disabled() ) { return false; }
$errors = $this->get_errors();
if ( $errors ) { $this->log_errors( $errors ); return false; }
$this->send_before();
$sent = \wp_mail( $this->to_email, $this->subject, $this->get_message(), $this->get_headers(), $this->get_attachments() );
$this->send_after();
return $sent; }
/** * Add filters / actions before the email is sent. * * @since 1.5.4 */ public function send_before() {
\do_action( 'wpforms_emails_mailer_send_before', $this ); \add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); \add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); \add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); }
/** * Remove filters / actions after the email is sent. * * @since 1.5.4 */ public function send_after() {
\do_action( 'wpforms_emails_mailer_send_after', $this ); \remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); \remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); \remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); } }
|