AddEmbeddedImage in PHPMailer

Hello,
is there anything compareable with the PHPMailer methode:

**AddEmbeddedImage**

in the cake mailer class?
Or do you suggest something else?
I like to embedd images into a mail body, it is ignoring the images.
Thanks in advance

Hi Klaus,

With your mailer class add the image / logo as an attachment, generate and pass a $contentId to your html template

// mailer 
$contentId = uniqid('tgn-logo');

$mailer
   // to, subject etc
   ->setAttachments([
            'toggen-logo-122x27.png' => [
                'file' => WWW_ROOT . 'img/toggen-logo-122x27.png',
                'mimetype' => 'image/png',
                'contentId' => $contentId,
])
   ->setVars(compact('contentId'))
  //.... 

// in your html email template add an img tag

<img src="<?= 'cid:' . $contentId; ?>">
<!-- or the cake way-->
<?= $this->Html->tag('img', null, [
    'src' => "cid:{$contentId}"
]); ?>

// looks like this
<img src="cid:tgn-logo6831bf349f7c4">

Hello jmcd,
many thanks, it’s working.
And now I have seen, it was already in the book.
But I had not seen it.
Best regards
Klaus

Hello,
thanks, I have replied via Forum.
But do you have any idea, how to implement the function ->setAttachments

Depending on the number of images. May one or for images.
I tried to create a foreach and insert the resukt like this:

    $i = 1;
    $attachments = array();
    //dd($pictures);
    foreach ($pictures as $pic) {
        $attachments[] = ['profile1.png' => [
                'file' => $pic,
                'mimetype' => 'image/png',
                'contentId' => 'pic-id-' . $i,
            ],
        ];
    }
    $mailer->setAttachments([
        $attachments,
    ]);

Kind regards
Klaus
Tel. 0173 5391019

Hi Klaus,
Just format your array as follows:

        $attachments = [     
            'toggen-logo-122x27.png' => [
                   'file' => WWW_ROOT . 'img/toggen-logo-122x27.png',
                   'mimetype' => 'image/png',
                   'contentId' => $contentId,
            ],
            'profile1.png' => [
                   'file' => WWW_ROOT . 'img/myrandom.jpeg',
                   // or 'data' => $imageContentHere,
                   'mimetype' => 'image/jpeg',
                   'contentId' => 'pic-id-pic2',
            ],
       ]
1 Like