Newer
Older
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
<?php
/**
* Copyright notice
*
* (c) in2code <info@in2code.de>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
*/
declare(strict_types=1);
namespace SGalinski\SgMail\Service;
/**
* Class PlaintextService
*/
class PlaintextService
{
/**
* Function makePlain() removes html tags and add linebreaks
* Easy generate a plain email bodytext from a html bodytext
*
* @param string $content HTML Mail bodytext
* @return string $content
*/
public function makePlain($content)
{
$content = $this->removeInvisibleElements($content);
$content = $this->removeLinebreaksAndTabs($content);
$content = $this->addLineBreaks($content);
$content = $this->addSpaceToTableCells($content);
$content = $this->extractLinkForPlainTextContent($content);
$content = $this->removeTags($content);
$array = [
'<br >',
'<br>',
'<br/>',
'<br />'
];
return trim(str_replace($array, PHP_EOL, $content));
}
/**
* Remove all invisible elements
*
* @param string $content
* @return string
*/
protected function removeInvisibleElements($content)
{
$content = preg_replace(
[
'/<head[^>]*?>.*?<\/head>/siu',
'/<style[^>]*?>.*?<\/style>/siu',
'/<script[^>]*?>.*?<\/script>/siu',
'/<object[^>]*?>.*?<\/object>/siu',
'/<embed[^>]*?>.*?<\/embed>/siu',
'/<applet[^>]*?>.*?<\/applet>/siu',
'/<noframes[^>]*?>.*?<\/noframes>/siu',
'/<noscript[^>]*?>.*?<\/noscript>/siu',
'/<noembed[^>]*?>.*?<\/noembed>/siu',
],
[
'', '', '', '', '', '', '', '', ''
],
$content
);
return $content;
}
/**
* Remove linebreaks and tabs
*
* @param string $content
* @return string
*/
protected function removeLinebreaksAndTabs($content)
{
$content = trim(str_replace(["\n", "\r", "\t"], '', $content));
return $content;
}
/**
Stefan Galinski
committed
* add linebreaks on some parts (</p> => </p><br>)
*
* @param string $content
* @return array
*/
protected function addLineBreaks($content)
{
$tags2LineBreaks = [
'</p>',
'</tr>',
'<ul>',
'</li>',
'</h1>',
'</h2>',
'</h3>',
'</h4>',
'</h5>',
'</h6>',
'</div>',
'</legend>',
'</fieldset>',
'</dd>',
'</dt>'
];
Stefan Galinski
committed
return str_replace($tags2LineBreaks, '</p><br>', $content);
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
}
/**
* Add a space character to a table cell
*
* @param string $content
* @return string
*/
protected function addSpaceToTableCells($content)
{
return str_replace(['</td>', '</th>'], '</td> ', $content);
}
/**
* Remove all tags but keep br and address
*
* @param string $content
* @return string
*/
protected function removeTags($content)
{
return strip_tags($content, '<br><address>');
}
/**
* Extract uri from href attributes and decode it
*
* replace links
* <a href="xyz">LINK</a>
* ->
* LINK [xyz]
*
* @param string $content
* @return string
*/
protected function extractLinkForPlainTextContent($content)
{
$pattern = '/<a[^>]+href\s*=\s*["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/misu';
return preg_replace_callback($pattern, function ($matches) {
return $matches[2] . ' [' . htmlspecialchars_decode($matches[1]) . ']';
}, $content);
}
}