How does a module tell Drupal which mail plugin should be used? How does a module tell Drupal which mail plugin should be used? The SMTP Authentication Support module uses the following code in the submission handler of its settings form.

$values = $form_state-&gt;getValues();
$config = $this-&gt;configFactory-&gt;getEditable('smtp.settings');
$mail_config = $this-&gt;configFactory-&gt;getEditable('system.mail');
$mail_system = $mail_config-&gt;get('interface');

// Updating config vars.
if (isset($values['smtp_password'])) {
  $config-&gt;set('smtp_password', $values['smtp_password']);
}
$config-&gt;set('smtp_on', $values['smtp_on'])
  -&gt;set('smtp_host', $values['smtp_host'])
  -&gt;set('smtp_hostbackup', $values['smtp_hostbackup'])
  -&gt;set('smtp_port', $values['smtp_port'])
  -&gt;set('smtp_protocol', $values['smtp_protocol'])
  -&gt;set('smtp_username', $values['smtp_username'])
  -&gt;set('smtp_from', $values['smtp_from'])
  -&gt;set('smtp_fromname', $values['smtp_fromname'])
  -&gt;set('smtp_allowhtml', $values['smtp_allowhtml'])
  -&gt;set('smtp_debugging', $values['smtp_debugging'])
  -&gt;save();

// If an address was given, send a test e-mail message.
if ($test_address = $values['smtp_test_address']) {
  $params['subject'] = t('Drupal SMTP test e-mail');
  $params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
  $account = \Drupal::currentUser();
  // If module is off, send the test message with SMTP by temporarily overriding.
  if (!$config-&gt;get('smtp_on')) {
    $original = $mail_config-&gt;get('interface');
    $mail_system['default'] = 'SMTPMailSystem';
    $mail_config-&gt;set('interface', $mail_system)-&gt;save();
  }
  \Drupal::service('plugin.manager.mail')-&gt;mail('smtp', 'smtp-test', $test_address, $account-&gt;getPreferredLangcode(), $params);
  if (!$config-&gt;get('smtp_on')) {
    $mail_config-&gt;set('interface', $original)-&gt;save();
  }
  drupal_set_message(t('A test e-mail has been sent to @email via SMTP. You may want to check the log for any error messages.', ['@email' =&gt; $test_address]));
}


This code is not working, since in the log set with the mail.log directive I see the following, where it is evident that Drupal is still using the mail plugin definined in PhpMail.php.


  [11-Mar-2016 06:36:14 UTC] mail() on [/home/domain/public_html/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php:101]: To: edited@email.org -- Headers: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes Content-Transfer-Encoding: 8Bit X-Mailer: Drupal Sender: edited@email.org From: Edited &lt;edited@email.org&gt;


I cleared the log before sending the test email from the administrative page of the SMTP Authentication Support module; so, I am sure the log is caused from sending the test email.

Since the method used from this module doesn't seem to work, what is the correct way to set the plugin Drupal uses to send emails?

Update

To make my question clearer, there are two things that are not clear to me in the code being used.


The code is setting the mail plugin Drupal should use, just for the test email, but as I can see that method doesn't work, since Drupal still uses the default plugin.
Apart from setting the mail plugin for the test email, the module doesn't change the mail plugin set by Drupal. How can Drupal know to use the mail plugin implemented by the module, then?
Basing on Mail backends are now annotated plugins, drupal_mail_service() has been removed and the documentation for the MailManagerInterface interface, a module implementing a mail plugin should not set its own plugin as default plugin because MailManagerInterface interface doesn't document any configuration key the classes implementing it should use; only the site maintainer eventually know which mail manager is being used, and which configuration keys that mail manager is using. This is similar to setting the cache handlers, which is done in the settings.php file.

The SMTP Authentication module is doing the correct thing when it doesn't set its plugin as default plugin for sending emails.  

As for the code to send a test email, the correct one should not directly set the configuration keys, if it wants to be independent from the current implementation of the mail manager. Eventually, code similar to the following should be used.

$mailer = \Drupal::service('plugin.manager.mail')-&gt;createInstance($plugin_id);
$bool = $mailer-&gt;send($mailer-&gt;format($message));