You are here

class SendInBlueEmail in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Mail/SendInBlueEmail.php \Drupal\sendinblue\Plugin\Mail\SendInBlueEmail

Defines the default Drupal mail backend, using PHP's native mail() function.

Plugin annotation


@Mail(
  id = "sendinblue_mail",
  label = @Translation("Sendinblue mailer"),
  description = @Translation("Send emails with Sendinblue mailer")
)

Hierarchy

Expanded class hierarchy of SendInBlueEmail

File

src/Plugin/Mail/SendInBlueEmail.php, line 21

Namespace

Drupal\sendinblue\Plugin\Mail
View source
class SendInBlueEmail implements MailInterface, ContainerFactoryPluginInterface {

  /**
   * SendinblueManager.
   *
   * @var \Drupal\sendinblue\SendinblueManager
   *   SendinblueManager
   */
  private $sendinblueManager;

  /**
   * Logger Service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   *   LoggerChannelFactory
   */
  protected $loggerFactory;

  /**
   * SendInBlueEmailConstructor.
   *
   * Allows to send SMTP emails with SendInBue API.
   */
  public function __construct(SendinblueManager $sendinblueManager, LoggerChannelFactoryInterface $logger_factory) {
    $this->sendinblueManager = $sendinblueManager;
    $this->loggerFactory = $logger_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($container
      ->get('sendinblue.manager'), $container
      ->get('logger.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function format(array $message) {

    // Join the body array into one string.
    $message['body'] = implode("\n\n", $message['body']);

    // Convert any HTML to plain-text.
    $message['body'] = MailFormatHelper::htmlToText($message['body']);

    // Wrap the mail body for sending.
    $message['body'] = MailFormatHelper::wrapMail($message['body']);
    return $message;
  }

  /**
   * {@inheritdoc}
   */
  public function mail(array $message) {
    try {
      $to = [
        'email' => $message['to'],
      ];
      $from = [
        'email' => $message['from'],
      ];
      $message['reply-to'] = !empty($message['reply-to']) ? $message['reply-to'] : $message['from'];
      $replyTo = [
        'email' => $message['reply-to'],
      ];
      unset($message['headers']['Content-Type']);
      $result = $this->sendinblueManager
        ->getSendinblueMailin()
        ->sendEmail($to, $message['subject'], nl2br($message['body']), $message['body'], $from, $replyTo, [], [], [], $message['headers']);
      if (empty($result
        ->getMessageId())) {
        $this->loggerFactory
          ->get('mail')
          ->error('[SENDINBLUE] - Error sending email (from %from to %to with reply-to %reply).', [
          '%from' => $message['from'],
          '%to' => $message['to'],
          '%reply' => $message['reply-to'] ? $message['reply-to'] : 'not set',
        ]);
        return FALSE;
      }
      $this->loggerFactory
        ->get('mail')
        ->info('[SENDINBLUE] - Sending email %messageId (from %from to %to).', [
        '%from' => $message['from'],
        '%to' => $message['to'],
        '%messageId' => $result
          ->getMessageId(),
      ]);
      return TRUE;
    } catch (\Exception $e) {
      $this->loggerFactory
        ->get('mail')
        ->error('[SENDINBLUE] - Error sending email (from %from to %to with reply-to %reply) [%error].', [
        '%from' => $message['from'],
        '%to' => $message['to'],
        '%reply' => $message['reply-to'] ? $message['reply-to'] : 'not set',
        '%error' => $e
          ->getMessage(),
      ]);
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SendInBlueEmail::$loggerFactory protected property Logger Service.
SendInBlueEmail::$sendinblueManager private property SendinblueManager.
SendInBlueEmail::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SendInBlueEmail::format public function Formats a message prior to sending. Overrides MailInterface::format
SendInBlueEmail::mail public function Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail(). Overrides MailInterface::mail
SendInBlueEmail::__construct public function SendInBlueEmailConstructor.