function SymfonyMailer::mail

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Mail/Plugin/Mail/SymfonyMailer.php \Drupal\Core\Mail\Plugin\Mail\SymfonyMailer::mail()

Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().

Parameters

array $message: Message array with at least the following elements:

  • id: A unique identifier of the email type. Examples: 'contact_user_copy', 'user_password_reset'.
  • to: The mail address or addresses where the message will be sent to. The formatting of this string will be validated with the PHP email validation filter. Some examples:

  • subject: Subject of the email to be sent. This must not contain any newline characters, or the mail may not be sent properly. The subject is converted to plain text by the mail plugin manager.
  • body: Message to be sent. Accepts both CRLF and LF line-endings. Email bodies must be wrapped. For smart plain text wrapping you can use \Drupal\Core\Mail\MailFormatHelper::wrapMail() .
  • headers: Associative array containing all additional mail headers not defined by one of the other parameters. PHP's mail() looks for Cc and Bcc headers and sends the mail to addresses in these headers too.

Return value

bool TRUE if the mail was successfully accepted for delivery, otherwise FALSE.

Overrides MailInterface::mail

File

core/lib/Drupal/Core/Mail/Plugin/Mail/SymfonyMailer.php, line 120

Class

SymfonyMailer
Defines an experimental mail backend, based on the Symfony mailer component.

Namespace

Drupal\Core\Mail\Plugin\Mail

Code

public function mail(array $message) {
    try {
        $email = new Email();
        $headers = $email->getHeaders();
        foreach ($message['headers'] as $name => $value) {
            if (!in_array(strtolower($name), self::SKIP_HEADERS, TRUE)) {
                if (in_array(strtolower($name), self::MAILBOX_LIST_HEADERS, TRUE)) {
                    // Split values by comma, but ignore commas encapsulated in double
                    // quotes.
                    $value = str_getcsv($value, escape: '\\');
                }
                $headers->addHeader($name, $value);
            }
        }
        $email->to($message['to'])
            ->subject($message['subject'])
            ->text($message['body']);
        $mailer = $this->getMailer();
        $mailer->send($email);
        return TRUE;
    } catch (\Exception $e) {
        Error::logException($this->logger, $e);
        return FALSE;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.