Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laminas module version #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mailjet\Mailjet\Helper;

use Mailjet\Mailjet\Helper\MailjetAPI as MailjetAPI;
use Laminas\Http\Client\Adapter\Socket;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -258,9 +259,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
private $encryptor;

/**
* @var \Zend\Http\Client\Adapter\Socket
* @var Socket
*/
private $zendHttpClient;
private $httpClient;

/**
* Data constructor.
Expand All @@ -270,21 +271,21 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\Encryption\Encryptor $encryptor
* @param \Zend\Http\Client\Adapter\Socket $zendHttpClient
* @param Socket $httpClient
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ConfigResource\ConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Encryption\Encryptor $encryptor,
\Zend\Http\Client\Adapter\Socket $zendHttpClient
Socket $httpClient
) {
$this->config = $config;
$this->cacheTypeList = $cacheTypeList;
$this->urlBuilder = $urlBuilder;
$this->encryptor = $encryptor;
$this->zendHttpClient = $zendHttpClient;
$this->httpClient = $httpClient;

parent::__construct($context);
}
Expand Down Expand Up @@ -387,12 +388,12 @@ public function getEventStatus($config, $event)
public function testSmtpConnection($port, $ssl)
{
try {
$this->zendHttpClient->setOptions(['ssltransport' => $ssl, 'timeout' => 5]);
$this->httpClient->setOptions(['ssltransport' => $ssl, 'timeout' => 5]);

if ($ssl == 'tls') {
$this->zendHttpClient->connect(\Mailjet\Mailjet\Helper\Data::SMTP_HOST, $port);
$this->httpClient->connect(\Mailjet\Mailjet\Helper\Data::SMTP_HOST, $port);
} else {
$this->zendHttpClient->connect('ssl://' . \Mailjet\Mailjet\Helper\Data::SMTP_HOST, $port);
$this->httpClient->connect('ssl://' . \Mailjet\Mailjet\Helper\Data::SMTP_HOST, $port);
}

return true;
Expand Down
61 changes: 27 additions & 34 deletions Model/Framework/Mail/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,50 @@

namespace Mailjet\Mailjet\Model\Framework\Mail;

use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\EmailMessageInterface;
use Magento\Framework\Phrase;
use Mailjet\Mailjet\Helper\Data;
use Laminas\Mail\Message;
use Laminas\Mail\Transport\Smtp;
use Laminas\Mail\Transport\SmtpOptions;

class Transport
{
/**
* @var \Mailjet\Mailjet\Helper\Data
* @var Data
*/
protected $dataHelper;

/**
* @var \Magento\Framework\Escaper
*/
protected $escaper;

/**
* Transport model construct
*
* @param \Mailjet\Mailjet\Helper\Data $dataHelper
* @param \Magento\Framework\Escaper $escaper
* @param Data $dataHelper
*/
public function __construct(
\Mailjet\Mailjet\Helper\Data $dataHelper,
\Magento\Framework\Escaper $escaper
Data $dataHelper
) {
$this->dataHelper = $dataHelper;
$this->escaper = $escaper;
}

/**
* Send smtp message
*
* @param \Magento\Framework\Mail\EmailMessageInterface|\Zend\Mail\Message $message
* @param EmailMessageInterface|Message $message
* @param array $config
*
* @return bool
*
* @throws MailException
*/
public function sendSmtpMessage($message, $config)
{
if (!($message instanceof \Zend\Mail\Message)) {
$message = \Zend\Mail\Message::fromString($message->getRawMessage());
if (!($message instanceof Message)) {
$message = Message::fromString($message->getRawMessage());
}

//set config
$options = new \Zend\Mail\Transport\SmtpOptions(
$options = new SmtpOptions(
[
// 'name' => '',
'host' => $config['host'],
Expand All @@ -52,31 +55,21 @@ public function sendSmtpMessage($message, $config)

$options->setConnectionClass('login');

$connectionConfig = [
'username' => $config['username'],
'password' => $config['password'],
'ssl' => $config['ssl'],
];

$options->setConnectionConfig($connectionConfig);
$options->setConnectionConfig(
[
'username' => $config['username'],
'password' => $config['password'],
'ssl' => $config['ssl'],
]
);

try {
$transport = new \Zend\Mail\Transport\Smtp();
$transport = new Smtp();
$transport->setOptions($options);
$transport->send($message);
$toArr = [];

foreach ($message->getTo() as $toAddr) {
$toArr[] = $toAddr->getEmail();
}

$data['recipient'] = implode(',', $toArr);
$data['sender'] = $message->getFrom()->rewind()->getEmail();
$data['subject'] = $message->getSubject();
$data['body'] = $this->escaper->escapeHtml($message->toString());

return true;
} catch (Exception $e) {
} catch (\Exception $e) {
throw new MailException(
new Phrase($e->getMessage()),
$e
Expand Down
14 changes: 7 additions & 7 deletions Plugin/Email/Model/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Magento\Store\Model\ScopeInterface;
use Mailjet\Mailjet\Helper\Data;
use Magento\Email\Model\Transport as ModelTransport;
use Laminas\Mail\Message;

class Transport
{
Expand Down Expand Up @@ -72,19 +73,18 @@ public function aroundSendMessage(\Magento\Email\Model\Transport $subject, calla
);

try {

$zendMessage = \Zend\Mail\Message::fromString(
$messageObject = Message::fromString(
$subject->getMessage()->getRawMessage()
)->setEncoding('utf-8');
if (2 === $isSetReturnPath && $returnPathValue) {
$zendMessage->setSender($returnPathValue);
} elseif (1 === $isSetReturnPath && $zendMessage->getFrom()->count()) {
$fromAddressList = $zendMessage->getFrom();
$messageObject->setSender($returnPathValue);
} elseif (1 === $isSetReturnPath && $messageObject->getFrom()->count()) {
$fromAddressList = $messageObject->getFrom();
$fromAddressList->rewind();
$zendMessage->setSender($fromAddressList->current()->getEmail());
$messageObject->setSender($fromAddressList->current()->getEmail());
}

$smtp->sendSmtpMessage($zendMessage, $config);
$smtp->sendSmtpMessage($messageObject, $config);
} catch (\Exception $e) {
throw new MailException(new \Magento\Framework\Phrase($e->getMessage()), $e);
}
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"type": "magento2-module",
"version": "1.0.1",
"require": {
"mailjet/mailjet-apiv3-php": "*"
"mailjet/mailjet-apiv3-php": "*",
"laminas/laminas-http": "*",
"laminas/laminas-mail": "*"
}
}