diff --git a/src/Form/TestForm.php b/src/Form/TestForm.php new file mode 100644 index 0000000..00c33a9 --- /dev/null +++ b/src/Form/TestForm.php @@ -0,0 +1,61 @@ + '

' . t('This page allows you to send a test e-mail to a recipient of your choice.') . '

', + ); + + $form['test'] = array( + '#type' => 'fieldset', + '#title' => t('Recipient'), + '#description' => '

' . t('You can send a test e-mail to a recipient of your choice. The e-mail will be sent using the default values as provided by the Swift Mailer module or as configured by you.') . '

', + ); + + $form['test']['recipient'] = array( + '#title' => t('E-mail'), + '#type' => 'textfield', + '#required' => TRUE, + '#default_value' => \Drupal::currentUser()->getEmail(), + ); + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Send'), + '#button_type' => 'primary', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + \Drupal::service('plugin.manager.mail')->mail('swiftmailer', 'test', $form_state->getValue(['test', 'recipient']), \Drupal::languageManager()->getDefaultLanguage()->getId()); + drupal_set_message(t('An attempt has been made to send an e-mail to @email.', ['@email' => $form_state->getValue(['test', 'recipient'])])); + } + +} diff --git a/src/Plugin/Mail/SwiftMailer.php b/src/Plugin/Mail/SwiftMailer.php index 5b5919d..2d6829e 100644 --- a/src/Plugin/Mail/SwiftMailer.php +++ b/src/Plugin/Mail/SwiftMailer.php @@ -510,7 +510,6 @@ class SwiftMailer implements MailInterface, ContainerFactoryPluginInterface { * */ private function getApplicableFormat($message) { - // Get the configured default format. $default_format = $this->config['message']['format']; diff --git a/swiftmailer.install b/swiftmailer.install index febc66c..123c07d 100644 --- a/swiftmailer.install +++ b/swiftmailer.install @@ -16,3 +16,10 @@ function swiftmailer_requirements($phase) { return $requirements; } + +/** + * Implements hook_install(). + */ +function swiftmailer_install() { + Drupal::configFactory()->getEditable('system.mail')->set('interface.swiftmailer', 'swiftmailer')->save(); +} diff --git a/swiftmailer.links.menu.yml b/swiftmailer.links.menu.yml index eeb90ed..2343d2a 100644 --- a/swiftmailer.links.menu.yml +++ b/swiftmailer.links.menu.yml @@ -15,3 +15,9 @@ swiftmailer.message_settings: route_name: swiftmailer.message_settings description: 'Configure how Swift Mailer will compose messages.' parent: swiftmailer.config + +swiftmailer.test: + title: 'Test' + route_name: swiftmailer.message_settings + description: 'Send a test e-mail to verify that the configuration works.' + parent: swiftmailer.config diff --git a/swiftmailer.links.task.yml b/swiftmailer.links.task.yml index 715cdda..c144909 100644 --- a/swiftmailer.links.task.yml +++ b/swiftmailer.links.task.yml @@ -7,3 +7,8 @@ swiftmailer.message_settings: route_name: swiftmailer.message_settings title: Messages base_route: swiftmailer.transport_settings + +swiftmailer.test: + route_name: swiftmailer.test + title: Test + base_route: swiftmailer.transport_settings diff --git a/swiftmailer.module b/swiftmailer.module index 8355839..c5d5a8e 100644 --- a/swiftmailer.module +++ b/swiftmailer.module @@ -4,6 +4,8 @@ * This is the primary module file. */ +use Drupal\Core\Render\Markup; + include_once __DIR__ . '/includes/helpers/utilities.inc'; // Define permissions. @@ -38,19 +40,16 @@ define('SWIFTMAILER_VARIABLE_CHARACTER_SET_DEFAULT', 'UTF-8'); * Implements hook_mail(). */ function swiftmailer_mail($key, &$message) { - $user = \Drupal::currentUser(); + $message['headers']['Content-Type'] = SWIFTMAILER_FORMAT_HTML; - //$message['params']['format'] = SWIFTMAILER_FORMAT_HTML; - - $text[] = '

' . t('Dear !user,', array('!user' => $user->getUsername())) . '

'; - $text[] = '

' . t('This e-mail has been sent from !site by the Swift Mailer module. The module has been successfully configured.', array('!site' => variable_get('site_name', 'a Drupal site'))) . '

'; + $text[] = '

' . t('Dear @user,', array('@user' => $user->getDisplayName())) . '

'; + $text[] = '

' . t('This e-mail has been sent from @site by the Swift Mailer module. The module has been successfully configured.', array('@site' => \Drupal::config('system.site')->get('name'))) . '

'; $text[] = t('Kind regards') . '

'; $text[] = t('The Swift Mailer module'); $message['subject'] = t('Swift Mailer has been successfully configured!'); - $message['body'] = $text; - + $message['body'] = array_map(function ($text) { return Markup::create($text); }, $text); } /** diff --git a/swiftmailer.routing.yml b/swiftmailer.routing.yml index 83f3b8b..1ac9690 100644 --- a/swiftmailer.routing.yml +++ b/swiftmailer.routing.yml @@ -21,3 +21,11 @@ swiftmailer.message_settings: _title: 'Message settings' requirements: _permission: 'administer swiftmailer' + +swiftmailer.test: + path: 'admin/config/people/swiftmailer/test' + defaults: + _form: '\Drupal\swiftmailer\Form\TestForm' + _title: Test + requirements: + _permission: 'administer swiftmailer'