Skip to content

Commit

Permalink
Merge pull request #721 from mailchimp/mandrill-hotfix-issue719
Browse files Browse the repository at this point in the history
Mandrill hotfix issue719
  • Loading branch information
Santiagoebizmarts authored Jun 1, 2018
2 parents c3ebf99 + fed5b34 commit 853cc0b
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 14 deletions.
92 changes: 80 additions & 12 deletions app/code/community/Ebizmarts/MailChimp/Model/Email/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ class Ebizmarts_MailChimp_Model_Email_Template extends Ebizmarts_MailChimp_Model

/**
* @param array|string $email
* @param null $name
* @param array $variables
* @param null $name
* @param array $variables
* @return bool
* @throws Exception
*/
public function send($email, $name = null, array $variables = array())
{
$email_config = $this->getDesignConfig();
$store = (integer) $email_config->getStore();
if (!Mage::getStoreConfig(Ebizmarts_MailChimp_Model_Config::MANDRILL_ACTIVE, $store)) {
return parent::send($email, $name, $variables);
$storeId = (integer) $email_config->getStore();
$mandrillHelper = $this->makeMandrillHelper();
if (!$mandrillHelper->isMandrillEnabled($storeId)) {
return $this->parentSend($email, $name, $variables);
}

if (!$this->isValidForSend()) {
Expand All @@ -50,13 +52,13 @@ public function send($email, $name = null, array $variables = array())
$subject = $this->getProcessedTemplateSubject($variables);

$email = array('subject' => $subject, 'to' => array());
$setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
$setReturnPath = $this->getSendingSetReturnPath();
switch ($setReturnPath) {
case 1:
$returnPathEmail = $this->getSenderEmail();
break;
case 2:
$returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
$returnPathEmail = $this->getSendingReturnPathEmail();
break;
default:
$returnPathEmail = null;
Expand Down Expand Up @@ -88,7 +90,7 @@ public function send($email, $name = null, array $variables = array())

$email['from_name'] = $this->getSenderName();
$email['from_email'] = $this->getSenderEmail();
$mandrillSenders = $mail->senders->domains();
$mandrillSenders = $this->getSendersDomains($mail);
$senderExists = false;
foreach ($mandrillSenders as $sender)
{
Expand All @@ -101,11 +103,11 @@ public function send($email, $name = null, array $variables = array())
}

if(!$senderExists) {
$email['from_email'] = Mage::getStoreConfig('trans_email/ident_general/email');
$email['from_email'] = $this->getGeneralEmail();
}

$headers = $mail->getHeaders();
$headers[] = Mage::helper('mailchimp/mandrill')->getUserAgent();
$headers[] = $mandrillHelper->getUserAgent();
$email['headers'] = $headers;
if (isset($variables['tags']) && count($variables['tags'])) {
$email ['tags'] = $variables['tags'];
Expand Down Expand Up @@ -161,8 +163,10 @@ public function send($email, $name = null, array $variables = array())
}

try {
$result = $mail->messages->send($email);
$result = $this->sendMail($email, $mail);
$this->_mail = null;
} catch (Exception $e) {
$this->_mail = null;
Mage::logException($e);
return false;
}
Expand All @@ -172,7 +176,9 @@ public function send($email, $name = null, array $variables = array())
}

/**
* @return Mandrill_Message|Zend_Mail
* @return Mandrill_Message|null|Zend_Mail
* @throws Mage_Core_Model_Store_Exception
* @throws Mandrill_Error
*/
public function getMail()
{
Expand All @@ -190,4 +196,66 @@ public function getMail()
return $this->_mail;
}
}

/**
* @return Ebizmarts_MailChimp_Helper_Mandrill
*/
protected function makeMandrillHelper()
{
return Mage::helper('mailchimp/mandrill');
}

/**
* @param $email
* @param $name
* @param array $variables
* @return bool
*/
protected function parentSend($email, $name, array $variables)
{
return parent::send($email, $name, $variables);
}

/**
* @return mixed
*/
protected function getSendingSetReturnPath()
{
return Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
}

/**
* @param $mail
* @return mixed
*/
protected function getSendersDomains($mail)
{
return $mail->senders->domains();
}

/**
* @param $email
* @param $mail
* @return mixed
*/
protected function sendMail($email, $mail)
{
return $mail->messages->send($email);
}

/**
* @return mixed
*/
protected function getSendingReturnPathEmail()
{
return Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
}

/**
* @return mixed
*/
protected function getGeneralEmail()
{
return Mage::getStoreConfig('trans_email/ident_general/email');
}
}
4 changes: 2 additions & 2 deletions app/code/community/Ebizmarts/MailChimp/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<config>
<modules>
<Ebizmarts_MailChimp>
<version>1.1.12</version>
<version>1.1.12.1</version>
</Ebizmarts_MailChimp>
<Ebizmarts_Mandrill>
<version>1.1.12</version>
<version>1.1.12.1</version>
</Ebizmarts_Mandrill>
</modules>
<global>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

class Ebizmarts_MailChimp_Model_TemplateTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Mage::app('default');
}

