| 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 V7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * @param array<string|int,string|null>|string|null|bool $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public static function stringify($email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (!$email || is_bool($email)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (is_array($email)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return $email[1] . ' <' . $email[0] . '>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param array<string|int,string|null>|string|null|bool $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public static function validate($email) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return boolval(filter_var(self::stringify($email), FILTER_VALIDATE_EMAIL)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Convert an html email to a text email while keeping formatting and links | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param string $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public static function convert_html_to_text($content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         // Prevent styles to be included | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $content = preg_replace('/<style.*>([\s\S]*)<\/style>/i', '', $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // Convert html entities to strip them later on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $content = html_entity_decode($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // Bold | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $content = str_ireplace(['<strong>', '</strong>', '<b>', '</b>'], "*", $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // Replace links to keep them accessible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $content = preg_replace('/<a[\s\S]href="(.*?)"[\s\S]*?>(.*?)<\/a>/i', '$2 ($1)', $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         // Replace new lines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $content = str_replace(['<br>', '<br/>', '<br />'], "\r\n", $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Remove html tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $content = strip_tags($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // Avoid lots of spaces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $content = preg_replace('/^[\s][\s]+(\S)/m', "\n$1", $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // Trim content so that it's nice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $content = trim($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Match all words and whitespace, will be terminated by '<' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Note: use /u to support utf8 strings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $rfc_email_string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public static function get_displayname_from_rfc_email($rfc_email_string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $name = preg_match('/[\w\s\-\.]+/u', $rfc_email_string, $matches); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $matches[0] = trim($matches[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $matches[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Extract parts between brackets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $rfc_email_string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public static function get_email_from_rfc_email($rfc_email_string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (strpos($rfc_email_string, '<') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             return $rfc_email_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if (empty($matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             return $rfc_email_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $matches[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param \SilverStripe\Control\Email\Email $Email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return \Symfony\Component\Mime\Header\Headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public static function getHeaders($Email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return method_exists($Email, 'getSwiftMessage') ? $Email->getSwiftMessage()->getHeaders() : $Email->getHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |