Albert Albert - 6 months ago 57
PHP Question

Embed image in CakePHP3 Email

Original Question



I am sending an email using CakePHP 3's Mailer Aware trait.

My mail is being sent with this function:

public function forgotPassword($user,$password)
{
$this
->from(['no-reply@deltabec.com' => 'DeltaBEC Intraweb'])
->emailFormat('html')
->to([$user->email => $user->name . ' ' . $user->surname])
->subject('New Intraweb Password')
->template('Users' . DS . 'forgot_password', 'default')
->attachments([[
'file' => WWW_ROOT . 'img' . DS . 'logo-dark.png',
'mimetype' => 'image/png',
'contentId' => 'company_logo'
]])
->viewVars(compact('user','password'));
}


In my email I have this:

<?= $this->Html->image('cid:company_logo'); ?>


But my mail comes through like this:

enter image description here

What am I doing wrong? Is there a reason it's not embedding the image in the email?

EDIT 1 - Discovery



So I took the email and saved it as html. This is what it shows me:

<img id="_x0000_i1025" src="/cakesite/img/cid%3Acompany_logo">


Seems it's encoding the link and placing a whole URL instead of just the
cid


EDIT 2 - Feature request



I'd expect it to work the way I used it. There's nothing fundamentally wrong with the way I did it, so I added a ticket on Github so it can be discussed, and hopefully it get's added.

ndm ndm
Answer

The HTML helpers image() method, respectively the URL helper, doesn't recognize the special cid scheme, or any other scheme that doesn't use // after the :, and therefore will try to create an image asset URL.

This may be something for an enhancement request, or maybe even considered as a bug, so you may want to open a ticket over at GitHub.

For now, just ditch using the helper and create the <img> tags manually.