Install and use Swift Mailer in Drupal 8 (part 1 : dependency)

 

Swiftmailer is abandoned and will be replaced by Symfony Mailer : check our post about Symfony Mailer

 

In a previous post from 2015, we described usage of Swift Mailer module to send HTML mail and mail with attachment. At this time, the module was not yet available for Drupal 8.

There is now a version alpha1 available. Let's go through installation process.

Because it has been rather tedious for us, we will try to explain the flow of the process as much as possible to help you save time.

 

1) Composer

 

The prerequisite is the installation of composer.

In our case we installed first on Windows inside a folder named  F:\Program Files\composer2\.

The installation exe for Windows can be found here.

However, it did not work in our case and we needed to install it manually with the below command (see more details here):

 

php -c C:\windows\php.ini -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"

 

Once installed you will get the confirmation message:

 

Installation on Ubuntu server was very easy (see how):

 

2) Composer manager install and init

The Drupal 8 version of this module is deprecated and no longer needed, due to improvements in Drupal 8.1. Use Composer directly to get the needed modules, which will also download their required libraries.

 

If you try to install the Swift Mailer module now, without dependencies, you will get the following error message:

Thus we will use composer manager to install the library as suggested by the module.

Our version of composer manager is 8.x-1.0-rc1+0-dev. After installing the module (/admin/modules), the status report (/admin/reports/status) indicates that the module has to be initialized:

 

Composer Manager

Not initialized Run the module's init.php script on the command line

 

Init.php is in Drupal_path/modules/composer_manager/scripts/. We use the following command in Windows to do the initialization as required:

 

\Drupal path\modules\composer_manager\scripts\php init.php

 

Then we get the confirmation as follow:

 

For Ubuntu, the process is similar:

 


What happened in practice is that the "composer.json" file located in Drupal root has been updated as in the example below:

 

autoload": {
"psr-4": {
"Drupal\\Core\\Composer\\": "core/lib/Drupal/Core/Composer",
"Drupal\\composer_manager\\Composer\\": "F:\\path to module\\composer_manager/src/Composer"
}
},
"scripts": {
"pre-autoload-dump": "Drupal\\Core\\Composer\\Composer::preAutoloadDump",
"post-autoload-dump": "Drupal\\Core\\Composer\\Composer::ensureHtaccess",
"post-package-install": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup",
"post-package-update": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup",
"drupal-rebuild": "Drupal\\composer_manager\\Composer\\Command::rebuild",
"drupal-update": "Drupal\\composer_manager\\Composer\\Command::update"
}

 

 

Going back to the status report now indicates that we can run drupal-update to update modules dependencies via composer:

 

Composer Manager

Composer update needed. Run composer drupal-update on the command line to update dependencies.

 

 

Composer-manager has its own report as well with the following indications:

 

 

 

3) Update dependencies

 

 

From windows command line, run "composer drupal-update" from within the Drupal installation root:

If you get an error, you can also run the update by pointing to the composer file as per example below:

 

The libraries will be updated accordingly and specifically those needed by Swift Mailer:

You can check that in the folder \Drupal\vendor the swiftmailer and html2text folder are now present.

 

For Ubuntu, the process is the same for dependencies update:

 

 

4) Install Swift Mailer

 

Ok, now you can try again to install Swift Mailer module.

 

At this point, you may still have the error above about missing library which is weird since we just updated it.

Actually, it may be possible that composer did not update the autoloader properly (bug?). You may solve this by running the command

or

 

After this you should get the expected result:

 

We hope you can benefit from this experience.

Feel free to leave a comment or question if you have any.

On a next post we will see how we use Swift Mailer to attach documents to mail.

 

 

Comments

Jose Martinez

5 years 6 months ago

Great tutorial. I had installed Swiftmailer before, but after I updated the site I started getting an error. The dump autoloader part of the tutorial saved me.

 

Thanks!

Omkar Gaonkar

3 years 7 months ago

It's work for me. using this command

@ C:\xampp\htdocs\your_project_name> composer require drupal/swiftmailer 

@   composer require 'drupal/swiftmailer:^2.0'

Add new comment

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.