0

What I have

In my Controller (CakePHP 3.8):

$email = new \Cake\Mailer\Email();
$email
    ->emailFormat('html')
    ->setLayout('mylayout')
    ->setTo($to)
    ->setSubject($subject)
    ->addAttachments([
        'logo.png' => [
            'file' => $logoImage,
            'mimetype' => mime_content_type($logoImage),
            'contentId' => 'mylogo'
        ]
    ])
    ->send($message);

And then in src/Template/Layout/Email/html/mylayout.ctp:

<img src="cid:mylogo">

That works.

What I want

I want to embed that image from within the Layout file.

  • So I don't have to repeat ->addAttachments() every time I'm sending an e-mail
  • If I replace the image I can do it in the Layout only once
  • If I use another Layout that doesn't need the image, it won't be attached and sent

Using contentId looks like an option. But how do I do it from inside the Layout?

What I tried

  • Sending the image as data/uri, but that didn't work
  • Searching for some Cake\Mailer instance under $this when in Layout, but couldn't find any
ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • Adding attachments seems kinda out of scope for the view layer, you should probably look into using [**reusable mailers**](https://book.cakephp.org/3/en/core-libraries/email.html#creating-reusable-emails) instead. – ndm Nov 15 '19 at 15:26
  • @ndm, thanks. [The reusable emails example](https://book.cakephp.org/3/en/core-libraries/email.html#creating-reusable-emails) creates *a Mailer that contains user-related emails*, so there must be a reason for that separation. Please advise, if I create a one big Mailer reused for all emails, users and not, would it break some CakePHP pattern or convention that I may not be knowing/thinking about? – ᴍᴇʜᴏᴠ Nov 18 '19 at 11:01

0 Answers0