Source of file EmailUtils.php
Size: 2,202 Bytes - Last Modified: 2021-12-23T10:01:22+00:00
/var/www/docs.ssmods.com/process/src/code/EmailUtils.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 | <?php namespace LeKoala\Mandrill; /** * Copied from sparkpost */ class EmailUtils { /** * Convert an html email to a text email while keeping formatting and links * * @param string $content * @return string */ public static function convert_html_to_text($content) { // Prevent styles to be included $content = preg_replace('/<style.*>([\s\S]*)<\/style>/i', '', $content); // Convert html entities to strip them later on $content = html_entity_decode($content); // Bold $content = str_ireplace(['<strong>', '</strong>', '<b>', '</b>'], "*", $content); // Newlines $content = str_ireplace(['<br>', '<br/>'], "\n", $content); // Replace links to keep them accessible $content = preg_replace('/<a[\s\S]href="(.*?)"[\s\S]*?>(.*?)<\/a>/i', '$2 ($1)', $content); // Remove html tags $content = strip_tags($content); // Avoid lots of spaces $content = preg_replace('/^[\s][\s]+(\S)/m', "\n$1", $content); // Trim content so that it's nice $content = trim($content); return $content; } /** * Match all words and whitespace, will be terminated by '<' * * Note: use /u to support utf8 strings * * @param string $rfc_email_string * @return string */ public static function get_displayname_from_rfc_email($rfc_email_string) { $name = preg_match('/[\w\s\-\.]+/u', $rfc_email_string, $matches); if (!$name || empty($matches)) { return $rfc_email_string; } $matches[0] = trim($matches[0]); return $matches[0]; } /** * Extract parts between brackets * * @param string $rfc_email_string * @return string */ public static function get_email_from_rfc_email($rfc_email_string) { if (strpos($rfc_email_string, '<') === false) { return $rfc_email_string; } $mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches); if (!$mailAddress || empty($matches)) { return $rfc_email_string; } return $matches[1]; } } |