-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.php
629 lines (496 loc) · 14.5 KB
/
mail.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<?php
add_filter( 'wpcf7_mail_html_body', 'wpcf7_mail_html_body_autop', 10, 1 );
/**
* Filter callback that applies auto-p to HTML email message body.
*/
function wpcf7_mail_html_body_autop( $body ) {
if ( wpcf7_autop_or_not( array( 'for' => 'mail' ) ) ) {
$body = wpcf7_autop( $body );
}
return $body;
}
/**
* Class that represents an attempt to compose and send email.
*/
class WPCF7_Mail {
private static $current = null;
private $name = '';
private $locale = '';
private $template = array();
private $component = '';
private $use_html = false;
private $exclude_blank = false;
/**
* Returns the singleton instance of this class.
*/
public static function get_current() {
return self::$current;
}
/**
* Returns the name of the email template currently processed.
*
* Expected output: 'mail' or 'mail_2'
*/
public static function get_current_template_name() {
$current = self::get_current();
if ( $current instanceof self ) {
return $current->get_template_name();
}
}
/**
* Returns the name of the email template component currently processed.
*
* Expected output: 'recipient', 'sender', 'subject',
* 'additional_headers', 'body', or 'attachments'
*/
public static function get_current_component_name() {
$current = self::get_current();
if ( $current instanceof self ) {
return $current->get_component_name();
}
}
/**
* Composes and sends email based on the specified template.
*
* @param array $template Array of email template.
* @param string $name Optional name of the template, such as
* 'mail' or 'mail_2'. Default empty string.
* @return bool Whether the email was sent successfully.
*/
public static function send( $template, $name = '' ) {
self::$current = new self( $name, $template );
return self::$current->compose();
}
/**
* The constructor method.
*
* @param string $name The name of the email template.
* Such as 'mail' or 'mail_2'.
* @param array $template Array of email template.
*/
private function __construct( $name, $template ) {
$this->name = trim( $name );
$this->use_html = ! empty( $template['use_html'] );
$this->exclude_blank = ! empty( $template['exclude_blank'] );
$this->template = wp_parse_args( $template, array(
'subject' => '',
'sender' => '',
'body' => '',
'recipient' => '',
'additional_headers' => '',
'attachments' => '',
) );
if ( $submission = WPCF7_Submission::get_instance() ) {
$contact_form = $submission->get_contact_form();
$this->locale = $contact_form->locale();
}
}
/**
* Returns the name of the email template.
*/
public function name() {
return $this->name;
}
/**
* Returns the name of the email template. A wrapper method of name().
*/
public function get_template_name() {
return $this->name();
}
/**
* Returns the name of the email template component currently processed.
*/
public function get_component_name() {
return $this->component;
}
/**
* Retrieves a component from the email template.
*
* @param string $component The name of the component.
* @param bool $replace_tags Whether to replace mail-tags
* within the component.
* @return string The text representation of the email component.
*/
public function get( $component, $replace_tags = false ) {
$this->component = $component;
$use_html = ( $this->use_html && 'body' === $component );
$exclude_blank = ( $this->exclude_blank && 'body' === $component );
$template = $this->template;
$component = isset( $template[$component] ) ? $template[$component] : '';
if ( $replace_tags ) {
$component = $this->replace_tags( $component, array(
'html' => $use_html,
'exclude_blank' => $exclude_blank,
) );
if ( $use_html ) {
// Convert <[email protected]> to <[email protected]>.
$component = preg_replace_callback(
'/<(.*?)>/',
static function ( $matches ) {
if ( is_email( $matches[1] ) ) {
return sprintf( '<%s>', $matches[1] );
} else {
return $matches[0];
}
},
$component
);
if ( ! preg_match( '%<html[>\s].*</html>%is', $component ) ) {
$component = $this->htmlize( $component );
}
}
}
$this->component = '';
return $component;
}
/**
* Creates HTML message body by adding the header and footer.
*
* @param string $body The body part of HTML.
* @return string Formatted HTML.
*/
private function htmlize( $body ) {
if ( $this->locale ) {
$lang_atts = sprintf( ' %s',
wpcf7_format_atts( array(
'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr',
'lang' => str_replace( '_', '-', $this->locale ),
) )
);
} else {
$lang_atts = '';
}
$header = apply_filters( 'wpcf7_mail_html_header',
'<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"' . $lang_atts . '>
<head>
<title>' . esc_html( $this->get( 'subject', true ) ) . '</title>
</head>
<body>
',
$this
);
$body = apply_filters( 'wpcf7_mail_html_body', $body, $this );
$footer = apply_filters( 'wpcf7_mail_html_footer',
'</body>
</html>',
$this
);
return $header . $body . $footer;
}
/**
* Composes an email message and attempts to send it.
*
* @param bool $send Whether to attempt to send email. Default true.
*/
private function compose( $send = true ) {
$components = array(
'subject' => $this->get( 'subject', true ),
'sender' => $this->get( 'sender', true ),
'body' => $this->get( 'body', true ),
'recipient' => $this->get( 'recipient', true ),
'additional_headers' => $this->get( 'additional_headers', true ),
'attachments' => $this->attachments(),
);
$components = apply_filters( 'wpcf7_mail_components',
$components, wpcf7_get_current_contact_form(), $this
);
if ( ! $send ) {
return $components;
}
$subject = wpcf7_strip_newline( $components['subject'] );
$sender = wpcf7_strip_newline( $components['sender'] );
$recipient = wpcf7_strip_newline( $components['recipient'] );
$body = $components['body'];
$additional_headers = trim( $components['additional_headers'] );
$headers = "From: $sender\n";
if ( $this->use_html ) {
$headers .= "Content-Type: text/html\n";
$headers .= "X-WPCF7-Content-Type: text/html\n";
} else {
$headers .= "X-WPCF7-Content-Type: text/plain\n";
}
if ( $additional_headers ) {
$headers .= $additional_headers . "\n";
}
$attachments = array_filter(
(array) $components['attachments'],
function ( $attachment ) {
$path = path_join( WP_CONTENT_DIR, $attachment );
if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) {
wp_trigger_error(
'',
sprintf(
/* translators: %s: Attachment file path. */
__( 'Failed to attach a file. %s is not in the allowed directory.', 'contact-form-7' ),
$path
),
E_USER_NOTICE
);
return false;
}
if ( ! is_readable( $path ) or ! is_file( $path ) ) {
wp_trigger_error(
'',
sprintf(
/* translators: %s: Attachment file path. */
__( 'Failed to attach a file. %s is not a readable file.', 'contact-form-7' ),
$path
),
E_USER_NOTICE
);
return false;
}
static $total_size = array();
if ( ! isset( $total_size[$this->name] ) ) {
$total_size[$this->name] = 0;
}
$file_size = (int) @filesize( $path );
if ( 25 * MB_IN_BYTES < $total_size[$this->name] + $file_size ) {
wp_trigger_error(
'',
__( 'Failed to attach a file. The total file size exceeds the limit of 25 megabytes.', 'contact-form-7' ),
E_USER_NOTICE
);
return false;
}
$total_size[$this->name] += $file_size;
return true;
}
);
return wp_mail( $recipient, $subject, $body, $headers, $attachments );
}
/**
* Replaces mail-tags within the given text.
*/
public function replace_tags( $content, $options = '' ) {
if ( true === $options ) {
$options = array( 'html' => true );
}
$options = wp_parse_args( $options, array(
'html' => false,
'exclude_blank' => false,
) );
return wpcf7_mail_replace_tags( $content, $options );
}
/**
* Creates an array of attachments based on uploaded files and local files.
*/
private function attachments( $template = null ) {
if ( ! $template ) {
$template = $this->get( 'attachments' );
}
$attachments = array();
if ( $submission = WPCF7_Submission::get_instance() ) {
$uploaded_files = $submission->uploaded_files();
foreach ( (array) $uploaded_files as $name => $paths ) {
if ( false !== strpos( $template, "[{$name}]" ) ) {
$attachments = array_merge( $attachments, (array) $paths );
}
}
}
foreach ( explode( "\n", $template ) as $line ) {
$line = trim( $line );
if ( '' === $line or '[' == substr( $line, 0, 1 ) ) {
continue;
}
$attachments[] = path_join( WP_CONTENT_DIR, $line );
}
if ( $submission = WPCF7_Submission::get_instance() ) {
$attachments = array_merge(
$attachments,
(array) $submission->extra_attachments( $this->name )
);
}
return $attachments;
}
}
/**
* Replaces all mail-tags within the given text content.
*
* @param string $content Text including mail-tags.
* @param string|array $options Optional. Output options.
* @return string Result of replacement.
*/
function wpcf7_mail_replace_tags( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'exclude_blank' => false,
) );
if ( is_array( $content ) ) {
foreach ( $content as $key => $value ) {
$content[$key] = wpcf7_mail_replace_tags( $value, $options );
}
return $content;
}
$content = explode( "\n", $content );
foreach ( $content as $num => $line ) {
$line = new WPCF7_MailTaggedText( $line, $options );
$replaced = $line->replace_tags();
if ( $options['exclude_blank'] ) {
$replaced_tags = $line->get_replaced_tags();
if ( empty( $replaced_tags )
or array_filter( $replaced_tags, 'strlen' ) ) {
$content[$num] = $replaced;
} else {
unset( $content[$num] ); // Remove a line.
}
} else {
$content[$num] = $replaced;
}
}
$content = implode( "\n", $content );
return $content;
}
add_action( 'phpmailer_init', 'wpcf7_phpmailer_init', 10, 1 );
/**
* Adds custom properties to the PHPMailer object.
*/
function wpcf7_phpmailer_init( $phpmailer ) {
$custom_headers = $phpmailer->getCustomHeaders();
$phpmailer->clearCustomHeaders();
$wpcf7_content_type = false;
foreach ( (array) $custom_headers as $custom_header ) {
$name = $custom_header[0];
$value = $custom_header[1];
if ( 'X-WPCF7-Content-Type' === $name ) {
$wpcf7_content_type = trim( $value );
} else {
$phpmailer->addCustomHeader( $name, $value );
}
}
if ( 'text/html' === $wpcf7_content_type ) {
$phpmailer->msgHTML( $phpmailer->Body );
} elseif ( 'text/plain' === $wpcf7_content_type ) {
$phpmailer->AltBody = '';
}
}
/**
* Class that represents a single-line text including mail-tags.
*/
class WPCF7_MailTaggedText {
private $html = false;
private $callback = null;
private $content = '';
private $replaced_tags = array();
/**
* The constructor method.
*/
public function __construct( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'callback' => null,
) );
$this->html = (bool) $options['html'];
if ( null !== $options['callback']
and is_callable( $options['callback'] ) ) {
$this->callback = $options['callback'];
} elseif ( $this->html ) {
$this->callback = array( $this, 'replace_tags_callback_html' );
} else {
$this->callback = array( $this, 'replace_tags_callback' );
}
$this->content = $content;
}
/**
* Retrieves mail-tags that have been replaced by this instance.
*
* @return array List of mail-tags replaced.
*/
public function get_replaced_tags() {
return $this->replaced_tags;
}
/**
* Replaces mail-tags based on regexp.
*/
public function replace_tags() {
$regex = '/(\[?)\[[\t ]*'
. '([a-zA-Z_][0-9a-zA-Z:._-]*)' // [2] = name
. '((?:[\t ]+"[^"]*"|[\t ]+\'[^\']*\')*)' // [3] = values
. '[\t ]*\](\]?)/';
return preg_replace_callback( $regex, $this->callback, $this->content );
}
/**
* Callback function for replacement. For HTML message body.
*/
private function replace_tags_callback_html( $matches ) {
return $this->replace_tags_callback( $matches, true );
}
/**
* Callback function for replacement.
*/
private function replace_tags_callback( $matches, $html = false ) {
// allow [[foo]] syntax for escaping a tag
if ( $matches[1] == '['
and $matches[4] == ']' ) {
return substr( $matches[0], 1, -1 );
}
$tag = $matches[0];
$tagname = $matches[2];
$values = $matches[3];
$mail_tag = new WPCF7_MailTag( $tag, $tagname, $values );
$field_name = $mail_tag->field_name();
$submission = WPCF7_Submission::get_instance();
$submitted = $submission
? $submission->get_posted_data( $field_name )
: null;
if ( $mail_tag->get_option( 'do_not_heat' ) ) {
$submitted = wp_unslash( $_POST[$field_name] ?? '' );
}
$replaced = $submitted;
if ( null !== $replaced ) {
if ( $format = $mail_tag->get_option( 'format' ) ) {
$replaced = $this->format( $replaced, $format );
}
$separator = ( 'body' === WPCF7_Mail::get_current_component_name() )
? wp_get_list_item_separator()
: ', ';
$replaced = wpcf7_flat_join( $replaced, array(
'separator' => $separator,
) );
if ( $html ) {
$replaced = esc_html( $replaced );
$replaced = wptexturize( $replaced );
}
}
if ( $form_tag = $mail_tag->corresponding_form_tag() ) {
$type = $form_tag->type;
$replaced = apply_filters(
"wpcf7_mail_tag_replaced_{$type}", $replaced,
$submitted, $html, $mail_tag
);
}
$replaced = apply_filters(
'wpcf7_mail_tag_replaced', $replaced,
$submitted, $html, $mail_tag
);
if ( null !== $replaced ) {
$replaced = trim( $replaced );
$this->replaced_tags[$tag] = $replaced;
return $replaced;
}
$special = apply_filters( 'wpcf7_special_mail_tags', null,
$mail_tag->tag_name(), $html, $mail_tag
);
if ( null !== $special ) {
$this->replaced_tags[$tag] = $special;
return $special;
}
return $tag;
}
/**
* Formats string based on the formatting option in the form-tag.
*/
public function format( $original, $format ) {
$original = (array) $original;
foreach ( $original as $key => $value ) {
if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value ) ) {
$datetime = date_create( $value, wp_timezone() );
if ( false !== $datetime ) {
$original[$key] = wp_date( $format, $datetime->getTimestamp() );
}
}
}
return $original;
}
}