You are here

public function SendinblueApiV3::sendEmail in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Tools/Api/SendinblueApiV3.php \Drupal\sendinblue\Tools\Api\SendinblueApiV3::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/SendinblueApiV3.php, line 220

Class

SendinblueApiV3
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 = []) {
  $to += [
    'email' => NULL,
    'name' => NULL,
  ];
  $sibTo = new SendSmtpEmailTo([
    'email' => $to['email'],
    'name' => $to['name'],
  ]);
  $emailData = [
    'to' => [
      $sibTo,
    ],
    'htmlContent' => $html,
    'textContent' => $text,
    'subject' => $subject,
  ];
  if (!empty($from)) {
    $from += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['sender'] = new SendSmtpEmailSender([
      'email' => $from['email'],
      'name' => $from['name'],
    ]);
  }
  if (!empty($bcc)) {
    $bcc += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['bcc'] = [
      new SendSmtpEmailBcc([
        'email' => $bcc['email'],
        'name' => $bcc['name'],
      ]),
    ];
  }
  if (!empty($cc)) {
    $cc += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['cc'] = [
      new SendSmtpEmailCc([
        'email' => $cc['email'],
        'name' => $cc['name'],
      ]),
    ];
  }
  if (!empty($replyto)) {
    $replyto += [
      'email' => NULL,
      'name' => NULL,
    ];
    $emailData['replyTo'] = new SendSmtpEmailReplyTo([
      'email' => $replyto['email'],
      'name' => $replyto['name'],
    ]);
  }
  if (!empty($headers)) {
    $emailData['headers'] = $headers;
  }
  if (!empty($attachment)) {
    $attachments = [];
    foreach ($attachment as $item) {
      $attachments[] = new SendSmtpEmailAttachment($item);
    }
    $emailData['attachment'] = $attachments;
  }
  $message = $this->sibTransactionalEmailsApi
    ->sendTransacEmail(new SendSmtpEmail($emailData));
  if ($message
    ->valid()) {
    return new CreateSmtpEmail($message
      ->getMessageId());
  }
  return NULL;
}