Skip to content

Commit

Permalink
Added "send at" and "ip pool" feature (#40)
Browse files Browse the repository at this point in the history
* fixed "RequestException"
  • Loading branch information
simialbi authored Dec 13, 2021
1 parent bb25be9 commit 74844c1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 54 deletions.
27 changes: 16 additions & 11 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,26 @@ protected function sendMessage($message): bool
self::LOG_CATEGORY
);

$config = [
'message' => $message->getMandrillMessageArray(),
'async' => $message->isAsync()
];
if ($message->getIpPool()) {
$config['ip_pool'] = $message->getIpPool();
}
if ($message->getSendAt()) {
$config['send_at'] = $message->getSendAt();
}

if ($this->useMandrillTemplates) {
$config['template_name'] = $message->getTemplateName();
$config['template_content'] = $message->getTemplateContent();
return $this->wasMessageSentSuccessful(
$this->_mailchimp->messages->sendTemplate([
'template_name' => $message->getTemplateName(),
'template_content' => $message->getTemplateContent(),
'message' => $message->getMandrillMessageArray(),
'async' => $message->isAsync()
])
$this->_mailchimp->messages->sendTemplate($config)
);
} else {
return $this->wasMessageSentSuccessful(
$this->_mailchimp->messages->send([
'message' => $message->getMandrillMessageArray(),
'async' => $message->isAsync()
])
$this->_mailchimp->messages->send($config)
);
}
}
Expand All @@ -237,7 +242,7 @@ private function wasMessageSentSuccessful($mandrillResponse): bool
self::LOG_CATEGORY
);

