diff --git a/src/EmailUtils.php b/src/EmailUtils.php index bbb4706..fcef0e7 100644 --- a/src/EmailUtils.php +++ b/src/EmailUtils.php @@ -29,6 +29,30 @@ public static function inline_styles($html, $css = '') return $html; } + /** + * @param array|string|null|bool $email + * @return string|null + */ + public static function stringify($email) + { + if (!$email || is_bool($email)) { + return null; + } + if (is_array($email)) { + return $email[1] . ' <' . $email[0] . '>'; + } + return $email; + } + + /** + * @param array|string|null|bool $email + * @return bool + */ + public static function validate($email) + { + return boolval(filter_var(self::stringify($email), FILTER_VALIDATE_EMAIL)); + } + /** * Convert an html email to a text email while keeping formatting and links * diff --git a/src/SparkPostAdmin.php b/src/SparkPostAdmin.php index 40156b2..b3a659a 100644 --- a/src/SparkPostAdmin.php +++ b/src/SparkPostAdmin.php @@ -2,7 +2,7 @@ namespace LeKoala\SparkPost; -use \Exception; +use Exception; use SilverStripe\Forms\Tab; use SilverStripe\Forms\Form; use SilverStripe\Forms\TabSet; @@ -150,7 +150,7 @@ public function settings($request) /** * @param HTTPRequest $request - * @return HTTPResponse|null + * @return HTTPResponse|string */ public function send_test($request) { @@ -168,8 +168,7 @@ public function send_test($request) $email->setTo($to); $email->send(); - var_dump('Email sent!'); - return null; + return 'Email sent'; } /** @@ -192,8 +191,6 @@ public function getEditForm($id = null, $fields = null) $id = $this->currentPageID(); } - $form = parent::getEditForm($id); - /** @var DataObject|null $record */ $record = $this->getRecord($id); @@ -283,9 +280,10 @@ public function getEditForm($id = null, $fields = null) $toolsHtml = '

Tools

'; // Show default from email - $defaultEmail = SparkPostHelper::resolveDefaultFromEmail(); - $toolsHtml .= "

Default sending email: " . $defaultEmail . " (" . SparkPostHelper::resolveDefaultFromEmailType() . ")

"; - if (!SparkPostHelper::isEmailDomainReady($defaultEmail)) { + $defaultEmail = SparkPostHelper::resolveDefaultFromEmail(); + $defaultEmailDisplayed = EmailUtils::stringify($defaultEmail); + $toolsHtml .= "

Default sending email: " . $defaultEmailDisplayed . " (" . SparkPostHelper::resolveDefaultFromEmailType() . ")

"; + if (!SparkPostHelper::isEmailDomainReady($defaultEmailDisplayed)) { $toolsHtml .= '

The default email is not ready to send emails

'; } @@ -327,9 +325,6 @@ public function getEditForm($id = null, $fields = null) $messagesTab->addExtraClass('ui-state-active'); } - $actions = new FieldList(); - - // Build replacement form $form = Form::create( $this, @@ -396,8 +391,9 @@ public function getCacheEnabled() protected function getCachedData($method, $params, $expireInSeconds = 60) { $enabled = $this->getCacheEnabled(); + $cacheResult = false; + $cache = $this->getCache(); if ($enabled) { - $cache = $this->getCache(); $key = md5(serialize($params)); $cacheResult = $cache->get($key); } @@ -572,7 +568,7 @@ public function Messages() $params = $this->getParams(); $messages = $this->getCachedData('searchEvents', $params, 60 * self::MESSAGE_CACHE_MINUTES); - if ($messages === false) { + if ($messages === false || !$messages) { if ($this->lastException) { return $this->lastException->getMessage(); } @@ -592,19 +588,17 @@ public function Messages() } $list = new ArrayList(); - if ($messages) { - foreach ($messages as $message) { - // If we have a transmission id but no subject, try to find the transmission details - if (isset($message['transmission_id']) && empty($message['subject']) && isset($transmissions[$message['transmission_id']])) { - $message = array_merge($transmissions[$message['transmission_id']], $message); - } - // In some case (errors, etc) we don't have a friendly from - if (empty($message['friendly_from']) && isset($message['msg_from'])) { - $message['friendly_from'] = $message['msg_from']; - } - $m = new ArrayData($message); - $list->push($m); + foreach ($messages as $message) { + // If we have a transmission id but no subject, try to find the transmission details + if (isset($message['transmission_id']) && empty($message['subject']) && isset($transmissions[$message['transmission_id']])) { + $message = array_merge($transmissions[$message['transmission_id']], $message); + } + // In some case (errors, etc) we don't have a friendly from + if (empty($message['friendly_from']) && isset($message['msg_from'])) { + $message['friendly_from'] = $message['msg_from']; } + $m = new ArrayData($message); + $list->push($m); } return $list; @@ -787,7 +781,11 @@ public function WebhookUrl() } } $protocol = Director::protocol(); - return $protocol . $this->getDomain() . '/sparkpost/incoming'; + $domain = $this->getDomain(); + if (!$domain) { + throw new Exception("No domain for webhook"); + } + return $protocol . $domain . '/sparkpost/incoming'; } /** @@ -939,7 +937,7 @@ public function VerifySendingDomain() $client = SparkPostHelper::getClient(); $host = $this->getDomain(); - if (!$host && is_bool($host)) { + if (!$host || is_bool($host)) { return false; } @@ -1044,8 +1042,9 @@ public function getDomainFromHost() public function getDomainFromEmail() { $email = SparkPostHelper::resolveDefaultFromEmail(null, false); - if ($email) { - $domain = substr((string)strrchr($email, "@"), 1); + if ($email && is_string($email)) { + $emailat = (string)strrchr($email, "@"); + $domain = substr($emailat, 1); if (!$domain) { return false; }