You can use symfony/mailer directly

until drupal/symfony_mailer is stable

Swiftmailer library is not supported since march 2022. The drupal/symfony_mailer project is not yet stable (alpha2). :-(

In the meantime you can easely use symfony/mailer directly.

I created a custom mailer class, which I use in all custom modules. This I do already long time because I had to change the mailer already several times since Drupal 8 was released. Bevore I used the commerce mailer, because it seemed for me the best solution. Now I could change the mailer again without changing any code in all custom modules.

 

 

 

Create the modulename.services.yml file.

services:
  modulename.mail_helper:
    class: Drupal\modulename\MailHelper
    arguments: ['@config.factory', '@token','@file_system']

The only method you need is the sendMail() method. The other 2 method's are used for token replacement and get configuration from the configurable email GUI I created.

 

<?php
namespace Drupal\modulename;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Utility\Token;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;;

class MailHelper implements MailHelperInterface {

  /**
   * The config factory service.
   *
   * @var Drupal\Core\Config\ConfigFactoryInterface;
   */
  protected $configFactory;

  /**
   * The token service.
   *
   * @var Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * The file manager service.
   *
   * @var \Drupal\Core\File\FileSystem;
   */
  protected $fileSystem;

  /**
   * MailHelper constructor.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   * @param \Drupal\Core\Utility\Token $token
   * @param \Drupal\commerce\MailHandler $mail_handler
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    Token $token,
    FileSystem $file_system
  ) {
    $this->configFactory = $config_factory;
    $this->token = $token;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritDoc}
   */
  public function getEmailConfigTokenReplaced($config_key, $token_objects) {

    $data = [];

    $mail_config = $this->getMailConfig($config_key);

    $data['subject'] = $this->token->replace($mail_config['email_subject'], $token_objects, ['clear' => TRUE]);
    $data['body'] = $this->token->replace($mail_config['email_body'], $token_objects, ['clear' => TRUE]);

    $data['bcc'] = $mail_config['bcc_email'];
    $data['from'] = $mail_config['from_email'];

    return $data;
  }

  /**
   * {@inheritDoc}
   */
  public function getMailConfig($config_key) {

    $mail_config = [];

    // Retrieves email configuration.
    $config = $this->configFactory->get('custom_mail_ui.' . $config_key);
    $mail_config['email_subject'] = $config->get('email_subject');
    $mail_config['email_body'] = $config->get('email_body');
    $mail_config['bcc_email'] = $config->get('bcc_email');
    $mail_config['from_email'] = $config->get('from_email');

    return $mail_config;
  }

  /**
   * {@inheritdoc}
   */
  public function sendMail($module, $key, $to, $langcode, $params) 


    // We use direct symfony mailer until drupal/sympfony_mailer is stable.
    // @see: symfony/mailer/README.md
    // @see: https://symfony.com/doc/current/mailer.html
    // native://default|sendmail://default|smtp://user:pass@smtp.example.com:25
    $transport = Transport::fromDsn('sendmail://default');
    $mailer = new Mailer($transport);

    $email = (new Email())
      ->to($to)
      //->text($params['body'])
      ->html($params['body']);

    if (isset($params['subject']) && $params['subject']) {
      $email->subject($params['subject']);
    }
    if (isset($params['from']) && $params['from']) {
      $email->from($params['from']);
    }
    if (isset($params['cc']) && $params['cc']) {
      $email->cc($params['cc']);
    }

    if (isset($params['bcc']) && $params['bcc']) {
      $email->bcc($params['bcc']);
    }

    if (isset($params['reply_to']) && $params['reply_to']) {
      $email->replyTo($params['reply_to']);
    }

    if (isset($params['priority']) && $params['priority']) {
      $email->priority($params['priority']);
    }

    foreach ($params['files'] as $file) {
      if ($file instanceof \Drupal\file\Entity\File) {
        $uri = $file->getFileUri();
        $file_path = $this->fileSystem->realpath($uri);
      } else {
        $file_path = $file->uri;
      }
      // MimeType will be guessed.
      $email->attachFromPath($file_path);
    }

    $mailer->send($email);

    // send() has no return value. If no Error we return true.
    return TRUE;
  }
}

Thats it!

This sends a HTML email with file attachement.

@see: https://symfony.com/doc/current/mailer.html

Tags