return false;
$mandrillResponse = "\r\n\r\n" . preg_replace('#.*\s*(\{.*\}).*#', '$1', $mandrillResponse->getMessage());
}
$this->_mandrillResponse = $mandrillResponse;
if (is_string($this->_mandrillResponse)) {
Expand Down
151 changes: 108 additions & 43 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ class Message extends BaseMessage
*/
private $_async = false;

/**
* The name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs,
* this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.
*
* @var string
*/
private $_ipPool;

/**
* When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the
* past, the message will be sent immediately.
*
* @var string
*/
private $_sendAt;

/**
* The name of the template inside mandrill.
*
Expand Down Expand Up @@ -392,6 +408,55 @@ public function disableAsync(): Message
return $this;
}

/**
* The name of the dedicated ip pool that should is set to be used to send the message.
*
* @return string|null
*/
public function getIpPool()
{
return $this->_ipPool;
}

/**
* The name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs,
* this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.
*
* @param string $ipPool Ip pool name
* @return static
*/
public function setIpPool(string $ipPool): Message
{
$this->_ipPool = $ipPool;

return $this;
}

/**
* When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format.
*
* @return string|null
*/
public function getSendAt()
{
return $this->_sendAt;
}

/**
* When this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the
* past, the message will be sent immediately.
*
* @param string $sendAt The date in YYYY-MM-DD HH:MM:SS format
*
* @return static
*/
public function setSendAt(string $sendAt): Message
{
$this->_sendAt = $sendAt;

return $this;
}

/**
* Returns the from email address in this format:
*
Expand Down Expand Up @@ -860,15 +925,16 @@ public function embedContent($content, array $options = []): Message
*/
public function setTemplateData(
string $templateName,
array $templateContent = [],
array $templateContent = [],
string $templateLanguage = self::LANGUAGE_MAILCHIMP
): Message {
): Message
{
$this->_templateName = $templateName;

// if ($templateLanguage === self::LANGUAGE_MAILCHIMP) {
$this->_templateContent = $this->convertParamsForTemplate($templateContent);
$this->_templateContent = $this->convertParamsForTemplate($templateContent);
// if ($templateLanguage === self::LANGUAGE_HANDLEBARS) {
$this->setGlobalMergeVars($templateContent);
$this->setGlobalMergeVars($templateContent);
// }

$this->_mergeLanguage = $templateLanguage;
Expand Down Expand Up @@ -924,6 +990,15 @@ public function disableTemplateDefaults(): Message
return $this;
}

/**
* @return string|null
* @since 1.7.0
*/
public function getSubaccount()
{
return $this->_subaccount;
}

/**
* @param string $subaccount
*
Expand All @@ -937,15 +1012,6 @@ public function setSubaccount(string $subaccount): Message
return $this;
}

/**
* @return string|null
* @since 1.7.0
*/
public function getSubaccount()
{
return $this->_subaccount;
}

/**
* Make the message important.
*
Expand Down Expand Up @@ -1069,16 +1135,6 @@ public function getGlobalMergeVars(): array
return $this->_globalMergeVars;
}

/**
* Returns the merge vars that will be submitted to mandrill.
*
* @return array
*/
public function getMergeVars(): array
{
return $this->_mergeVars;
}

/**
* Adds the given merge vars to the global merge vars array.
* Merge vars are case insensitive and cannot start with _
Expand All @@ -1104,6 +1160,15 @@ public function setGlobalMergeVars(array $mergeVars): Message
return $this;
}

/**
* Returns the merge vars that will be submitted to mandrill.
*
* @return array
*/
public function getMergeVars(): array
{
return $this->_mergeVars;
}

/**
* Adds the given merge vars to the merge vars array.
Expand Down Expand Up @@ -1136,16 +1201,6 @@ public function getMetadata(): array
return $this->_metadata;
}

/**
* Returns the per recipient meta data that will be submitted to mandrill.
*
* @return array
*/
public function getRecipientMetadata(): array
{
return $this->_recipientMetadata;
}

/**
* Adds the given meta data to the global meta data array
*
Expand All @@ -1167,6 +1222,16 @@ public function setMetadata(array $metadata): Message
return $this;
}

/**
* Returns the per recipient meta data that will be submitted to mandrill.
*
* @return array
*/
public function getRecipientMetadata(): array
{
return $this->_recipientMetadata;
}

/**
* Adds the given meta data to the per recipient meta data array
*
Expand Down Expand Up @@ -1209,16 +1274,6 @@ public function getGoogleAnalyticsDomains(): array
return $this->_googleAnalyticsDomains;
}

/**
* Returns the Google Analytics campaign that will be submitted to mandrill.
*
* @return string
*/
public function getGoogleAnalyticsCampaign(): string
{
return $this->_googleAnalyticsCampaign;
}

/**
* Sets the Google Analytics domains that will be submitted to mandrill.
*
Expand All @@ -1233,6 +1288,16 @@ public function setGoogleAnalyticsDomains(array $domains): Message
return $this;
}

/**
* Returns the Google Analytics campaign that will be submitted to mandrill.
*
* @return string
*/
public function getGoogleAnalyticsCampaign(): string
{
return $this->_googleAnalyticsCampaign;
}

/**
* Sets the Google Analytics campaign that will be submitted to mandrill.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/MandrillMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yiiunit\extensions\mandrill;

use nickcv\mandrill\Message;
use Yii;

class MandrillMessageTest extends TestCase
{
Expand Down Expand Up @@ -60,6 +61,14 @@ public function testChangeMessageAsyncMode()
$this->assertFalse($this->_message->isAsync());
}

public function testSendAt()
{
$date = Yii::$app->formatter->asDate('+5min', 'yyyy-MM-dd HH:mm:ss');
$this->assertNull($this->_message->getSendAt());
$this->assertInstanceOf('\nickcv\mandrill\Message', $this->_message->setSendAt($date));
$this->assertEquals($date, $this->_message->getSendAt());
}

public function testSetTemplateData()
{
// mailchimp
Expand Down
25 changes: 25 additions & 0 deletions tests/MandrillSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace yiiunit\extensions\mandrill;

use nickcv\mandrill\Mailer;
use Yii;

/**
* Class MandrillSendTest
Expand Down Expand Up @@ -123,6 +124,30 @@ public function testCannotSendIfMandrillTemplateNotFound()
$this->assertFalse($result);
}

/**
* @depends testSendMessage
*/
public function testSendAt()
{
$mandrill = new Mailer(['apikey' => $this->_apiKey]);
$result = $mandrill->compose('test')
->setFrom($this->_fromAddress)
->setTo($this->_toAddress)
->setSubject('test send at email')
->setSendAt(Yii::$app->formatter->asDate('+5min', 'yyyy-MM-dd HH:mm:ss'))
->send();

$this->assertInternalType('array', $mandrill->getLastTransaction());
$lastTransaction = $mandrill->getLastTransaction()[0];
$this->assertArrayHasKey('email', $lastTransaction);
$this->assertEquals($this->_toAddress, $lastTransaction['email']);
$this->assertArrayHasKey('status', $lastTransaction);
$this->assertEquals('scheduled', $lastTransaction['status']);
$this->assertArrayHasKey('_id', $lastTransaction);

$this->assertTrue($result);
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 74844c1

Please sign in to comment.