You are here

public function SendinblueApiV2::sendEmail in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Tools/Api/SendinblueApiV2.php \Drupal\sendinblue\Tools\Api\SendinblueApiV2::sendEmail()

Send email via sendinblue.

Parameters

array $to: A recipe email address.

string $subject: A subject of email.

string $html: A html body of email content.

string $text: A text body of email content.

array $from: A sender email address.

array $replyto: A reply address.

array $cc: A cc address.

array $bcc: A bcc address.

array $attachment: A attachment information.

array $headers: A header of email.

Return value

\Drupal\sendinblue\Tools\Model\CreateSmtpEmail An array of response code.

Overrides SendInBlueApiInterface::sendEmail

File

src/Tools/Api/SendinblueApiV2.php, line 190

Class

SendinblueApiV2
Sendinblue REST client.

Namespace

Drupal\sendinblue\Tools\Api

Code

public function sendEmail(array $to, string $subject, string $html, string $text, array $from = [], array $replyto = [], array $cc = [], array $bcc = [], array $attachment = [], array $headers = []) {
  $replyto += [
    'email' => NULL,
    'name' => NULL,
  ];
  $to += [
    'email' => NULL,
    'name' => NULL,
  ];
  $from += [
    'email' => NULL,
    'name' => NULL,
  ];
  $emailData = [
    "text" => $text,
    "replyto" => [
      $replyto['email'] => $replyto['name'],
    ],
    "html" => $html,
    "to" => [
      $to['email'] => $to['name'],
    ],
    "attachment" => $attachment,
    "from" => [
      $from['email'],
      $from['name'],
    ],
    "subject" => $subject,
    "headers" => $headers,
  ];
  if (!empty($cc)) {
    $cc += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['cc'] = [
      $cc['email'] => $cc['name'],
    ];
  }
  if (!empty($bcc)) {
    $bcc += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['bcc'] = [
      $bcc['email'] => $bcc['name'],
    ];
  }
  $message = $this->sIBHttpClient
    ->post("email", Json::encode($emailData));
  if ($message['code'] === 'success') {
    return new CreateSmtpEmail($message['data']['message-id']);
  }
  return NULL;
}