/** * {@inheritdoc} */ public function notify(NotificationInterface $notification) { $failedSms = []; $tos = $notification->getTo(); if (empty($tos)) { throw new NotificationFailedException('No recipients specified'); } foreach ($tos as $to) { $skebbySms = SkebbySms::create()->addRecipient($to)->setText($notification->getContent()); $result = new Result('skebby', $this->name, Result::OK); try { $response = $this->skebby->send($skebbySms)[0]; $result->setResponse($response); if (!$response->isSuccessful()) { $result->setResult(Result::FAIL); $failedSms[] = ['to' => $to, 'error_message' => $response->getErrorMessage()]; } } catch (\Exception $e) { $result->setResult(Result::FAIL)->setResponse($e); $failedSms[] = ['to' => $to, 'error_message' => $e->getMessage()]; } $notification->addResult($result); } if (count($tos) === count($failedSms)) { throw new NotificationFailedException('All the sms failed to be send', ['failed_sms' => $failedSms]); } }
/** * {@inheritdoc} */ public function notify(NotificationInterface $notification) { /** @var Email $notification */ if (null === $this->converter) { $this->converter = new SwiftMailerConverter(); } if (!empty($notification->getTo()) || !empty($notification->getCc()) || !empty($notification->getBcc())) { $email = $this->converter->convert($notification); $result = $this->mailer->send($email); $res = new Result('swiftmailer', $this->getName(), $result > 0); $res->setResponse($result); $notification->addResult($res); if (0 === $result) { throw new NotificationFailedException('Mailer reported all recipient failed'); } } }
/** * Send a notification. * Will trigger pre/post/notify events if an event dispatcher is set * * @param NotificationInterface $notification * * @throws NotificationFailedException */ public function notify(NotificationInterface $notification) { $notified = false; $this->eventDispatcher->dispatch(Events::PRE_NOTIFY, new PreNotifyEvent($notification)); foreach ($this->handlers as $handler) { if ($notification->getHandlerName() !== $handler->getName()) { continue; } $notified = $this->handle($notification, $handler) || $notified; } if ($this->throwIfNotNotified && !$notified) { $message = 'No handler has been defined for ' . get_class($notification); if (method_exists($notification, 'getConfig')) { $message .= ' (' . json_encode($notification->getConfig()) . ')'; } throw new NotificationFailedException($message); } $this->eventDispatcher->dispatch(Events::POST_NOTIFY, new PostNotifyEvent($notification)); }
/** * {@inheritdoc} */ public function notify(NotificationInterface $notification) { if (!class_exists('Swift_Message')) { throw new \RuntimeException('You need to install swift mailer to use mailgun transport'); } /** @var Email $notification */ $message = \Swift_Message::newInstance($notification->getSubject())->setFrom($notification->getFrom()); foreach ($notification->getParts() as $part) { $message->attach(\Swift_MimePart::newInstance($part->getContent(), $part->getContentType())); } foreach ($notification->getAttachments() as $attachment) { $message->attach(\Swift_Attachment::newInstance($attachment->getContent(), $attachment->getName(), $attachment->getContentType())); } $postData = ['o:tag' => $notification->getTags()]; foreach ($notification->getMetadata() as $key => $value) { $postData['v:' . $key] = $value; } if ($recipientVariables = $notification->getRecipientVariables()) { $postData['recipient-variables'] = json_encode($recipientVariables); } $failed = []; $success = []; $to = array_merge(array_values($notification->getTo()), array_values($notification->getCc()), array_values($notification->getBcc())); if (!empty($to)) { foreach (array_chunk($to, 1000) as $to_chunk) { $result = new Result('mailgun', $this->getName()); $data = $postData; $data['to'] = $to_chunk; $res = $this->mailgun->sendMessage($this->domain, $data, $message->toString()); if ($res->http_response_code == 200) { $success[] = $res; } else { $result->setResult(Result::FAIL); $failed[] = $res; } $result->setResponse($res); $notification->addResult($result); } if (count($success) === 0) { throw new NotificationFailedException("Sending failed for message {$notification->getSubject()}", $failed); } } }