@@ -209,6 +209,13 @@ class Message
209
209
*/
210
210
protected $ attachments = array ();
211
211
212
+ /**
213
+ * an array of images embedded in the message
214
+ *
215
+ * @var array
216
+ */
217
+ protected $ images = array ();
218
+
212
219
/**
213
220
* a custom domain to use for tracking opens and clicks instead of mandrillapp.com
214
221
*
@@ -482,6 +489,65 @@ public function addAttachmentFromPath($path, $type = '', $name = '') {
482
489
return $ this ;
483
490
}
484
491
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
+
485
551
/**
486
552
* an optional address to receive an exact copy of each recipient's email
487
553
*
@@ -720,6 +786,14 @@ public function getAttachments()
720
786
return $ this ->attachments ;
721
787
}
722
788
789
+ /**
790
+ * @return array
791
+ */
792
+ public function getImages ()
793
+ {
794
+ return $ this ->images ;
795
+ }
796
+
723
797
/**
724
798
* @return boolean
725
799
*/
0 commit comments