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

Mtc 6845 verfiy custom name, from address & reply to address #14

Merged
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
2 changes: 1 addition & 1 deletion Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
return [
'name' => 'Mailjet Adapter by Leuchtfeuer',
'description' => 'Plugin allows sending emails with Mailjet in batches via API and callback handling used for bounce management.',
'version' => '1.0.8',
'version' => '1.0.9',
'author' => 'Leuchtfeuer Digital Marketing GmbH',
];
8 changes: 6 additions & 2 deletions Mailer/Factory/MailjetTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Factory;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetApiTransport;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetSmtpTransport;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetTransportCallback;
Expand All @@ -21,7 +23,9 @@ public function __construct(
private MailjetTransportCallback $transportCallback,
EventDispatcherInterface $eventDispatcher,
HttpClientInterface $client = null,
LoggerInterface $logger = null
LoggerInterface $logger = null,
protected CoreParametersHelper $coreParametersHelper,
protected EntityManager $em,
) {
parent::__construct($eventDispatcher, $client, $logger);
}
Expand All @@ -48,7 +52,7 @@ public function create(Dsn $dsn): TransportInterface
}

if (MailjetApiTransport::SCHEME === $dsn->getScheme() && $user && $password) {
return new MailjetApiTransport($user, $password, $sandbox, $this->transportCallback, $this->client, $this->dispatcher, $this->logger);
return new MailjetApiTransport($user, $password, $sandbox, $this->transportCallback, $this->client, $this->dispatcher, $this->logger, $this->coreParametersHelper, $this->em);
}

throw new UnsupportedSchemeException($dsn, 'mailjet', $this->getSupportedSchemes());
Expand Down
57 changes: 54 additions & 3 deletions Mailer/Transport/MailjetApiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\EmailBundle\Mailer\Message\MauticMessage;
use Mautic\EmailBundle\Mailer\Transport\TokenTransportInterface;
use Mautic\EmailBundle\Mailer\Transport\TokenTransportTrait;
Expand Down Expand Up @@ -48,7 +50,9 @@ public function __construct(
private MailjetTransportCallback $callback,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null,
LoggerInterface $logger = null
LoggerInterface $logger = null,
private CoreParametersHelper $coreParametersHelper,
private EntityManager $em,
) {
parent::__construct($client, $dispatcher, $logger);

Expand Down Expand Up @@ -151,7 +155,7 @@ public function preparePayloadFromMetadata(array $metadata, MauticMessage $email
$attachments = $this->prepareAttachments($email);
$newTokens = $this->prepareTokenFromLeadMetadata($email, $leadData);
$emailData = [
'From' => $this->formatAddress($envelope->getSender()),
'From' => $this->formatAddress($this->getEmailFrom($email, $envelope)),
'To' => $to,
'Subject' => $email->getSubject(),
'Attachments' => $attachments,
Expand All @@ -172,7 +176,7 @@ public function preparePayloadFromMetadata(array $metadata, MauticMessage $email
$emailData['Bcc'] = $this->formatAddresses($emails);
}

if ($emails = $email->getReplyTo()) {
if ($emails = $this->getReplyTo($email)) {
if (1 < $length = \count($emails)) {
throw new TransportException(sprintf('Mailjet\'s API only supports one Reply-To email, %d given.', $length));
}
Expand Down Expand Up @@ -254,6 +258,53 @@ private function formatAddress(Address $address): array
];
}

private function getEmailFrom(Email $email, Envelope $envelope): ?Address
{

$metadata = $email->getMetadata();
$metadata = reset($metadata);
$entityEmailFrom = '';
$entityNameFrom = '';
if(isset($metadata['emailId']) && !empty($metadata['emailId'])){
$emailEntity = $this->em->getRepository(\Mautic\EmailBundle\Entity\Email::class)->find($metadata['emailId']);
$entityEmailFrom = $emailEntity->getFromAddress();
$entityNameFrom = $emailEntity->getFromName();
}

$address = $envelope->getSender();
if (empty($entityEmailFrom)) {
$entityEmailFrom = $address->getAddress();
}

if (empty($entityNameFrom)) {
$entityNameFrom = $address->getName();
}

return new Address($entityEmailFrom, $entityNameFrom);

}

/**
* @return array<int, string>
*/
private function getReplyTo(Email $email): array
{
$metadata = $email->getMetadata();
$metadata = reset($metadata);
if(isset($metadata['emailId']) && !empty($metadata['emailId'])){
$emailEntity = $this->em->getRepository(\Mautic\EmailBundle\Entity\Email::class)->find($metadata['emailId']);
$entityReplyTo = $emailEntity->getReplyToAddress();
if (!empty($entityReplyTo)) {
$entityReplyTo = explode(',', $entityReplyTo);
foreach ($entityReplyTo as $key => $value) {
$entityReplyTo[$key] = new Address($value);
}
return $entityReplyTo;
}
}
return $email->getReplyTo();
}

/**
* @param Address[] $addresses
*
Expand Down
2 changes: 0 additions & 2 deletions Tests/Functional/Mailer/Transport/MailjetApiTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function testEmailDnsFieldValues(): void
$fields = [
'config[emailconfig][mailer_dsn][scheme]' => MailjetApiTransport::SCHEME,
'config[emailconfig][mailer_dsn][host]' => 'default',
'config[emailconfig][mailer_dsn][options][list][0][label]' => 'sandbox',
'config[emailconfig][mailer_dsn][options][list][0][value]' => 'true',
];
$form = $crawler->selectButton('config[buttons][save]')->form();

Expand Down
13 changes: 12 additions & 1 deletion Tests/Unit/Mailer/Factory/MailjetTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Tests\Unit\Mailer\Factory;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Factory\MailjetTransportFactory;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetApiTransport;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetSmtpTransport;
Expand All @@ -27,8 +29,17 @@ protected function setUp(): void
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
$httpClientMock = $this->createMock(HttpClientInterface::class);
$loggerMock = $this->createMock(LoggerInterface::class);
$coreParameterHelper = $this->createMock(CoreParametersHelper::class);
$entityManager = $this->createMock(EntityManager::class);

$this->mailjetTransportFactory = new MailjetTransportFactory($transportCallbackMock, $eventDispatcherMock, $httpClientMock, $loggerMock);
$this->mailjetTransportFactory = new MailjetTransportFactory(
$transportCallbackMock,
$eventDispatcherMock,
$httpClientMock,
$loggerMock,
$coreParameterHelper,
$entityManager
);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion Tests/Unit/Mailer/Transport/MailjetApiTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Tests\Unit\Mailer\Transport;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\EmailBundle\Mailer\Message\MauticMessage;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetApiTransport;
use MauticPlugin\LeuchtfeuerMailjetAdapterBundle\Mailer\Transport\MailjetTransportCallback;
Expand Down Expand Up @@ -38,6 +40,8 @@ protected function setUp(): void
$transportCallbackMock = $this->createMock(MailjetTransportCallback::class);
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
$loggerMock = $this->createMock(LoggerInterface::class);
$coreParameterHelper = $this->createMock(CoreParametersHelper::class);
$entityManager = $this->createMock(EntityManager::class);

$this->transport = new MailjetApiTransport(
'user',
Expand All @@ -46,7 +50,9 @@ protected function setUp(): void
$transportCallbackMock,
$this->httpClientMock,
$eventDispatcherMock,
$loggerMock
$loggerMock,
$coreParameterHelper,
$entityManager
);
}

Expand Down