Skip to content

Commit

Permalink
Merge pull request joomla#8643 from Kubik-Rubik/feature-sendtestmail
Browse files Browse the repository at this point in the history
Feature - Send Test Mail
  • Loading branch information
wilsonge committed Jan 20, 2016
2 parents 0eed608 + bf5ba5e commit 5d4a0ae
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_config
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;

/**
* Send Test Mail Controller from global configuration
*
* @since 3.6
*/
class ConfigControllerApplicationSendtestmail extends JControllerBase
{
/**
* Method to send the test mail.
*
* @return string
*
* @since 3.6
*/
public function execute()
{
if (!JSession::checkToken('get'))
{
$this->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();
}
}
42 changes: 42 additions & 0 deletions administrator/components/com_config/model/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<button type="button" class="btn btn-small" id="sendtestmail">
<span>' . JText::_('COM_CONFIG_SENDMAIL_ACTION_BUTTON') . '</span>
</button>';
8 changes: 8 additions & 0 deletions administrator/language/en-GB/en-GB.com_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>%s</strong> using <strong>%s</strong>. 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"
Expand Down
74 changes: 74 additions & 0 deletions media/system/js/sendtestmail.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 5d4a0ae

Please sign in to comment.