1 | <?php |
||
2 | |||
3 | namespace LeKoala\EmailTemplates\Helpers; |
||
4 | |||
5 | use Symfony\Component\Mime\Address; |
||
6 | use Symfony\Component\Mime\Part\TextPart; |
||
7 | |||
8 | /** |
||
9 | * Useful tools for emails |
||
10 | */ |
||
11 | class EmailUtils |
||
12 | { |
||
13 | /** |
||
14 | * Convert an html email to a text email while keeping formatting and links |
||
15 | * |
||
16 | * @param string|TextPart $content |
||
17 | * @return string |
||
18 | */ |
||
19 | public static function convert_html_to_text($content) |
||
20 | { |
||
21 | if ($content instanceof TextPart) { |
||
22 | $content = $content->getBody(); |
||
23 | } |
||
24 | // Prevent double title |
||
25 | $content = preg_replace('/<title>([\s\S]*)<\/title>/i', '', $content); |
||
26 | // Prevent styles to be included |
||
27 | $content = preg_replace('/<style.*>([\s\S]*)<\/style>/i', '', $content); |
||
28 | // Convert html entities to strip them later on |
||
29 | $content = html_entity_decode($content); |
||
30 | // Bold |
||
31 | $content = str_ireplace(['<strong>', '</strong>', '<b>', '</b>'], "*", $content); |
||
32 | // Replace links to keep them accessible |
||
33 | $content = preg_replace('/<a[\s\S]href="(.*?)"[\s\S]*?>(.*?)<\/a>/i', '$2 ($1)', $content); |
||
34 | // Replace new lines |
||
35 | $content = str_replace(['<br>', '<br/>', '<br />'], "\r\n", $content); |
||
36 | // Remove html tags |
||
37 | $content = strip_tags($content); |
||
38 | // Avoid lots of spaces |
||
39 | $content = preg_replace('/^[\s][\s]+(\S)/m', "\n$1", $content); |
||
40 | // Trim content so that it's nice |
||
41 | $content = trim($content); |
||
42 | return $content; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Format an array of addresses into a string |
||
47 | * |
||
48 | * @param array $emails |
||
49 | * @return string |
||
50 | */ |
||
51 | public static function format_email_addresses($emails) |
||
52 | { |
||
53 | if (empty($emails)) { |
||
54 | return ''; |
||
55 | } |
||
56 | $arr = []; |
||
57 | foreach ($emails as $address => $title) { |
||
58 | if ($title instanceof Address) { |
||
59 | $address = $title->getAddress(); |
||
60 | $title = $title->getName(); |
||
61 | } |
||
62 | $line = "$address"; |
||
63 | if ($title) { |
||
64 | $line .= " <$title>"; |
||
65 | } |
||
66 | $arr[] = $line; |
||
67 | } |
||
68 | return implode(', ', $arr); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Match all words and whitespace, will be terminated by '<' |
||
73 | * |
||
74 | * Note: use /u to support utf8 strings |
||
75 | * |
||
76 | * @param string $rfc_email_string |
||
77 | * @return string |
||
78 | */ |
||
79 | public static function get_displayname_from_rfc_email($rfc_email_string) |
||
80 | { |
||
81 | $name = preg_match('/[\w\s\-\.]+/u', $rfc_email_string, $matches); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
82 | $matches[0] = trim($matches[0]); |
||
83 | return $matches[0]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Extract parts between brackets |
||
88 | * |
||
89 | * @param string $rfc_email_string |
||
90 | * @return string |
||
91 | */ |
||
92 | public static function get_email_from_rfc_email($rfc_email_string) |
||
93 | { |
||
94 | if (strpos($rfc_email_string, '<') === false) { |
||
95 | return $rfc_email_string; |
||
96 | } |
||
97 | $mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches); |
||
0 ignored issues
–
show
|
|||
98 | if (empty($matches)) { |
||
99 | return $rfc_email_string; |
||
100 | } |
||
101 | return $matches[1]; |
||
102 | } |
||
103 | } |
||
104 |