Skip to content

Commit

Permalink
Fixed sending scheduled reports through Sparkpost & Momentum (#7441)
Browse files Browse the repository at this point in the history
Fixed sending scheduled reports through Sparkpost & Momentum
  • Loading branch information
kuzmany committed May 19, 2019
2 parents 9806f6d + 525d182 commit b8ad211
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
10 changes: 9 additions & 1 deletion app/bundles/EmailBundle/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,17 @@ public function getContentHash()
*/
public function setTo($addresses, $name = null)
{
$name = $this->cleanName($name);

if (!is_array($addresses)) {
$name = $this->cleanName($name);
$addresses = [$addresses => $name];
} elseif (array_keys($addresses)[0] === 0) {
// We need an array of $email => $name pairs
$addresses = array_reduce($addresses, function ($address, $item) use ($name) {
$address[$item] = $name;

return $address;
}, []);
}

$this->checkBatchMaxRecipients(count($addresses));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@ public function jsonSerialize()
if (count($this->metadata) !== 0) {
$json['metadata'] = $this->metadata;
}
if (count($this->substitutionData) !== 0) {

if (count($this->substitutionData) === 0) {
// `substitution_data` is required but Sparkpost will return the following error with empty arrays:
// field 'substitution_data' is of type 'json_array', but needs to be of type 'json_object'
$json['substitution_data'] = new \stdClass();
} else {
$json['substitution_data'] = $this->substitutionData;
}

if ($this->returnPath !== null) {
$json['return_path'] = $this->returnPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,14 @@ public function getSparkPostMessage(\Swift_Mime_Message $message)
$recipient['metadata'] = $metadata[$to['email']];
}

// Apparently Sparkpost doesn't like empty substitution_data or metadata
// Sparkpost requires substitution_data which can be byspassed by using MailHelper::setTo() rather than a Lead via MailHelper::setLead()
// Without it, Sparkpost returns the error: "field 'substitution_data' is required"
// But, it can't be an empty array or Sparkpost will return error: field 'substitution_data' is of type 'json_array', but needs to be of type 'json_object'
if (empty($recipient['substitution_data'])) {
unset($recipient['substitution_data']);
$recipient['substitution_data'] = new \stdClass();
}

// Sparkpost doesn't like empty metadata
if (empty($recipient['metadata'])) {
unset($recipient['metadata']);
}
Expand Down
30 changes: 30 additions & 0 deletions app/bundles/EmailBundle/Tests/Helper/MailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,34 @@ protected function getMockFactory($mailIsOwner = true, $parameterMap = [])

return $mockFactory;
}

public function testArrayOfAddressesAreRemappedIntoEmailToNameKeyValuePair()
{
$mockFactory = $this->getMockBuilder(MauticFactory::class)
->disableOriginalConstructor()
->getMock();
$mockFactory->method('getParameter')
->will(
$this->returnValueMap(
[
['mailer_return_path', false, null],
['mailer_spool_type', false, 'memory'],
]
)
);

$swiftMailer = new \Swift_Mailer(new SmtpTransport());

$mailer = new MailHelper($mockFactory, $swiftMailer, ['nobody@nowhere.com' => 'No Body']);

$mailer->setTo(['sombody@somewhere.com', 'sombodyelse@somewhere.com'], 'test');

$this->assertEquals(
[
'sombody@somewhere.com' => 'test',
'sombodyelse@somewhere.com' => 'test',
],
$mailer->message->getTo()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,46 @@ private function geTransformToTransmissionComplexData()
"email":"to1@test.local",
"name":"To1 test",
"header_to":"to1@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"to2@test.local",
"name":"To2 test",
"header_to":"to2@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"cc1@test.local",
"name":"CC1 test",
"header_to":"cc1@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"cc2@test.local",
"name":"CC2 test",
"header_to":"cc2@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc1@test.local",
"name":"BCC1 test"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc2@test.local",
"name":"BCC2 test"
}
},
"substitution_data": {}
}
],
"content":{
Expand Down Expand Up @@ -208,26 +214,30 @@ private function geTransformToTransmissionComplexDataWithEmailName()
"email":"cc1@test.local",
"name":"CC1 test",
"header_to":"cc1@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"cc2@test.local",
"name":"CC2 test",
"header_to":"cc2@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc1@test.local",
"name":"BCC1 test"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc2@test.local",
"name":"BCC2 test"
}
},
"substitution_data": {}
}
],
"content":{
Expand Down Expand Up @@ -344,26 +354,30 @@ private function geTransformToTransmissionComplexDataWithUtmTag()
"email":"cc1@test.local",
"name":"CC1 test",
"header_to":"cc1@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"cc2@test.local",
"name":"CC2 test",
"header_to":"cc2@test.local"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc1@test.local",
"name":"BCC1 test"
}
},
"substitution_data": {}
},
{
"address":{
"email":"bcc2@test.local",
"name":"BCC2 test"
}
},
"substitution_data": {}
}
],
"content":{
Expand Down

0 comments on commit b8ad211

Please sign in to comment.