2

I have to send an image using email template in OpenERP but I have to insert it just like we have a feature of insert image in gmail,yahoo and have here also in stackoverflow.com.

I have to send image like this is because I want it should be displayed at the same time when mail is open, along with the text.

If I send it using attachments it coems in attachments which we can see just after download the image. I dont need this download option what I need is open image.

thanks in advance !

StackUP
  • 1,233
  • 12
  • 22

3 Answers3

7

There are 3 common ways to embed images within an email (in increasing complexity order): using public URLs, using Data URI to inline the image data in the src attribute, and using images attached to the email. In all 3 cases you can use regular HTML techniques to include them in the HTML/Rich-Text body of the OpenERP email template: for example with an <img src="..."/> tag or the CSS background-image property. Only the first 2 techniques will work out-of-the-box in OpenERP's email templates.

1. Using hosted images

The image URL can simply be a public URL on your website or any image hosting service on the internet:<img src="http://www.example.com/myimage.png">. This is very similar to what happens when you embed an image on stackoverflow, as the image is first uploaded to imgur.com automatically and the img src attributes refers to the URL of the uploaded image on imgur.com.

This works but nowadays most email clients (thunderbird, gmail, outlook, ...) will block remote images, as they are commonly abused to detect the fact that a given email was opened by its recipient (if the image is downloaded, it means the mail was opened). Typically the recipient will have to click a banner or warning in order to display these remote images.

This is the only technique that works out-of-the-box with OpenERP's email templates.

2. Using Data URI to inline the image data

The src attribute of an img tag can use the Data URI (RFC 2397) scheme to include the image binary source inline. The image tag would look like this:

<img src="data:mime-type;encoding,<encoded_image_data>"/>

For a PNG image, this could look like this

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."/>

You can use this technique for OpenERP email templates, and it does not have the downside of being blocked by email clients. However you will need to manually convert your image to base64[A]. This will also substantially increase the size of your templates and your outgoing emails, and has several limitations (e.g. max 32KB if you want maximum compatibility).

[A]: Google "online base64 file encoder" if you want a simple way to do the conversion

3. Using attached images (not currently supported in OpenERP)

RFC 2111 specifies a way for message body parts to refer to other body parts using cid: URLs. For example the img src attribute within an email can refer to one of the attachments. Technically the img tag could look like this:

<img src="cid:some_unique_content_id/>

where some_unique_content_id is the Content-ID of the message part that contains the image. This is what it will look like in the final (raw) email:

------=_NextPart_WHATEVER_ID
Content-Type: image/jpeg;
    name="image.jpg"
Content-Transfer-Encoding: base64
Content-ID: <some_unique_content_id>
Content-Disposition: inline;
    filename="image.jpg"

/9j/4AAQ...continuation_of_the_base64_encoded_image

This technique also does not have the downside of being blocked by email clients, but it is more complex to implement because you need to specifically assign a Content-ID to each attachment, and the final email that is sent on the wire is bigger because it must contain the actual images.

This is not currently available in OpenERP, but it would not be very difficult to implement. One trivial way would be to automatically assign sequential Content-ID identifiers to all attachment (e.g. attachment_1, attachment_2) when the final mail is crafted by the email layer, and perhaps add an option in the email template Value Builder assistant to generate cid-based URLs for the attachments.

odony
  • 4,027
  • 17
  • 27
  • odony thanks, but there is so much about emails in OpenERP modules,not getting where to customize it,how to test? – StackUP Nov 22 '12 at 13:09
  • @user1791935: I'm not sure what you want to customize: the code or some test template? You should start by using the first 2 techniques I mentioned, and play with it using templates (no code change needed). For example create a test template on ``CRM Lead`` and preview it using the Preview button (select a sample Lead to render). Then go on the Lead record, and in the Communication&History tab use the "Send New Email" button to open the composition wizard. There will be an option to use templates: just select the test template you created and send yourself a test mail. Is that what you meant? – odony Nov 23 '12 at 14:40
  • odony: that is just a way to send text email with some attachments. I want to send inline image using tag Like you said for jpeg use this tag: Do you think it works? well,It did not work for me. – StackUP Nov 26 '12 at 12:57
  • @user1791935: All the techniques I described explain how to send *inline* images, really. The attachment technique (3) is also a way to send *inline* images. Read it carefully and tell me if you don't understand something. And if you tried something that doesn't work, please complete your question with the actual code you tried, so we can elaborate on that. – odony Nov 27 '12 at 17:29
  • @user1791935: hmm, perhaps you need to actually *read* my answer ;-) What you did is technique 3 in my answer, but the `cid:` part does **not** refer to the file name, it refers to a special ``Content-ID`` header that must be added when the attachments are embedded in the mail (that's why I said it is **not** supported in OpenERP right now). Look in my example: filename is `image.jpg` but `cid` is `some_unique_content_id`. You cannot do that without changing the OpenERP mail generation code. You should try technique 2 if the images are small (<32Kb), otherwise technique 1. – odony Nov 30 '12 at 08:34
  • I tied with your first technique too.I tried to write this code directly on gmail.In compose mail body I wrote but this also did not worked for me. I got the same tag in inbox but not an image. All my images would be greater than 32KB about cid,I read on some blog that cid is the attached file name thats why I tried but did not work form me. – StackUP Dec 03 '12 at 11:44
  • @user1791935: as far as I know gmail does not allow you to edit the HTML source of your messages. So if you type ```` in gmail it will transform it to be just pure text so you will simply see the text when you receive it. You cannot test technique 1 in gmail. Use the Rich Text/HTML version of OpenERP email templates to test it, and make sure you use a correct and complete image URL in the ``src`` attribute of the ``img`` tag: it needs to start with ``http://``. – odony Dec 04 '12 at 12:37
  • Did not work for me I wrote in OpenERP mail template wrote this line used email id of gmail and smtp server "smtp.googlemail.come" but happened same again, got "" in Inbox.If rich text already enabled in OpenERP server 6.1 or from where I can enable it?Did you get to send inline mail using OpenERP? – StackUP Dec 05 '12 at 13:51
1

this maybe useful embedding image in html email

although I don't think this is an openerp-related problem...

Community
  • 1
  • 1
simahawk
  • 2,421
  • 1
  • 16
  • 22
1

Here is how I added the company logo on my Sale Order email template with Odoo 11:

<p>
% set company = object.company_id or ctx.get('company')
% if company.logo:
% set field_value = '%s' % company.logo
% set company_logo_base64 = 'data:image/*;base64,%s' % field_value[2:-1]
% endif
<img t-if="company_logo_base64" src="${company_logo_base64}">
</p>
Wellington Souza
  • 2,200
  • 2
  • 22
  • 33