diff --git a/administrator/components/com_config/controller/application/sendtestmail.php b/administrator/components/com_config/controller/application/sendtestmail.php new file mode 100644 index 0000000000000..553933124b428 --- /dev/null +++ b/administrator/components/com_config/controller/application/sendtestmail.php @@ -0,0 +1,43 @@ +app->enqueueMessage(JText::_('JINVALID_TOKEN')); + $this->app->redirect('index.php'); + } + + if (!JFactory::getUser()->authorise('core.admin')) + { + $this->app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR')); + $this->app->redirect('index.php'); + } + + $model = new ConfigModelApplication; + echo new JResponseJson($model->sendTestMail()); + JFactory::getApplication()->close(); + } +} diff --git a/administrator/components/com_config/model/application.php b/administrator/components/com_config/model/application.php index fbb0babe49ec9..d072bd72505fd 100644 --- a/administrator/components/com_config/model/application.php +++ b/administrator/components/com_config/model/application.php @@ -430,4 +430,46 @@ public function storePermissions($permission) return $e->getMessage(); } } + + /** + * Method to send a test mail which is called via an AJAX request + * + * @return bool + * + * @since 3.6 + * @throws Exception + */ + public function sendTestMail() + { + // Set the new values to test with the current settings + $app = JFactory::getApplication(); + $input = $app->input; + + $app->set('smtpauth', $input->get('smtpauth')); + $app->set('smtpuser', $input->get('smtpuser', '', 'STRING')); + $app->set('smtppass', $input->get('smtppass', '', 'RAW')); + $app->set('smtphost', $input->get('smtphost')); + $app->set('smtpsecure', $input->get('smtpsecure')); + $app->set('smtpport', $input->get('smtpport')); + $app->set('mailfrom', $input->get('mailfrom', '', 'STRING')); + $app->set('fromname', $input->get('fromname', '', 'STRING')); + $app->set('mailer', $input->get('mailer')); + $app->set('mailonline', $input->get('mailonline')); + + // Prepare email and send try to send it + $mailSubject = JText::sprintf('COM_CONFIG_SENDMAIL_SUBJECT', $app->get('sitename')); + $mailBody = JText::sprintf('COM_CONFIG_SENDMAIL_BODY', JText::_('COM_CONFIG_SENDMAIL_METHOD_' . strtoupper($app->get('mailer')))); + + if (JFactory::getMailer()->sendMail($app->get('mailfrom'), $app->get('fromname'), $app->get('mailfrom'), $mailSubject, $mailBody) === true) + { + $methodName = JText::_('COM_CONFIG_SENDMAIL_METHOD_' . strtoupper($app->get('mailer'))); + $app->enqueueMessage(JText::sprintf('COM_CONFIG_SENDMAIL_SUCCESS', $app->get('mailfrom'), $methodName), 'success'); + + return true; + } + + $app->enqueueMessage(JText::_('COM_CONFIG_SENDMAIL_ERROR'), 'error'); + + return false; + } } diff --git a/administrator/components/com_config/view/application/tmpl/default_mail.php b/administrator/components/com_config/view/application/tmpl/default_mail.php index bdfed8d60c827..865f5e6d5704b 100644 --- a/administrator/components/com_config/view/application/tmpl/default_mail.php +++ b/administrator/components/com_config/view/application/tmpl/default_mail.php @@ -9,6 +9,15 @@ defined('_JEXEC') or die; +JHtml::script('system/sendtestmail.js', false, true); +JFactory::getDocument()->addScriptDeclaration(' + var sendtestmail_url = "' . addslashes(JUri::base()) . 'index.php?option=com_config&task=config.sendtestmail.application&format=json&' . JSession::getFormToken() . '=1"; + '); + $this->name = JText::_('COM_CONFIG_MAIL_SETTINGS'); $this->fieldsname = 'mail'; echo JLayoutHelper::render('joomla.content.options_default', $this); + +echo ''; diff --git a/administrator/language/en-GB/en-GB.com_config.ini b/administrator/language/en-GB/en-GB.com_config.ini index 234e43971f1ec..ef18de4f28c88 100644 --- a/administrator/language/en-GB/en-GB.com_config.ini +++ b/administrator/language/en-GB/en-GB.com_config.ini @@ -226,6 +226,14 @@ COM_CONFIG_PERMISSION_SETTINGS="Permission Settings" COM_CONFIG_PERMISSIONS="Permissions" COM_CONFIG_PROXY_SETTINGS="Proxy Settings" COM_CONFIG_SAVE_SUCCESS="Configuration successfully saved." +COM_CONFIG_SENDMAIL_ACTION_BUTTON="Send Test Mail" +COM_CONFIG_SENDMAIL_BODY="This is a test mail sent using "_QQ_"%s"_QQ_". If you receive it, then your email settings are correct!" +COM_CONFIG_SENDMAIL_ERROR="Test mail could not be sent." +COM_CONFIG_SENDMAIL_METHOD_MAIL="PHP Mail" +COM_CONFIG_SENDMAIL_METHOD_SENDMAIL="Sendmail" +COM_CONFIG_SENDMAIL_METHOD_SMTP="SMTP" +COM_CONFIG_SENDMAIL_SUBJECT="Test mail from %s" +COM_CONFIG_SENDMAIL_SUCCESS="The email was sent successfully to %s using %s. You should check that you've received the test email." COM_CONFIG_SEO_SETTINGS="SEO Settings" COM_CONFIG_SERVER="Server" COM_CONFIG_SERVER_SETTINGS="Server Settings" diff --git a/media/system/js/sendtestmail.js b/media/system/js/sendtestmail.js new file mode 100644 index 0000000000000..de473ad714b35 --- /dev/null +++ b/media/system/js/sendtestmail.js @@ -0,0 +1,74 @@ +/** + * @package Joomla.JavaScript + * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +/** + * Calls the sending process of the config class + */ +jQuery(document).ready(function ($) +{ + $('#sendtestmail').click(function () + { + var email_data = { + smtpauth : $('input[name="jform[smtpauth]"]').val(), + smtpuser : $('input[name="jform[smtpuser]"]').val(), + smtppass : $('input[name="jform[smtppass]"]').val(), + smtphost : $('input[name="jform[smtphost]"]').val(), + smtpsecure: $('select[name="jform[smtpsecure]"]').val(), + smtpport : $('input[name="jform[smtpport]"]').val(), + mailfrom : $('input[name="jform[mailfrom]"]').val(), + fromname : $('input[name="jform[fromname]"]').val(), + mailer : $('select[name="jform[mailer]"]').val(), + mailonline: $('input[name="jform[mailonline]"]:checked').val() + }; + + $.ajax({ + url: sendtestmail_url, + data: email_data + }) + + .done(function (response) + { + var data_response = $.parseJSON(response); + var msg = {}; + + if (data_response.data) + { + if (typeof data_response.messages == 'object') + { + if (typeof data_response.messages.success != 'undefined' && data_response.messages.success.length > 0) + { + msg.success = [data_response.messages.success]; + } + } + + } + else + { + if (typeof data_response.messages == 'object') + { + if (typeof data_response.messages.error != 'undefined' && data_response.messages.error.length > 0) + { + msg.error = [data_response.messages.error]; + } + + if (typeof data_response.messages.notice != 'undefined' && data_response.messages.notice.length > 0) + { + msg.notice = [data_response.messages.notice]; + } + + if (typeof data_response.messages.message != 'undefined' && data_response.messages.message.length > 0) + { + msg.message = [data_response.messages.message]; + } + } + } + + Joomla.renderMessages(msg); + }); + + window.scrollTo(0, 0); + }); +});