diff --git a/README.txt b/README.txt index f2cc77d..35b10f8 100644 --- a/README.txt +++ b/README.txt @@ -92,7 +92,7 @@ $language The language used to compose the e-mail. $params An array of parameters. This is the $params optionally provided to - drupal_mail(). + \Drupal\Core\Mail\MailManagerInterface::mail $subject The subject. $body @@ -302,13 +302,13 @@ example demonstrates how this can be achieved. function test() { // Define message format. - $p = array( + $p = [ 'format' => 'text/html', 'charset' => 'UTF-8', - ); + ]; // Send message. - drupal_mail('mymodule', 'key', 'test@test.com', language_default(), $p); + \Drupal::service('plugin.manager.mail')->mail('mymodule', 'key', 'test@test.com', \Drupal::languageManager()->getDefaultLanguage()->getId(), $p); } @@ -337,3 +337,9 @@ simply provide the HTML version in $message['body'] and the plain text version in $message['plain']. Please make sure you set $message['params']['format'] to 'text/html'. The Swift Mailer module will not attempt to generate a plain text version if one is already available. + +5.0 Custom settings/behavior + +If you want to add custom settings or behavior to the mailer or the message, +before the it is sent, you can implement hook_swiftmailer_alter(). +See swiftmailer.api.php for an example. diff --git a/src/Plugin/Mail/SwiftMailer.php b/src/Plugin/Mail/SwiftMailer.php index 33e6143..3e89311 100644 --- a/src/Plugin/Mail/SwiftMailer.php +++ b/src/Plugin/Mail/SwiftMailer.php @@ -25,6 +25,7 @@ use Swift_Image; use Swift_Mailer; use Swift_MailTransport; use Swift_Message; +use Swift_NullTransport; use Swift_SendmailTransport; use Swift_SmtpTransport; use Swift_SpoolTransport; @@ -352,8 +353,6 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface { if (!empty($password)) { $transport->setPassword($password); } - - $mailer = Swift_Mailer::newInstance($transport); break; case SWIFTMAILER_TRANSPORT_SENDMAIL: @@ -363,13 +362,11 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface { // Instantiate transport. $transport = Swift_SendmailTransport::newInstance($path . ' -' . $mode); - $mailer = Swift_Mailer::newInstance($transport); break; case SWIFTMAILER_TRANSPORT_NATIVE: // Instantiate transport. $transport = Swift_MailTransport::newInstance(); - $mailer = Swift_Mailer::newInstance($transport); break; case SWIFTMAILER_TRANSPORT_SPOOL: @@ -377,10 +374,22 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface { $spooldir = $this->config['transport']['spool_directory']; $spool = new Swift_FileSpool($spooldir); $transport = Swift_SpoolTransport::newInstance($spool); - $mailer = Swift_Mailer::newInstance($transport); + break; + + case SWIFTMAILER_TRANSPORT_NULL: + $transport = Swift_NullTransport::newInstance(); break; } + if (!isset($transport)) { + throw new \LogicException('The transport method is undefined.'); + } + + $mailer = Swift_Mailer::newInstance($transport); + + // Allows other modules to customize the message. + $this->moduleHandler->alter('swiftmailer', $mailer, $m, $message); + // Send the message. Conversion::swiftmailer_filter_message($m); /** @var Swift_Mailer $mailer */ diff --git a/swiftmailer.api.php b/swiftmailer.api.php new file mode 100644 index 0000000..9b22c63 --- /dev/null +++ b/swiftmailer.api.php @@ -0,0 +1,31 @@ +setReadReceiptTo(['your@address.com']); + + // Register a SwiftMailer Plugin. + // @see https://swiftmailer.symfony.com/docs/plugins.html + $replacements = []; + $decorator = new Swift_Plugins_DecoratorPlugin($replacements); + $swiftMailer->registerPlugin($decorator); +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/swiftmailer.module b/swiftmailer.module index 5805349..a0aa238 100644 --- a/swiftmailer.module +++ b/swiftmailer.module @@ -23,6 +23,7 @@ define('SWIFTMAILER_TRANSPORT_SMTP', 'smtp'); define('SWIFTMAILER_TRANSPORT_SENDMAIL', 'sendmail'); define('SWIFTMAILER_TRANSPORT_NATIVE', 'native'); define('SWIFTMAILER_TRANSPORT_SPOOL', 'spool'); +define('SWIFTMAILER_TRANSPORT_NULL', 'null'); // Define header types. define('SWIFTMAILER_HEADER_TEXT', 'text'); diff --git a/tests/modules/swiftmailer_test/src/SwiftMailerDrupalStateLogger.php b/tests/modules/swiftmailer_test/src/SwiftMailerDrupalStateLogger.php new file mode 100644 index 0000000..2792648 --- /dev/null +++ b/tests/modules/swiftmailer_test/src/SwiftMailerDrupalStateLogger.php @@ -0,0 +1,40 @@ +add([ + 'method' => 'beforeSendPerformed', + 'body' => $evt->getMessage()->getBody(), + 'subject' => $evt->getMessage()->getSubject() + ]); + } + + public function sendPerformed(Swift_Events_SendEvent $evt) { + $this->add([ + 'method' => 'sendPerformed', + 'body' => $evt->getMessage()->getBody(), + 'subject' => $evt->getMessage()->getSubject() + ]); + } + + public function add($entry) { + $captured_emails = \Drupal::state()->get('swiftmailer.mail_collector') ?: []; + $captured_emails[] = $entry; + \Drupal::state()->set('swiftmailer.mail_collector', $captured_emails); + } + + public function clear() { + \Drupal::state()->delete('swiftmailer.mail_collector'); + } + + public function dump() { + return \Drupal::state()->get('swiftmailer.mail_collector', []); + } + +} diff --git a/tests/modules/swiftmailer_test/swiftmailer_test.info.yml b/tests/modules/swiftmailer_test/swiftmailer_test.info.yml new file mode 100644 index 0000000..20fc654 --- /dev/null +++ b/tests/modules/swiftmailer_test/swiftmailer_test.info.yml @@ -0,0 +1,6 @@ +name: 'SwiftMailer Test Module' +type: module +description: 'Support module for SwiftMailer testing.' +package: Testing +version: VERSION +core: 8.x diff --git a/tests/modules/swiftmailer_test/swiftmailer_test.module b/tests/modules/swiftmailer_test/swiftmailer_test.module new file mode 100644 index 0000000..a91a951 --- /dev/null +++ b/tests/modules/swiftmailer_test/swiftmailer_test.module @@ -0,0 +1,15 @@ +setBody('Replace text in swiftmailer_test_swiftmailer_alter'); + $swiftMailer->registerPlugin(new SwiftMailerDrupalStateLogger()); + } +} diff --git a/tests/src/Functional/SwiftMailerAlterTest.php b/tests/src/Functional/SwiftMailerAlterTest.php new file mode 100644 index 0000000..3ab99dd --- /dev/null +++ b/tests/src/Functional/SwiftMailerAlterTest.php @@ -0,0 +1,35 @@ +getEditable('mailsystem.settings') + ->set('modules.swiftmailer_test.none', [ + 'formatter' => 'swiftmailer', + 'sender' => 'swiftmailer', + ]) + ->save(); + Drupal::configFactory() + ->getEditable('swiftmailer.transport') + ->set('transport', 'null') + ->save(); + \Drupal::state()->set('swiftmailer_test_swiftmailer_alter_1', TRUE); + \Drupal::service('plugin.manager.mail')->mail('swiftmailer_test', 'test_1', 'test@example.com', \Drupal::languageManager()->getDefaultLanguage()->getId()); + $logger = new SwiftMailerDrupalStateLogger(); + $this->assertEquals('Replace text in swiftmailer_test_swiftmailer_alter', $logger->dump()[0]['body']); + } + +}