Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.phar
/vendor/
composer.lock
.idea/
37 changes: 16 additions & 21 deletions SwiftMailer/MailjetTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mailjet\MailjetSwiftMailer\SwiftMailer;

use Mailjet\Response;
use Mailjet\Client;
use \Swift_Events_EventDispatcher;
use \Swift_Events_EventListener;
use \Swift_Events_SendEvent;
Expand Down Expand Up @@ -30,7 +32,7 @@ class MailjetTransport implements Swift_Transport {
protected $mailjetClient = null;

/**
* @var Mailjet\MailjetSwiftMailer\SwiftMailer\MessageFormat\MessageFormatStrategyInterface
* @var \Mailjet\MailjetSwiftMailer\SwiftMailer\MessageFormat\MessageFormatStrategyInterface
*/
public $messageFormat;

Expand Down Expand Up @@ -62,14 +64,16 @@ class MailjetTransport implements Swift_Transport {
protected $clientOptions;

/**
* @var array|null
* @var array|null|Response
*/
protected $resultApi;

/**
* MailjetTransport constructor.
* @param Swift_Events_EventDispatcher $eventDispatcher
* @param string $apiKey
* @param string $apiSecret
* @param bool $call
* @param array $clientOptions
*/
public function __construct(Swift_Events_EventDispatcher $eventDispatcher, $apiKey = null, $apiSecret = null, $call = true, array $clientOptions = []) {
Expand Down Expand Up @@ -112,6 +116,7 @@ public function ping() {
* @param Swift_Mime_Message $message
* @param null $failedRecipients
* @return int Number of messages sent
* @throws \Swift_TransportException
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
$this->resultApi = null;
Expand All @@ -122,7 +127,6 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
return 0;
}
}
$sendCount = 0;

// extract Mailjet Message from SwiftMailer Message
$mailjetMessage = $this->messageFormat->getMailjetMessage($message);
Expand Down Expand Up @@ -158,16 +162,16 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
}

/**
* @param array $message (of Swift_Mime_Message)
* @param array $messages (of Swift_Mime_Message)
* @param null $failedRecipients
* @return int Number of messages sent
* @throws \Swift_TransportException
*/
public function bulkSend(array $messages, &$failedRecipients = null) {

$this->resultApi = null;
$failedRecipients = (array) $failedRecipients;
$bulkContainer = ['Messages' => []];
$sendCount = 0;
foreach ($messages as $message) {
// extract Mailjet Message from SwiftMailer Message
$mailjetMessage = $this->messageFormat->getMailjetMessage($message);
Expand All @@ -185,23 +189,13 @@ public function bulkSend(array $messages, &$failedRecipients = null) {
}
}
// Create mailjetClient
$mailjetClient = $this->createMailjetClient();

$this->createMailjetClient();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has no effect at the moment. We create new client instance and throw it into space.

I suggest we move the creation of the client before the foreach loop and keep the is_null condition.

// Create mailjetClient
if (is_null($this->mailjetClient)) {
    $this->mailjetClient = $this->createMailjetClient();
}
foreach ($messages as $message) {
    // ...
}

This will have performance gain as well.

try {
// send API call
$this->resultApi = $this->mailjetClient->post(Resources::$Email, ['body' => $bulkContainer]);

$sendCount = $this->findNumberOfSentMails();
// get result
if ($this->resultApi->success()) {
$resultStatus = Swift_Events_SendEvent::RESULT_SUCCESS;
} else {
$resultStatus = Swift_Events_SendEvent::RESULT_FAILED;
}
} catch (\Exception $e) {
//$failedRecipients = $mailjetMessage['Recipients'];
$sendCount = 0;
$resultStatus = Swift_Events_SendEvent::RESULT_FAILED;
}

return $sendCount;
Expand Down Expand Up @@ -250,18 +244,19 @@ protected function createMailjetClient() {
}

if (isset($this->clientOptions)) {
return new \Mailjet\Client($this->apiKey, $this->apiSecret, $this->call, $this->clientOptions);
return new Client($this->apiKey, $this->apiSecret, $this->call, $this->clientOptions);
}

return new \Mailjet\Client($this->apiKey, $this->apiSecret, $this->call);
return new Client($this->apiKey, $this->apiSecret, $this->call);
}

/**
* Inject an external Mailjet\Client
* @method setExternalMailjetClient
* @param \Mailjet\Client $client
* @param \Mailjet\Client $client
* @return \Mailjet\Client
*/
public function setExternalMailjetClient(\Mailjet\Client $client)
public function setExternalMailjetClient(Client $client)
{
$this->mailjetClient = $client;
return $this->mailjetClient;
Expand Down Expand Up @@ -348,7 +343,7 @@ public function getClientOptions() {
}

/**
* @return null|array
* @return array|Response|null
*/
public function getResultApi() {
return $this->resultApi;
Expand Down