diff --git a/classes/tl_nc_gateway.php b/classes/tl_nc_gateway.php index d9697b82..6e9147fc 100644 --- a/classes/tl_nc_gateway.php +++ b/classes/tl_nc_gateway.php @@ -10,6 +10,7 @@ namespace NotificationCenter; +use Haste\Dca\PaletteManipulator; use NotificationCenter\Gateway\LabelCallbackInterface; use NotificationCenter\Model\Gateway; @@ -23,6 +24,39 @@ public function loadSettingsLanguageFile() \System::loadLanguageFile('tl_settings'); } + public function loadPalette($dc) + { + $paletteManipulator = PaletteManipulator::create(); + + $paletteManipulator->addLegend('gateway_legend', 'title_legend'); + + // Add the "email_overrideSmtp" field in Contao <4.10 + if (version_compare(VERSION, '4.10', '<')) { + $paletteManipulator + ->addField('email_overrideSmtp', 'gateway_legend', PaletteManipulator::POSITION_APPEND) + ->applyToPalette('email', 'tl_nc_gateway') + ; + return; + } + + // Show the legacy field "email_overrideSmtp" as long as it is configured + $gatewayModel = Gateway::findByPk($dc->id); + if (null !== $gatewayModel && 'email' === $gatewayModel->type && $gatewayModel->email_overrideSmtp) { + $paletteManipulator + ->addField('email_overrideSmtp', 'gateway_legend', PaletteManipulator::POSITION_APPEND) + ->addField('mailerTransport', 'gateway_legend', PaletteManipulator::POSITION_APPEND) + ->applyToPalette('email', 'tl_nc_gateway') + ; + return; + } + + // Just show the "mailerTransport" field instead of the "email_overrideSmtp" + $paletteManipulator + ->addField('mailerTransport', 'gateway_legend', PaletteManipulator::POSITION_APPEND) + ->applyToPalette('email', 'tl_nc_gateway') + ; + } + /** * Validate the queue delay * diff --git a/config/config.php b/config/config.php index 3f02b4dc..de7c4c9b 100644 --- a/config/config.php +++ b/config/config.php @@ -81,6 +81,7 @@ $GLOBALS['TL_HOOKS']['updatePersonalData'][] = array('NotificationCenter\ContaoHelper', 'sendPersonalDataEmail'); $GLOBALS['TL_HOOKS']['getUserNavigation'][] = array('NotificationCenter\ContaoHelper', 'addQueueToUserNavigation'); $GLOBALS['TL_HOOKS']['activateAccount'][] = array('NotificationCenter\ContaoHelper', 'sendActivationEmail'); +$GLOBALS['TL_HOOKS']['getSystemMessages'][] = array('NotificationCenter\ContaoHelper', 'alertLegacySmtpSetting'); /** * Queue manager diff --git a/dca/tl_nc_gateway.php b/dca/tl_nc_gateway.php index cd4e33d9..541e23b8 100644 --- a/dca/tl_nc_gateway.php +++ b/dca/tl_nc_gateway.php @@ -21,7 +21,8 @@ 'enableVersioning' => true, 'onload_callback' => array ( - array('NotificationCenter\tl_nc_gateway', 'loadSettingsLanguageFile') + array('NotificationCenter\tl_nc_gateway', 'loadSettingsLanguageFile'), + array('NotificationCenter\tl_nc_gateway', 'loadPalette') ), 'onsubmit_callback' => array ( @@ -98,7 +99,7 @@ '__selector__' => array('type', 'queue_cronEnable', 'email', 'email_overrideSmtp', 'file_connection'), 'default' => '{title_legend},title,type', 'queue' => '{title_legend},title,type;{gateway_legend},queue_targetGateway,queue_delay;{cronjob_legend},queue_cronExplanation,queue_cronEnable', - 'email' => '{title_legend},title,type;{gateway_legend},email_overrideSmtp,', + 'email' => '{title_legend},title,type', 'file' => '{title_legend},title,type;{gateway_legend},file_type,file_connection', 'postmark' => '{title_legend},title,type;{gateway_legend},postmark_key,postmark_test,postmark_ssl', ), @@ -251,6 +252,14 @@ 'eval' => array('mandatory'=>true, 'rgxp'=>'digit', 'nospace'=>true, 'tl_class'=>'w50'), 'sql' => "varchar(16) NOT NULL default ''" ), + 'mailerTransport' => array + ( + 'exclude' => true, + 'inputType' => 'select', + 'eval' => array('tl_class'=>'w50', 'includeBlankOption'=>true), + 'options_callback' => array(\Contao\CoreBundle\Mailer\AvailableTransports::class, 'getTransportOptions'), + 'sql' => "varchar(255) NOT NULL default ''" + ), 'file_type' => array ( 'label' => &$GLOBALS['TL_LANG']['tl_nc_gateway']['file_type'], diff --git a/languages/en/default.xlf b/languages/en/default.xlf new file mode 100644 index 00000000..aa18ac45 --- /dev/null +++ b/languages/en/default.xlf @@ -0,0 +1,10 @@ + + + + + + [Notification Center]: Using custom SMTP settings has been changed in Contao 4.10. Please visit <a href="https://docs.contao.org/manual/en/system/settings/#different-e-mail-configurations-and-sender-addresses">the documentation</a> to learn how to define SMTP settings. Then, please update all email gateways that need this feature and disable "Override SMTP settings". + + + + diff --git a/languages/en/tl_nc_gateway.xlf b/languages/en/tl_nc_gateway.xlf index 80705494..67ed4605 100644 --- a/languages/en/tl_nc_gateway.xlf +++ b/languages/en/tl_nc_gateway.xlf @@ -58,6 +58,14 @@ This gateway will take the Contao e-mail settings by default. If you want to override the SMTP settings for this specific gateway, activate this checkbox. This gateway will take the Contao e-mail settings by default. If you want to override the SMTP settings for this specific gateway, activate this checkbox. + + Mailer transport + Mailer transport + + + Here you can override the mailer transport used to send e-mails. + Here you can override the mailer transport used to send e-mails. + File type File type diff --git a/library/NotificationCenter/ContaoHelper.php b/library/NotificationCenter/ContaoHelper.php index b4c4b890..43f3f77d 100644 --- a/library/NotificationCenter/ContaoHelper.php +++ b/library/NotificationCenter/ContaoHelper.php @@ -11,6 +11,8 @@ namespace NotificationCenter; +use NotificationCenter\Model\Gateway; + class ContaoHelper extends \Controller { /** @@ -166,4 +168,25 @@ public function addQueueToUserNavigation($arrModules, $blnShowAll) return $arrModules; } + + /** + * Show a warning if legacy SMTP settings are defined in Contao 4.10. + * Hook: getSystemMessages + */ + public function alertLegacySmtpSetting() + { + if (version_compare(VERSION, '4.10', '<') && $this->objModel->email_overrideSmtp) { + return ''; + } + + $legacyGateways = Gateway::findBy(["type='email'", "email_overrideSmtp='1'"], []); + if (null === $legacyGateways) { + return ''; + } + + return sprintf( + '

%s

', + $GLOBALS['TL_LANG']['MSC']['notification_center']['warn_legacy_smtp'] + ); + } } diff --git a/library/NotificationCenter/Gateway/Email.php b/library/NotificationCenter/Gateway/Email.php index a2c17203..49744489 100644 --- a/library/NotificationCenter/Gateway/Email.php +++ b/library/NotificationCenter/Gateway/Email.php @@ -48,11 +48,17 @@ public function createDraft(Message $objMessage, array $arrTokens, $strLanguage return new EmailMessageDraft($objMessage, $objLanguage, $arrTokens); } - /** - * @param EmailMessageDraft $objDraft - */ - public function sendDraft(EmailMessageDraft $objDraft) + private function instantiateEmail() { + if (version_compare(VERSION, '4.10', '>=')) { + $objEmail = new \Email(); + if ($this->objModel->mailerTransport) { + $objEmail->addHeader('X-Transport', $this->objModel->mailerTransport); + } + + return $objEmail; + } + // Override SMTP settings if desired if (version_compare(VERSION, '4.4', '>=') && $this->objModel->email_overrideSmtp) { if (method_exists(\Swift_SmtpTransport::class, 'newInstance')) { @@ -71,13 +77,23 @@ public function sendDraft(EmailMessageDraft $objDraft) $transport->setUsername($this->objModel->email_smtpUser)->setPassword($this->objModel->email_smtpPass); } - $objEmail = new \Email(new \Swift_Mailer($transport)); - } else { - $this->overrideSMTPSettings(); - $objEmail = new \Email(); - $this->resetSMTPSettings(); + return new \Email(new \Swift_Mailer($transport)); } + $this->overrideSMTPSettings(); + $objEmail = new \Email(); + $this->resetSMTPSettings(); + + return $objEmail; + } + + /** + * @param EmailMessageDraft $objDraft + */ + public function sendDraft(EmailMessageDraft $objDraft) + { + $objEmail = $this->instantiateEmail(); + // Set priority $objEmail->priority = $objDraft->getPriority(); diff --git a/library/NotificationCenter/Model/Notification.php b/library/NotificationCenter/Model/Notification.php index 3ec10b2c..b898e298 100644 --- a/library/NotificationCenter/Model/Notification.php +++ b/library/NotificationCenter/Model/Notification.php @@ -10,7 +10,9 @@ namespace NotificationCenter\Model; -class Notification extends \Model +use Contao\Model; + +class Notification extends Model { /**