You are here

protected function MimeMail::prepareMessage in Mime Mail 8

Prepares the message for sending.

Parameters

array $message: An array containing the message data. The optional parameters are:

  • plain: Whether to send the message as plaintext only or HTML. If this evaluates to TRUE the message will be sent as plaintext.
  • plaintext: Optional plaintext portion of a multipart email.
  • attachments: An array of arrays which describe one or more attachments. Existing files can be added by path, dynamically-generated files can be added by content. The internal array contains the following elements:

    • filepath: Relative Drupal path to an existing file (filecontent is NULL).
    • filecontent: The actual content of the file (filepath is NULL).
    • filename: The filename of the file.
    • filemime: The MIME type of the file.

    The array of arrays looks something like this: Array ( [0] => Array ( [filepath] => '/sites/default/files/attachment.txt' [filecontent] => 'My attachment.' [filename] => 'attachment.txt' [filemime] => 'text/plain' ) )

Return value

array All details of the message.

1 call to MimeMail::prepareMessage()
MimeMail::format in src/Plugin/Mail/MimeMail.php
Concatenates and wraps the email body for plain-text mails.

File

src/Plugin/Mail/MimeMail.php, line 141

Class

MimeMail
Defines the default Drupal mail backend, using PHP's native mail() function.

Namespace

Drupal\mimemail\Plugin\Mail

Code

protected function prepareMessage(array $message) {
  $module = $message['module'];
  $key = $message['key'];
  $to = $message['to'];
  $from = $message['from'];
  $subject = $message['subject'];
  $body = $message['body'];
  $headers = isset($message['params']['headers']) ? $message['params']['headers'] : [];
  $plain = isset($message['params']['plain']) ? $message['params']['plain'] : NULL;
  $plaintext = isset($message['params']['plaintext']) ? $message['params']['plaintext'] : NULL;
  $attachments = isset($message['params']['attachments']) ? $message['params']['attachments'] : [];
  $site_name = $this->configFactory
    ->get('system.site')
    ->get('name');
  $site_mail = $this->configFactory
    ->get('system.site')
    ->get('mail');
  $simple_address = $this->configFactory
    ->get('mimemail.settings')
    ->get('simple_address');

  // Override site mails default sender.
  if (empty($from) || $from == $site_mail) {
    $mimemail_name = $this->configFactory
      ->get('mimemail.settings')
      ->get('name');
    $mimemail_mail = $this->configFactory
      ->get('mimemail.settings')
      ->get('mail');
    $from = [
      'name' => !empty($mimemail_name) ? $mimemail_name : $site_name,
      'mail' => !empty($mimemail_mail) ? $mimemail_mail : $site_mail,
    ];
  }
  if (empty($body)) {

    // Body is empty, this is a plaintext message.
    $plain = TRUE;
  }
  elseif (is_null($plain)) {
    if (is_string($to) && $this->emailValidator
      ->isValid($to)) {
      $user_plaintext_field = $this->configFactory
        ->get('mimemail.settings')
        ->get('user_plaintext_field');
      if (is_object($account = user_load_by_mail($to)) && $account
        ->hasField($user_plaintext_field)) {

        /* @var boolean $plain */
        $plain = $account->{$user_plaintext_field}->value;

        // Might as well pass the user object to the address function.
        $to = $account;
      }
    }
  }

  // Removing newline character introduced by _drupal_wrap_mail_line().
  $subject = str_replace([
    "\n",
  ], '', trim(MailFormatHelper::htmlToText($subject)));
  $body = [
    '#theme' => 'mimemail_message',
    '#module' => $module,
    '#key' => $key,
    '#recipient' => $to,
    '#subject' => $subject,
    '#body' => $body,
  ];
  $body = $this->renderer
    ->renderPlain($body);

  /*foreach (module_implements('mail_post_process') as $module) {
      $function = $module . '_mail_post_process';
      $function($body, $key);
    }*/
  $plain = $plain || $this->configFactory
    ->get('mimemail.settings')
    ->get('textonly');
  $from = MimeMailFormatHelper::mimeMailAddress($from);
  $mail = MimeMailFormatHelper::mimeMailHtmlBody($body, $subject, $plain, $plaintext, $attachments);
  $headers = array_merge($message['headers'], $headers, $mail['headers']);
  $message['to'] = MimeMailFormatHelper::mimeMailAddress($to, $simple_address);
  $message['from'] = $from;
  $message['subject'] = $subject;
  $message['body'] = $mail['body'];
  $message['headers'] = MimeMailFormatHelper::mimeMailHeaders($headers, $from);
  return $message;
}