You are here

public function HtmlMailSystem::mail in HTML Mail 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Mail/HTMLMailSystem.php \Drupal\htmlmail\Plugin\Mail\HTMLMailSystem::mail()

Send an email message.

Parameters

array $message: An associative array containing at least:

  • headers: An associative array of (name => value) email headers.
  • body: The text/plain or text/html message body.
  • MailMIME: The message, parsed into a MailMIME object.

Return value

bool TRUE if the mail was successfully accepted or queued, FALSE otherwise.

Overrides MailInterface::mail

See also

drupal_mail()

https://documentation.mailgun.com/api-sending.html#sending

File

src/Plugin/Mail/HtmlMailSystem.php, line 363

Class

HtmlMailSystem
Modify the Drupal mail system to use HTML Mail when sending emails.

Namespace

Drupal\htmlmail\Plugin\Mail

Code

public function mail(array $message) {
  $eol = $this->siteSettings
    ->get('mail_line_endings', PHP_EOL);
  $params = [];

  // Ensure that subject is non-null.
  $message += [
    'subject' => t('(No subject)'),
  ];

  // Check for empty recipient.
  if (empty($message['to'])) {
    if (empty($message['headers']['To'])) {
      $this
        ->getLogger()
        ->error('Cannot send email about %subject without a recipient.', [
        '%subject' => $message['subject'],
      ]);
      return FALSE;
    }
    $message['to'] = $message['headers']['To'];
  }
  if (class_exists('HtmlMailMime')) {
    $mime = new HtmlMailMime($this->logger, $this->siteSettings, $this->mimeType, $this->fileSystem);
    $to = $mime
      ->mimeEncodeHeader('to', $message['to']);
    $subject = $mime
      ->mimeEncodeHeader('subject', $message['subject']);
    $txt_headers = $mime
      ->mimeTxtHeaders($message['headers']);
  }
  else {
    $to = Unicode::mimeHeaderEncode($message['to']);
    $subject = Unicode::mimeHeaderEncode($message['subject']);
    $txt_headers = $this
      ->txtHeaders($message['headers']);
  }
  $body = preg_replace('#(\\r\\n|\\r|\\n)#s', $eol, $message['body']);

  // Check for empty body.
  if (empty($body)) {
    $this
      ->getLogger()
      ->warning('Refusing to send a blank email to %recipient about %subject.', [
      '%recipient' => $message['to'],
      '%subject' => $message['subject'],
    ]);
    return FALSE;
  }
  if ($this->configVariables
    ->get('debug')) {
    $params = [
      $to,
      $subject,
      mb_substr($body, 0, min(80, strpos("\n", $body))) . '...',
      $txt_headers,
    ];
  }
  if (isset($message['headers']['Return-Path'])) {

    // A return-path was set.
    if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {

      // On Windows, PHP will use the value of sendmail_from for the
      // Return-Path header.
      $old_from = ini_get('sendmail_from');
      ini_set('sendmail_from', $message['headers']['Return-Path']);
      $result = @mail($to, $subject, $body, $txt_headers);
      ini_set('sendmail_from', $old_from);
    }
    else {

      // On most non-Windows systems, the "-f" option to the sendmail command
      // is used to set the Return-Path.
      // We validate the return path, unless it is equal to the site mail,
      // which we assume to be safe.
      $site_mail = $this
        ->getDefaultFromMail();
      $extra = $site_mail === $message['headers']['Return-Path'] || static::isShellSafe($message['headers']['Return-Path']) ? '-f' . $message['headers']['Return-Path'] : '';
      $result = @mail($to, $subject, $body, $txt_headers, $extra);
      if ($this->configVariables
        ->get('debug')) {
        $params[] = $extra;
      }
    }
  }
  else {

    // No return-path was set.
    $result = @mail($to, $subject, $body, $txt_headers);
  }
  if (!$result && $this->configVariables
    ->get('debug')) {
    $call = '@mail(' . implode(', ', $params) . ')';
    foreach ($params as $i => $value) {
      $params[$i] = var_export($value, TRUE);
    }
    $trace = print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), TRUE);
    $this
      ->getLogger()
      ->info('Mail sending failed because:<br /><pre>@call</pre><br />returned FALSE.<br /><pre>@trace</pre>', [
      '@call' => $call,
      '@trace' => $trace,
    ]);
  }
  return $result;
}