Albert Albert - 2 years ago 196
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)
->from(['' => 'DeltaBEC Intraweb'])
->to([$user->email => $user->name . ' ' . $user->surname])
->subject('New Intraweb Password')
->template('Users' . DS . 'forgot_password', 'default')
'file' => WWW_ROOT . 'img' . DS . 'logo-dark.png',
'mimetype' => 'image/png',
'contentId' => 'company_logo'

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download