1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\SparkPost; |
4
|
|
|
|
5
|
|
|
use Pelago\Emogrifier\CssInliner; |
6
|
|
|
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner; |
7
|
|
|
use Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter; |
8
|
|
|
|
9
|
|
|
class EmailUtils |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Inline styles using Pelago Emogrifier V6 |
13
|
|
|
* |
14
|
|
|
* This is much better than the functionnality provided by SparkPost anyway |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/MyIntervals/emogrifier#more-complex-example |
17
|
|
|
* @param string $html |
18
|
|
|
* @param string $css (optional) css to inline |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public static function inline_styles($html, $css = '') |
22
|
|
|
{ |
23
|
|
|
$domDocument = CssInliner::fromHtml($html)->inlineCss($css)->getDomDocument(); |
24
|
|
|
|
25
|
|
|
HtmlPruner::fromDomDocument($domDocument)->removeElementsWithDisplayNone(); |
26
|
|
|
$html = CssToAttributeConverter::fromDomDocument($domDocument) |
27
|
|
|
->convertCssToVisualAttributes()->render(); |
28
|
|
|
|
29
|
|
|
return $html; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert an html email to a text email while keeping formatting and links |
34
|
|
|
* |
35
|
|
|
* @param string $content |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public static function convert_html_to_text($content) |
39
|
|
|
{ |
40
|
|
|
// Prevent styles to be included |
41
|
|
|
$content = preg_replace('/<style.*>([\s\S]*)<\/style>/i', '', $content); |
42
|
|
|
// Convert html entities to strip them later on |
43
|
|
|
$content = html_entity_decode($content); |
44
|
|
|
// Bold |
45
|
|
|
$content = str_ireplace(['<strong>', '</strong>', '<b>', '</b>'], "*", $content); |
46
|
|
|
// Replace links to keep them accessible |
47
|
|
|
$content = preg_replace('/<a[\s\S]href="(.*?)"[\s\S]*?>(.*?)<\/a>/i', '$2 ($1)', $content); |
48
|
|
|
// Replace new lines |
49
|
|
|
$content = str_replace(['<br>', '<br/>', '<br />'], "\r\n", $content); |
50
|
|
|
// Remove html tags |
51
|
|
|
$content = strip_tags($content); |
52
|
|
|
// Avoid lots of spaces |
53
|
|
|
$content = preg_replace('/^[\s][\s]+(\S)/m', "\n$1", $content); |
54
|
|
|
// Trim content so that it's nice |
55
|
|
|
$content = trim($content); |
56
|
|
|
return $content; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Match all words and whitespace, will be terminated by '<' |
61
|
|
|
* |
62
|
|
|
* Note: use /u to support utf8 strings |
63
|
|
|
* |
64
|
|
|
* @param string $rfc_email_string |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function get_displayname_from_rfc_email($rfc_email_string) |
68
|
|
|
{ |
69
|
|
|
$name = preg_match('/[\w\s\-\.]+/u', $rfc_email_string, $matches); |
|
|
|
|
70
|
|
|
$matches[0] = trim($matches[0]); |
71
|
|
|
return $matches[0]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Extract parts between brackets |
76
|
|
|
* |
77
|
|
|
* @param string $rfc_email_string |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function get_email_from_rfc_email($rfc_email_string) |
81
|
|
|
{ |
82
|
|
|
if (strpos($rfc_email_string, '<') === false) { |
83
|
|
|
return $rfc_email_string; |
84
|
|
|
} |
85
|
|
|
$mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches); |
|
|
|
|
86
|
|
|
if (empty($matches)) { |
87
|
|
|
return $rfc_email_string; |
88
|
|
|
} |
89
|
|
|
return $matches[1]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Useful when dealing with legacy code |
94
|
|
|
* |
95
|
|
|
* @param \SilverStripe\Control\Email\Email $Email |
96
|
|
|
* @return \Symfony\Component\Mime\Header\Headers|Swift_Mime_SimpleHeaderSet |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
public static function getHeaders($Email) |
99
|
|
|
{ |
100
|
|
|
return method_exists($Email, 'getSwiftMessage') ? $Email->getSwiftMessage()->getHeaders() : $Email->getHeaders(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|