1

In odoo 13 i have a field example:

birthday_picture = fields.Image(string='Birthday Picture')

i want to add birthday_picture image to be added in email template also which i declared in xml, i have added <img> tag and added still the image is not reflecting below i have shared the line

<div>
    <img src="${'/birthday_picture.png=%s' % object.birthday_picture}" style="width: 60px; height: 60px"/><br/>
</div>

please let me know where i am doing wrong

Sidharth Panda
  • 404
  • 3
  • 17

2 Answers2

1

If birthday_picture must be dynamic (this means the same email template may render different images) you should consider using fields.Binary

fields.Binary stores a binary file in odoo filesystem and returns a base64 encoded string.

birthday_picture = fields.Binary(string='Birthday Picture')
<img src="${'data:image/png;base64,%s' % object.birthday_picture}" style="width: 60px; height: 60px"/>

In case birthday_picture is always the same (so it is static) best solution would be to create an ir.attachment, set it as public and render into src it's url

Another option would be to manually convert your file into base64 using an online tool and replace src value with the encoded string.

icra
  • 460
  • 3
  • 14
  • Yes Brother i want the image dynamically, and as you sid above i did try this but still doesnot return image which i provide in binary field – Sidharth Panda Jan 11 '23 at 02:41
  • Can you debug email raw body in order to understand what it is printing instead of the base64 string? Also are you sure the model selected in `mail.template` is the same model where you added `birthday_picture` field? Try printing `object.birthday_picture` into the email template to further investigate – icra Jan 11 '23 at 19:03
  • yeah i am sure `birthday_picture` is in the same model as provided in `mail.template`, also i already checked what is printing instead of base64 string, it is printing the binary format of the image. – Sidharth Panda Jan 12 '23 at 02:14
  • This means binary data must be first converted to base64. You can use `image_data_uri` or `to_text` qweb helper functions to obtain this. For example: `${'data:image/png;base64,%s' % image_data_uri(object.birthday_picture)}` – icra Jan 12 '23 at 19:18
  • No still i cannot see the template to reflect the image however i already solved that – Sidharth Panda Jan 13 '23 at 07:31
0

Actually i have added this:

<p>
    <img src="/web/image/birthday.reminder/${object.id}/birthday_picture/"/>
</p>

which doesnot reflect the image in template but when i looked for undelivered email from techncal > email. by removing force_send from code i can see my image reflected on it.

however i already searched that the email doesnot support base64 images but there is a tweak to do it as well. here is the link which descried the tweak

Sidharth Panda
  • 404
  • 3
  • 17