Skip to content

Commit

Permalink
Merge pull request #9685 from andrepereiradasilva/patch-6
Browse files Browse the repository at this point in the history
[Send Test Mail] Use json MimeType, send data via POST method and catch json response errors
  • Loading branch information
rdeutz committed Apr 12, 2016
2 parents c97a2b7 + f56e8c8 commit d5a0c4b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public function execute()
$this->app->redirect('index.php');
}

$this->app->mimeType = 'application/json';
$this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
$this->app->sendHeaders();

$model = new ConfigModelApplication;
echo new JResponseJson($model->sendTestMail());
JFactory::getApplication()->close();
$this->app->close();
}
}
7 changes: 7 additions & 0 deletions administrator/components/com_config/view/application/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public function render()

$this->userIsSuperAdmin = $user->authorise('core.admin');

// Add strings for translations in Javascript.
JText::script('COM_CONFIG_SENDMAIL_JS_ERROR_CONNECTION_ABORT');
JText::script('COM_CONFIG_SENDMAIL_JS_ERROR_NO_CONTENT');
JText::script('COM_CONFIG_SENDMAIL_JS_ERROR_OTHER');
JText::script('COM_CONFIG_SENDMAIL_JS_ERROR_PARSE');
JText::script('COM_CONFIG_SENDMAIL_JS_ERROR_TIMEOUT');

$this->addToolbar();

return parent::render();
Expand Down
5 changes: 5 additions & 0 deletions administrator/language/en-GB/en-GB.com_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ 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_JS_ERROR_CONNECTION_ABORT="A connection abort has occured while fetching the JSON data."
COM_CONFIG_SENDMAIL_JS_ERROR_NO_CONTENT="No content was returned."
COM_CONFIG_SENDMAIL_JS_ERROR_OTHER="An error has occured while fetching the JSON data: HTTP %s status code."
COM_CONFIG_SENDMAIL_JS_ERROR_PARSE="A parse error has occured while processing the following JSON data:<br/><code style="_QQ_"color:inherit;white-space:pre;padding:0;margin:0;border:0;background:inherit;"_QQ_">%s</code>"
COM_CONFIG_SENDMAIL_JS_ERROR_TIMEOUT="A timeout has occured while fetching the JSON data."
COM_CONFIG_SENDMAIL_METHOD_MAIL="PHP Mail"
COM_CONFIG_SENDMAIL_METHOD_SENDMAIL="Sendmail"
COM_CONFIG_SENDMAIL_METHOD_SMTP="SMTP"
Expand Down
70 changes: 52 additions & 18 deletions media/system/js/sendtestmail-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,77 @@ jQuery(document).ready(function ($)
};

$.ajax({
url: sendtestmail_url,
data: email_data
})
method: "POST",
url: sendtestmail_url,
data: email_data,
dataType: "json"
})
.fail(function (jqXHR, textStatus, error) {
var msg = {};

if (textStatus == 'parsererror')
{
// Html entity encode.
var encodedJson = jqXHR.responseText.trim();

var buf = [];
for (var i = encodedJson.length-1; i >= 0; i--) {
buf.unshift( [ '&#', encodedJson[i].charCodeAt(), ';' ].join('') );
}

.done(function (response)
{
var data_response = $.parseJSON(response);
encodedJson = buf.join('');

msg.error = [ Joomla.JText._('COM_CONFIG_SENDMAIL_JS_ERROR_PARSE').replace('%s', encodedJson) ];
}
else if (textStatus == 'nocontent')
{
msg.error = [ Joomla.JText._('COM_CONFIG_SENDMAIL_JS_ERROR_NO_CONTENT') ];
}
else if (textStatus == 'timeout')
{
msg.error = [ Joomla.JText._('COM_CONFIG_SENDMAIL_JS_ERROR_TIMEOUT') ];
}
else if (textStatus == 'abort')
{
msg.error = [ Joomla.JText._('COM_CONFIG_SENDMAIL_JS_ERROR_CONNECTION_ABORT') ];
}
else
{
msg.error = [ Joomla.JText._('COM_CONFIG_SENDMAIL_JS_ERROR_OTHER').replace('%s', jqXHR.status) ];
}

Joomla.renderMessages(msg);
})
.done(function (response) {
var msg = {};

if (data_response.data)
if (response.data)
{
if (typeof data_response.messages == 'object')
if (typeof response.messages == 'object')
{
if (typeof data_response.messages.success != 'undefined' && data_response.messages.success.length > 0)
if (typeof response.messages.success != 'undefined' && response.messages.success.length > 0)
{
msg.success = [data_response.messages.success];
msg.success = [response.messages.success];
}
}

}
else
{
if (typeof data_response.messages == 'object')
if (typeof response.messages == 'object')
{
if (typeof data_response.messages.error != 'undefined' && data_response.messages.error.length > 0)
if (typeof response.messages.error != 'undefined' && response.messages.error.length > 0)
{
msg.error = [data_response.messages.error];
msg.error = [response.messages.error];
}

if (typeof data_response.messages.notice != 'undefined' && data_response.messages.notice.length > 0)
if (typeof response.messages.notice != 'undefined' && response.messages.notice.length > 0)
{
msg.notice = [data_response.messages.notice];
msg.notice = [response.messages.notice];
}

if (typeof data_response.messages.message != 'undefined' && data_response.messages.message.length > 0)
if (typeof response.messages.message != 'undefined' && response.messages.message.length > 0)
{
msg.message = [data_response.messages.message];
msg.message = [response.messages.message];
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions media/system/js/sendtestmail.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5a0c4b

Please sign in to comment.