4

I am using the Grails mail plugin to send emails and I want to send images in the body of the email, not as attachment. What I want is for images to be shown in the email body itself, as in newsletters. I tried:

img style="display:block; height:100px; width:100; " alt="candle"
src="http://www.xyz.com//Candle_4.jpg">

but it is being displayed as-is.

I also tried using the wikimedia format:

[[:File:Example.jpg]]<br/>
[[Special:FilePath/Example.jpg]]

but again, both seem to link to external.

What am I doing wrong?

This is the email template I am using:

<img src="cid:springsourceInlineImage" /> Dear [username],<br/>
Thank you for shopping with us.<br/>
You have placed a new order with following details.<br/>
[details]<br/>
Happy shopping!

But, if I want to put 10 images in my template, how would I do that?

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
zade
  • 165
  • 3
  • 14

3 Answers3

13

You have to do three things

  1. Declare mail as multipart
  2. Attach your image as inline image
  3. Reference you inline image from your mail template

According to this mailing list thread it should work like this:

sendMail{
    multipart true
    to "[hidden email]"
    subject "Subject goes here"
    html  g.render( template: '/emails/mailTemplate')
    inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')
}

In your template you could refer the image like this

<img src="cid:springsourceInlineImage" />

Hope that helps.

aiolos
  • 4,637
  • 1
  • 23
  • 28
  • What is cid? it needs to be set? because now the code** ** is appearing in email. ie. still not displaying.. – zade May 04 '12 at 13:21
  • The `cid` scheme stands for `content-ID` and is used to identify a specific part of your multipart mail. Is the wished image attached to the mail? If the presented code is the whole HTML source of your mail, it looks like you haven't a complete mail template. – aiolos May 04 '12 at 21:34
  • sory for the late reply, actually now the image is in my mailbox but as an attachment with "noname", moreover the line " " is still displayed as it is in the template as it is. – zade May 07 '12 at 05:47
  • Coukld you please post your whole mail template? – aiolos May 07 '12 at 09:27
  • hey!! I added the template in the question – zade May 07 '12 at 13:55
  • Although your mail template isn't well formed html - I tested the code and everything works fine. The image is attached and cid reference is working for my mail client. I uesd grail 2.0.1 with mail plugin version 1.0. Its maybe a problem with your mail client and/or server as @cdeszaq described. You could check the raw mail code. Did you find the base64 encoded inline image part? – aiolos May 08 '12 at 12:29
  • I am using grails 1.3.7 and mail plugin 1.0-SNAPSHOT. i tried using base64 encoded image but even that shows up a very very very long image tag. Using gmail account. Okay I will just chk upgrading grails. Thanks for help – zade May 09 '12 at 06:18
  • Thanks for the help. I was trying to figure it out. Works like a charm – Joel Azevedo Dec 06 '17 at 12:36
  • @aiolos Could I change `springsourceInlineImage` to another name? Whether I can embed multiple images into an email? – Tung Jan 29 '18 at 12:15
  • 1
    Yes. You could use any name. – aiolos Jan 29 '18 at 12:53
  • @aiolos: Thanks for confirming! I have found the official documentation `http://gpc.github.io/grails-mail/` that might be helpful other users. – Tung Jan 29 '18 at 16:56
0

In general, most "newsletter" type emails are just HTML emails and don't in-line the images within the body of the emails. In fact, many email servers will outright reject emails that are too big, and putting the image data inside the body of the email can easily run this risk.

Instead, the way to do with this is to just have HTML emails with the images you wish to include as normal HTML image links.

Please refer to the Mail plugin documentation for more info on how to send HTML emails.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
0

Sorry I don't have enough Stackoverflow points to leave comment on others' answers.

In @aiolos 's answer, it contains:

inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')

This specifies 'image/jpg' as the output, but the file is in png format. Is this intentional?

Wikipedia provides a list of mime types: http://en.wikipedia.org/wiki/Internet_media_type#Type_image

I tried 'image/png' and it worked, so I'm not sure if setting it to 'image/jpg' makes any difference.

Max
  • 1,064
  • 9
  • 23