You are here

public function SendinblueManager::sendEmail in SendinBlue 8

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

Send mail.

Parameters

string $type: A type of email.

string $to_email: A recipe address.

string $template_id: A template identification.

File

src/SendinblueManager.php, line 450

Class

SendinblueManager
Basic manager of module.

Namespace

Drupal\sendinblue

Code

public function sendEmail($type, $to_email, $template_id = '-1') {
  $subjects = [
    'confirm' => $this
      ->t('Subscription confirmed'),
    'test' => $this
      ->t('[SendinBlue SMTP] test email'),
  ];
  $account_email = $this
    ->getAccountEmail();
  $account_username = $this
    ->getAccountUsername();

  // Set subject info.
  $subject = $subjects[$type] ?? '[SendinBlue]';
  $sender_email = !empty($account_email) ? $account_email : $this
    ->t('no-reply@sendinblue.com');
  $sender_name = !empty($account_username) ? $account_username : $this
    ->t('SendinBlue');

  // Get template html and text.
  $template_contents = $this
    ->getEmailTemplate($type);
  $html_content = $template_contents['html_content'];
  $text_content = $template_contents['text_content'];
  if ($type === "confirm" && $template_id !== '-1') {
    $template = $this->sendinblueMailin
      ->getTemplate($template_id);
    if ($template !== NULL) {
      $html_content = $template
        ->getHtmlContent();
      $subject = $template
        ->getSubject();
      if ($template
        ->getFromName() !== '[DEFAULT_FROM_NAME]' && $template
        ->getFromEmail() !== '[DEFAULT_FROM_EMAIL]') {
        $sender_name = $template
          ->getFromName();
        $sender_email = $template
          ->getFromEmail();
      }
    }
  }

  // Send mail.
  $replyTo = [
    'email' => $sender_email,
    'name' => $sender_name,
  ];
  $from = [
    'email' => $sender_email,
    'name' => $sender_name,
  ];
  $to = [
    'email' => $to_email,
  ];
  $base_url = $this
    ->getBaseUrl();
  $site_domain = str_replace([
    'https://',
    'http://',
  ], '', $base_url);
  $html_content = str_replace([
    '{title}',
    '{site_domain}',
  ], [
    $subject,
    $site_domain,
  ], $html_content);
  $text_content = str_replace('{site_domain}', $base_url, $text_content);
  $this->sendinblueMailin
    ->sendEmail($to, $subject, $html_content, $text_content, $from, $replyTo);
}