-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-plain-text-email.php
More file actions
executable file
·112 lines (93 loc) · 3.31 KB
/
Copy pathadd-plain-text-email.php
File metadata and controls
executable file
·112 lines (93 loc) · 3.31 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
<?php
/*
Plugin Name: Add Plain Text Email
Plugin URI: https://www.dannyvankooten.com/
Description: Adds a text/plain email to text/html emails to decrease the chance of emails being tagged as spam.
Version: 1.2.1
Author: Danny van Kooten
Author URI: https://www.dannyvanKooten.com
Requires PHP: 7.4
License: GPL v3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Add Plain Text Email
Copyright (C) 2013 Danny van Kooten
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
defined( 'ABSPATH' ) OR exit;
class Add_Plain_Text_Email {
/**
* Add hooks
*/
public function add_hooks() {
add_action('phpmailer_init', array( $this, 'set_plaintext_body' ) );
}
/**
* @param PHPMailer $phpmailer
*
* Note: somehow we can not type-hint the parameter here as PHPMailer...
*/
public function set_plaintext_body( $phpmailer ) {
// don't run if sending plain text email already
if( $phpmailer->ContentType === 'text/plain' ) {
return;
}
// don't run if AltBody is set (by other plugin)
if( ! empty( $phpmailer->AltBody ) ) {
return;
}
// set AltBody
$phpmailer->AltBody = $this->strip_html_tags( $phpmailer->Body );
}
/**
* Remove HTML tags, including invisible text such as style and
* script code, and embedded objects. Add line breaks around
* block-level tags to prevent word joining after tag removal.
*/
public function strip_html_tags( $text ) {
$text = preg_replace(
array(
// Remove invisible content
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
'@\t+@su',
'@\n+@su'
),
'',
$text );
// replace certain elements with a line-break
$text = preg_replace(
array(
'@</?((div)|(h[1-9])|(/tr)|(p)|(pre))@iu'
),
"\n\$0",
$text );
// replace other elements with a space
$text = preg_replace(
array(
'@</((td)|(th))@iu'
),
" \$0",
$text );
// preserve the href attribute from <a> elements
$text = preg_replace( '@<a[^>]+href=["\']([^"\']+)[^>]+>(.+)</a>@iu', "\$2 (\$1)", $text );
// strip all remaining HTML tags
$text = strip_tags( $text );
return trim( $text );
}
}
$apte = new Add_Plain_Text_Email;
$apte->add_hooks();