|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Mandrill; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copied from sparkpost |
|
7
|
|
|
*/ |
|
8
|
|
|
class EmailUtils |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Convert an html email to a text email while keeping formatting and links |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $content |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function convert_html_to_text($content) |
|
17
|
|
|
{ |
|
18
|
|
|
// Prevent styles to be included |
|
19
|
|
|
$content = preg_replace('/<style.*>([\s\S]*)<\/style>/i', '', $content); |
|
20
|
|
|
// Convert html entities to strip them later on |
|
21
|
|
|
$content = html_entity_decode($content); |
|
22
|
|
|
// Bold |
|
23
|
|
|
$content = str_ireplace(['<strong>', '</strong>', '<b>', '</b>'], "*", $content); |
|
24
|
|
|
// Newlines |
|
25
|
|
|
$content = str_ireplace(['<br>', '<br/>'], "\n", $content); |
|
26
|
|
|
// Replace links to keep them accessible |
|
27
|
|
|
$content = preg_replace('/<a[\s\S]href="(.*?)"[\s\S]*?>(.*?)<\/a>/i', '$2 ($1)', $content); |
|
28
|
|
|
// Remove html tags |
|
29
|
|
|
$content = strip_tags($content); |
|
30
|
|
|
// Avoid lots of spaces |
|
31
|
|
|
$content = preg_replace('/^[\s][\s]+(\S)/m', "\n$1", $content); |
|
32
|
|
|
// Trim content so that it's nice |
|
33
|
|
|
$content = trim($content); |
|
34
|
|
|
return $content; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Match all words and whitespace, will be terminated by '<' |
|
39
|
|
|
* |
|
40
|
|
|
* Note: use /u to support utf8 strings |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $rfc_email_string |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function get_displayname_from_rfc_email($rfc_email_string) |
|
46
|
|
|
{ |
|
47
|
|
|
$name = preg_match('/[\w\s\-\.]+/u', $rfc_email_string, $matches); |
|
48
|
|
|
if (!$name || empty($matches)) { |
|
49
|
|
|
return $rfc_email_string; |
|
50
|
|
|
} |
|
51
|
|
|
$matches[0] = trim($matches[0]); |
|
52
|
|
|
return $matches[0]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Extract parts between brackets |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $rfc_email_string |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function get_email_from_rfc_email($rfc_email_string) |
|
62
|
|
|
{ |
|
63
|
|
|
if (strpos($rfc_email_string, '<') === false) { |
|
64
|
|
|
return $rfc_email_string; |
|
65
|
|
|
} |
|
66
|
|
|
$mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches); |
|
67
|
|
|
if (!$mailAddress || empty($matches)) { |
|
68
|
|
|
return $rfc_email_string; |
|
69
|
|
|
} |
|
70
|
|
|
return $matches[1]; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|