Skip to content

Commit f379f29

Browse files
committed
Merge pull request #28 from bnrmth/master
Added embedded images support
2 parents 889d390 + e0ae60d commit f379f29

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

Message.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ class Message
209209
*/
210210
protected $attachments = array();
211211

212+
/**
213+
* an array of images embedded in the message
214+
*
215+
* @var array
216+
*/
217+
protected $images = array();
218+
212219
/**
213220
* a custom domain to use for tracking opens and clicks instead of mandrillapp.com
214221
*
@@ -482,6 +489,65 @@ public function addAttachmentFromPath($path, $type = '', $name = '') {
482489
return $this;
483490
}
484491

492+
/**
493+
* Add images embedded in the message
494+
*
495+
* @param string $type - the MIME type of the image - must start with "image/"
496+
* @param string $name - the Content-ID of the embedded image - use <img src="cid:THIS_VALUE"> to reference the image in your HTML content
497+
* @param string $data - base64 encoded image data
498+
*
499+
* @return Message
500+
*/
501+
public function addImage($type, $name, $data)
502+
{
503+
$this->images[] = array(
504+
'type' => $type,
505+
'name' => $name,
506+
'content' => $data
507+
);
508+
509+
return $this;
510+
}
511+
512+
/**
513+
* @param string $path - path to local or remote image file
514+
* @param string $type - the MIME type of the image - must start with "image/" (optional)
515+
* @param string $name - the Content-ID of the embedded image - use <img src="cid:THIS_VALUE"> to reference the image in your HTML content (optional)
516+
*
517+
* @throws \Exception
518+
* @return Message
519+
*/
520+
public function addImageFromPath($path, $type = '', $name = '')
521+
{
522+
if (!is_readable($path)) {
523+
throw new \Exception('cannot read file ' . $path);
524+
}
525+
526+
$data = file_get_contents($path);
527+
528+
$base64data = base64_encode($data);
529+
530+
if (empty($name)) {
531+
$name = basename($path);
532+
}
533+
534+
if (empty($type)) {
535+
// open fileinfo database
536+
$finfo = finfo_open(FILEINFO_MIME, '/usr/share/misc/magic');
537+
538+
if (!$finfo) {
539+
throw new \Exception('Opening fileinfo database failed, please specify type');
540+
}
541+
542+
$type = finfo_file($finfo, $path);
543+
finfo_close($finfo);
544+
}
545+
546+
$this->addImage($type, $name, $base64data);
547+
548+
return $this;
549+
}
550+
485551
/**
486552
* an optional address to receive an exact copy of each recipient's email
487553
*
@@ -720,6 +786,14 @@ public function getAttachments()
720786
return $this->attachments;
721787
}
722788

789+
/**
790+
* @return array
791+
*/
792+
public function getImages()
793+
{
794+
return $this->images;
795+
}
796+
723797
/**
724798
* @return boolean
725799
*/

Tests/MessageTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,16 @@ public function testSetSubaccount()
124124

125125
$this->assertEquals($message->getSubaccount(), 'Subaccount Name');
126126
}
127+
128+
public function testAddImage()
129+
{
130+
$message = new Message();
131+
132+
$message->addImage('image/jpg', '1x1', '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AL+AD//Z');
133+
134+
$images = $message->getImages();
135+
136+
$this->assertEquals(1, count($images));
137+
$this->assertEquals($images[0]['name'], '1x1');
138+
}
127139
}

0 commit comments

Comments
 (0)