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->getValues();
$config = $this->configFactory->getEditable('smtp.settings');
$mail_config = $this->configFactory->getEditable('system.mail');
$mail_system = $mail_config->get('interface');
// Updating config vars.
if (isset($values['smtp_password'])) {
$config->set('smtp_password', $values['smtp_password']);
}
$config->set('smtp_on', $values['smtp_on'])
->set('smtp_host', $values['smtp_host'])
->set('smtp_hostbackup', $values['smtp_hostbackup'])
->set('smtp_port', $values['smtp_port'])
->set('smtp_protocol', $values['smtp_protocol'])
->set('smtp_username', $values['smtp_username'])
->set('smtp_from', $values['smtp_from'])
->set('smtp_fromname', $values['smtp_fromname'])
->set('smtp_allowhtml', $values['smtp_allowhtml'])
->set('smtp_debugging', $values['smtp_debugging'])
->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->get('smtp_on')) {
$original = $mail_config->get('interface');
$mail_system['default'] = 'SMTPMailSystem';
$mail_config->set('interface', $mail_system)->save();
}
\Drupal::service('plugin.manager.mail')->mail('smtp', 'smtp-test', $test_address, $account->getPreferredLangcode(), $params);
if (!$config->get('smtp_on')) {
$mail_config->set('interface', $original)->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' => $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
<edited@email.org>
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?
interface.MODULE_NAME
entry somewhere that's overriding it? I am also not sure if plugin ids can have capital letters or not.