public function testSend()
{
$storeId = 1;
$enabled = true;
$email = '[email protected]';
$name = 'name';
$variables = array('email' => $email, 'name' => $name);
$message = 'message';
$subject = 'subject';
$returnPath = 1;
$senderEmail = '[email protected]';
$bcc = array('[email protected]');
$userAgent = 'Ebizmarts_Mandrill1.1.12/MageCE1.9.3.7';
$emailArray = array ('subject' => 'subject', 'to' => array(array('email' => $email, 'name' => $name), array('email' => '[email protected]', 'type' => 'bcc')), 'from_name' => 'name',
'from_email' => $senderEmail, 'headers' => array($userAgent), 'tags' => array('default_tag'), 'text' => 'message');
$mandrillSenders = array(array('domain' => 'email.com'));

/**
* @var \Ebizmarts_MailChimp_Model_Email_Template $templateMock
*/
$templateMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Email_Template::class)
->disableOriginalConstructor()
->setMethods(array('getDesignConfig', 'isValidForSend', 'setUseAbsoluteLinks', 'getProcessedTemplate',
'getProcessedTemplateSubject', 'getSenderEmail', 'getMail', 'getSenderName', 'isPlain', 'makeMandrillHelper',
'hasQueue', 'getQueue', 'makeHelper', 'getSendingSetReturnPath', 'getSendersDomains', 'sendMail'))
->getMock();

$varienObjectMock = $this->getMockBuilder(Varien_Object::class)
->disableOriginalConstructor()
->setMethods(array('getStore'))
->getMock();

$mandrillHelperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Mandrill::class)
->disableOriginalConstructor()
->setMethods(array('isMandrillEnabled', 'getUserAgent'))
->getMock();

$mailObjectMock = $this->getMockBuilder(Mandrill_Message::class)
->disableOriginalConstructor()
->setMethods(array('getBcc', 'getHeaders'))
->getMock();

$templateMock->expects($this->once())->method('getDesignConfig')->willReturn($varienObjectMock);

$varienObjectMock->expects($this->once())->method('getStore')->willReturn($storeId);

$templateMock->expects($this->once())->method('makeMandrillHelper')->willReturn($mandrillHelperMock);

$mandrillHelperMock->expects($this->once())->method('isMandrillEnabled')->with($storeId)->willReturn($enabled);

$templateMock->expects($this->once())->method('isValidForSend')->willReturn(true);
$templateMock->expects($this->once())->method('setUseAbsoluteLinks')->willReturn(true);
$templateMock->expects($this->once())->method('getProcessedTemplate')->with($variables, true)->willReturn($message);
$templateMock->expects($this->once())->method('getProcessedTemplateSubject')->with($variables)->willReturn($subject);
$templateMock->expects($this->once())->method('getSendingSetReturnPath')->willReturn($returnPath);
$templateMock->expects($this->exactly(2))->method('getSenderEmail')->willReturnOnConsecutiveCalls($senderEmail, $senderEmail);
$templateMock->expects($this->once())->method('getMail')->willReturn($mailObjectMock);

$mailObjectMock->expects($this->once())->method('getBcc')->willReturn($bcc);

$templateMock->expects($this->once())->method('getSenderName')->willReturn($name);
$templateMock->expects($this->once())->method('getSendersDomains')->with($mailObjectMock)->willReturn($mandrillSenders);

$mailObjectMock->expects($this->once())->method('getHeaders')->willReturn(array());

$mandrillHelperMock->expects($this->once())->method('getUserAgent')->willReturn($userAgent);

$templateMock->expects($this->once())->method('isPlain')->willReturn(true);
$templateMock->expects($this->once())->method('hasQueue')->willReturn(true);
$templateMock->expects($this->once())->method('getQueue')->willReturn(true);
$templateMock->expects($this->once())->method('sendMail')->with($emailArray, $mailObjectMock);

$templateMock->send($email, $name, $variables);
}
}

0 comments on commit 853cc0b

Please sign in to comment.