You are here

public static function SendinblueManager::sendEmail in SendinBlue 7.2

Same name and namespace in other branches
  1. 7 includes/sendinblue.manage.inc \SendinblueManager::sendEmail()

Send mail.

Parameters

string $type: A type of email.

string $to_email: A recipe address.

string $template_id: A template identification.

2 calls to SendinblueManager::sendEmail()
sendinblue_send_email_form_submit in includes/sendinblue_home.admin.inc
Submit Handler of Form to send email of Home page.
sendinblue_signup_subscribe_form_validate in ./sendinblue.module
Validate handler to add users to lists on subscription form submission.

File

includes/sendinblue.manage.inc, line 239
Manage class file.

Class

SendinblueManager
Basic manager of module.

Code

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

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

  // Get template html and text.
  $template_contents = self::getEmailTemplate($type);
  $html_content = $template_contents['html_content'];
  $text_content = $template_contents['text_content'];
  if ($type === "confirm" && $template_id !== '-1') {
    $template = $sendinblueMailin
      ->getCampaign($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 = self::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);
  $sendinblueMailin
    ->sendEmail($to, $subject, $html_content, $text_content, $from, $replyTo);
}