Skip to content

Commit

Permalink
Merge pull request #3 from dghyse/feature/fixEmbedAttachments
Browse files Browse the repository at this point in the history
Add Embed managment
  • Loading branch information
pgaultier authored Sep 4, 2018
2 parents 0d4a1ba + da9929c commit 45d16f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ public function sendMessage($message)
$mailJetMessage['Attachments'] = $attachments;
}

//Get inilined attachments
$inlinedAttachments = $message->getInlinedAttachments();
if ($inlinedAttachments !== null) {
$mailJetMessage['InlinedAttachments'] = $inlinedAttachments;
}

$headers = $message->getHeaders();
if (empty($headers) === false) {
$mailJetMessage['Headers'] = $headers;
Expand Down
38 changes: 34 additions & 4 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class Message extends BaseMessage
*/
protected $attachments = [];

/**
* @var array
*/
protected $inlinedAttachments = [];

/**
* @var string
*/
Expand Down Expand Up @@ -496,6 +501,31 @@ public function getAttachments()
}
}

/**
* @return array|null list of inlinedAttachments
* @since XXX
*/
public function getInlinedAttachments()
{
if (empty($this->inlinedAttachments) === true) {
return null;
} else {
$inlinedAttachments = array_map(function($inlinedAttachment) {
$item = [
'ContentType' => $inlinedAttachment['ContentType'],
'Filename' => $inlinedAttachment['Name'],
'Base64Content' => $inlinedAttachment['Content'],
];
if (isset($inlinedAttachment['ContentID']) === true) {
$item['ContentID'] = $inlinedAttachment['ContentID'];
}
return $item;
}, $this->inlinedAttachments);
return $inlinedAttachments;
}
}


/**
* @inheritdoc
*/
Expand Down Expand Up @@ -558,8 +588,8 @@ public function embed($fileName, array $options = [])
} else {
$embed['ContentType'] = 'application/octet-stream';
}
$embed['ContentID'] = 'cid:' . uniqid();
$this->attachments[] = $embed;
$embed['ContentID'] = uniqid();
$this->inlinedAttachments[] = $embed;
return $embed['ContentID'];
}

Expand All @@ -581,8 +611,8 @@ public function embedContent($content, array $options = [])
} else {
$embed['ContentType'] = 'application/octet-stream';
}
$embed['ContentID'] = 'cid:' . uniqid();
$this->attachments[] = $embed;
$embed['ContentID'] = uniqid();
$this->inlinedAttachments[] = $embed;
return $embed['ContentID'];
}

Expand Down
32 changes: 16 additions & 16 deletions tests/unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,28 @@ public function testGettersSetters()
$this->assertEquals(base64_encode('plop'), $message->getAttachments()[3]['Base64Content']);

$cid = $message->embed(__FILE__, ['fileName' => 'file.php', 'contentType' => 'text/plain']);
$this->assertEquals('file.php', $message->getAttachments()[4]['Filename']);
$this->assertEquals('text/plain', $message->getAttachments()[4]['ContentType']);
$this->assertEquals(base64_encode(file_get_contents(__FILE__)), $message->getAttachments()[4]['Base64Content']);
$this->assertEquals($cid, $message->getAttachments()[4]['ContentID']);
$this->assertEquals('file.php', $message->getInlinedAttachments()[0]['Filename']);
$this->assertEquals('text/plain', $message->getInlinedAttachments()[0]['ContentType']);
$this->assertEquals(base64_encode(file_get_contents(__FILE__)), $message->getInlinedAttachments()[0]['Base64Content']);
$this->assertEquals($cid, $message->getInlinedAttachments()[0]['ContentID']);

$cid = $message->embed(__FILE__);
$this->assertEquals('MessageTest.php', $message->getAttachments()[5]['Filename']);
$this->assertEquals('application/octet-stream', $message->getAttachments()[5]['ContentType']);
$this->assertEquals(base64_encode(file_get_contents(__FILE__)), $message->getAttachments()[5]['Base64Content']);
$this->assertEquals($cid, $message->getAttachments()[5]['ContentID']);
$this->assertEquals('MessageTest.php', $message->getInlinedAttachments()[1]['Filename']);
$this->assertEquals('application/octet-stream', $message->getInlinedAttachments()[1]['ContentType']);
$this->assertEquals(base64_encode(file_get_contents(__FILE__)), $message->getInlinedAttachments()[1]['Base64Content']);
$this->assertEquals($cid, $message->getInlinedAttachments()[1]['ContentID']);

$cid = $message->embedContent('plop', ['fileName' => 'file.php', 'contentType' => 'text/plain']);
$this->assertEquals('file.php', $message->getAttachments()[6]['Filename']);
$this->assertEquals('text/plain', $message->getAttachments()[6]['ContentType']);
$this->assertEquals(base64_encode('plop'), $message->getAttachments()[6]['Base64Content']);
$this->assertEquals($cid, $message->getAttachments()[6]['ContentID']);
$this->assertEquals('file.php', $message->getInlinedAttachments()[2]['Filename']);
$this->assertEquals('text/plain', $message->getInlinedAttachments()[2]['ContentType']);
$this->assertEquals(base64_encode('plop'), $message->getInlinedAttachments()[2]['Base64Content']);
$this->assertEquals($cid, $message->getInlinedAttachments()[2]['ContentID']);

$cid = $message->embedContent('plop', ['fileName' => 'file.php']);
$this->assertEquals('file.php', $message->getAttachments()[7]['Filename']);
$this->assertEquals('application/octet-stream', $message->getAttachments()[7]['ContentType']);
$this->assertEquals(base64_encode('plop'), $message->getAttachments()[7]['Base64Content']);
$this->assertEquals($cid, $message->getAttachments()[7]['ContentID']);
$this->assertEquals('file.php', $message->getInlinedAttachments()[3]['Filename']);
$this->assertEquals('application/octet-stream', $message->getInlinedAttachments()[3]['ContentType']);
$this->assertEquals(base64_encode('plop'), $message->getInlinedAttachments()[3]['Base64Content']);
$this->assertEquals($cid, $message->getInlinedAttachments()[3]['ContentID']);
}

public function testAttachException()
Expand Down

0 comments on commit 45d16f6

Please sign in to comment.