Skip to content

Commit

Permalink
Fix email encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Mar 20, 2020
1 parent eb2f2e9 commit f08ac06
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* "Reset to Defaults" button renderer
*
*/
class TestButton extends Field
class ValidateConfigButton extends Field
{
/** @var UrlInterface */
protected $_urlBuilder;
Expand All @@ -42,7 +42,7 @@ public function __construct(
protected function _construct()
{
parent::_construct();
$this->setTemplate('MagePal_GmailSmtpApp::system/config/testbutton.phtml');
$this->setTemplate('MagePal_GmailSmtpApp::system/config/validateConfigButton.phtml');
}

/**
Expand All @@ -58,8 +58,7 @@ public function getButtonHtml()
)->setData(
[
'id' => 'gmailsmtpapp_debug_result_button',
'label' => __('Send Test Email'),
'onclick' => 'javascript:gmailSmtpAppDebugTest(); return false;',
'label' => __('Send Test Email')
]
);

Expand All @@ -69,7 +68,7 @@ public function getButtonHtml()
public function getAdminUrl()
{
return $this->_urlBuilder->getUrl(
'magepalsmtp/testemail',
'magepalsmtp/validateconfig',
['store' => $this->_request->getParam('store')]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Class EmailTest
* @package MagePal\GmailSmtpApp\Block\Adminhtml
*/
class EmailTest extends Template
class ValidateConfig extends Template
{
/**
* @var Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* http://www.magepal.com | support@magepal.com
*/

namespace MagePal\GmailSmtpApp\Controller\Adminhtml\Testemail;
namespace MagePal\GmailSmtpApp\Controller\Adminhtml\Validateconfig;

use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
Expand Down
7 changes: 4 additions & 3 deletions Model/TwoDotThree/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ public function setStoreModel(Store $storeModel)
* @param EmailMessageInterface $message
* @throws MailException
*/
public function sendSmtpMessage(
EmailMessageInterface $message
) {
public function sendSmtpMessage(EmailMessageInterface $message)
{
$dataHelper = $this->dataHelper;
$dataHelper->setStoreId($this->storeModel->getStoreId());

$encoding = $message->getEncoding();
$message = Message::fromString($message->getRawMessage());
$message->getHeaders()->get('to')->setEncoding('utf-8');
$message->getHeaders()->get('reply-to')->setEncoding('utf-8');
$message->getHeaders()->get('from')->setEncoding('utf-8');
$message->getHeaders()->setEncoding('utf-8');
$message->setEncoding($encoding);

Expand Down
2 changes: 2 additions & 0 deletions Model/ZendMailTwo/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function sendSmtpMessage(
* Temporary fix since 2.2.x will be deprecated shortly and 2.3.3 uses new logic.
*/
$message = Message::fromString($message->toString());
$message->setEncoding('utf-8');

//Set reply-to path
switch ($dataHelper->getConfigSetReturnPath()) {
Expand Down Expand Up @@ -176,6 +177,7 @@ public function sendSmtpMessage(
$message->getHeaders()->get('to')->setEncoding('utf-8');
$message->getHeaders()->get('reply-to')->setEncoding('utf-8');
$message->getHeaders()->get('from')->setEncoding('utf-8');
$message->getHeaders()->setEncoding('utf-8');
$transport->send($message);
} catch (Exception $e) {
throw new MailException(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*"
},
"type": "magento2-module",
"version": "2.7.0",
"version": "2.7.1",
"license": [
"proprietary"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
<frontend_class>validate-email</frontend_class>
</field>
<field id="button" translate="label comment" type="button" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>MagePal\GmailSmtpApp\Block\Adminhtml\System\Config\TestButton</frontend_model>
<frontend_model>MagePal\GmailSmtpApp\Block\Adminhtml\System\Config\ValidateConfigButton</frontend_model>
</field>
</group>
</group>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root">
<block class="MagePal\GmailSmtpApp\Block\Adminhtml\EmailTest" name="magepal.smtp_test"
template="MagePal_GmailSmtpApp::emailtest/email.phtml" />
<block class="MagePal\GmailSmtpApp\Block\Adminhtml\ValidateConfig" name="magepal.smtp_validate"
template="MagePal_GmailSmtpApp::validateConfig/result.phtml" />
</container>
</layout>
</layout>
7 changes: 7 additions & 0 deletions view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var config = {
map: {
'*': {
magePalGmailSmtpAppValidateConfig: 'MagePal_GmailSmtpApp/js/validate-config'
}
}
};
59 changes: 0 additions & 59 deletions view/adminhtml/templates/system/config/testbutton.phtml

This file was deleted.

20 changes: 20 additions & 0 deletions view/adminhtml/templates/system/config/validateConfigButton.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/
?>

<?php /** @var $block MagePal\GmailSmtpApp\Block\Adminhtml\System\Config\ValidateConfigButton **/ ?>
<?= $block->getButtonHtml() ?>

<script type="text/x-magento-init">
{
"#gmailsmtpapp_debug_result_button": {
"magePalGmailSmtpAppValidateConfig": {
"postUrl":"<?= $block->getAdminUrl() ?>"
}
}
}
</script>
59 changes: 59 additions & 0 deletions view/adminhtml/web/js/validate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/

define([
'jquery',
'Magento_Ui/js/modal/alert'
], function ($, alert) {

var formSubmit = function (config) {
var postData = {
form_key: FORM_KEY
};

/** global var configForm **/
configForm.find('[id^=system_gmailsmtpapp]').find(':input').serializeArray().map(function (field) {
var name = field.name.match(/groups\[gmailsmtpapp\]?(\[groups\]\[debug\])?\[fields\]\[(.*)\]\[value]/);

/**
* groups[gmailsmtpapp][groups][debug][fields][email][value]
* groups[gmailsmtpapp][fields][password][value]
*/

if (name && name.length === 3) {
postData[name[2]] = field.value;
}
});

$.ajax({
url: config.postUrl,
type: 'post',
dataType: 'html',
data: postData,
showLoader: true
}).done(function (response) {
if (typeof response === 'object') {
if (response.error) {
alert({ title: 'Error', content: response.message });
} else if (response.ajaxExpired) {
window.location.href = response.ajaxRedirect;
}
} else {
alert({
title:'',
content:response,
buttons: []
});
}
});
};

return function (config) {
$('#gmailsmtpapp_debug_result_button').on('click', function () {
formSubmit(config);
});
}
});

0 comments on commit f08ac06

Please sign in to comment.