You are here

public function SendInBlueMailSystem::mail in SendinBlue 7.2

Send a message composed by drupal_mail().

Parameters

$message: Message array with at least the following elements:

  • id: A unique identifier of the e-mail type. Examples: 'contact_user_copy', 'user_password_reset'.
  • to: The mail address or addresses where the message will be sent to. The formatting of this string will be validated with the PHP e-mail validation filter. Some examples are:

  • subject: Subject of the e-mail to be sent. This must not contain any newline characters, or the mail may not be sent properly.
  • body: Message to be sent. Accepts both CRLF and LF line-endings. E-mail bodies must be wrapped. You can use drupal_wrap_mail() for smart plain text wrapping.
  • headers: Associative array containing all additional mail headers not defined by one of the other parameters. PHP's mail() looks for Cc and Bcc headers and sends the mail to addresses in these headers too.

Return value

TRUE if the mail was successfully accepted for delivery, otherwise FALSE.

Overrides MailSystemInterface::mail

File

includes/sendinblue.mail.inc, line 26
Implements SendInBlue as a Drupal MailSystemInterface.

Class

SendInBlueMailSystem
Modify the Drupal mail system to use SendInBlue when sending e-mails.

Code

public function mail(array $message) {
  $loggerFactory = new SendInBlueLoggerFactory();
  $sendinblueMailin = new SendinblueMailin();
  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 = $sendinblueMailin
      ->sendEmail($to, $message['subject'], nl2br($message['body']), $message['body'], $from, $replyTo, [], [], [], $message['headers']);
    if (empty($result
      ->getMessageId())) {
      $loggerFactory
        ->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;
    }
    $loggerFactory
      ->info('[SENDINBLUE] - Sending email %messageId (from %from to %to).', [
      '%from' => $message['from'],
      '%to' => $message['to'],
      '%messageId' => $result
        ->getMessageId(),
    ]);
    return TRUE;
  } catch (Exception $e) {
    $loggerFactory
      ->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;
  }
}