1

I have base64 encoded image stored in the database. I want to generate emails with this base64 image as inline image within the body. I tried sending the image as

<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAooA....'/>

But my receiving email server is considering email body with base64 content as spam. But I was able to solve this issue in python. The code used for the same is

msg = MIMEMultipart()
msg['Subject'] = Header(u'Subject', 'utf-8')
msg['From'] = sender
msg['To'] = receiver

msg_alternative = MIMEMultipart('alternative')
msg.attach(msg_alternative)

data = 'Hi, <img src="cid:imageId" alt="alternate text"><br><br>'
decodedImage = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAooA....")
msg_image = MIMEImage(decodedImage, name="image")
msg_image.add_header('Content-ID', '<imageId>')
msg_image.add_header('Content-Disposition', 'inline')
msg.attach(msg_image)

Using the above code, i was able to deliver the email with inline image successfully using sendmail module in python.

But what I want was to achieve the same using grails email plugin. And this stack overflow URL helped me to start with. Send an image in an email with Grails

But I went somewhere wrong. The code I used is given below.

String base64String = "iVBORw0KGgoAAAANSUhEUgAAAooA....";
byte[] byteArray = Base64.decodeBase64(base64String.getBytes());
String decodedString = new String(byteArray);

String htmlStr = 'Hi, <img src="cid:imageId" alt="alternate text"><br><br>';

    sendMail {
    from "sender@somedomain.com"
    to "receiver@somedomain.com"
    subject "Report - Sample"
    html htmlStr
    inline 'imageId', 'image/png', decodedString
    }

But it is showing some errors like "No matching property inline..". But I think the problem is with the paramater value decodedString with the inline property.

Note: The base64 image string I used is not a complete one.

Please help.

Community
  • 1
  • 1
Nidhin Joseph
  • 1,461
  • 3
  • 15
  • 32

2 Answers2

1

According to plugins source code there are possible inline() invocations:

inline(String fileName, String contentType, byte[] bytes)
inline(File file)
inline(String fileName, File file)
inline(String fileName, String contentType, File file)
inline(String fileName, String contentType, InputStreamSource source)

So you should pass your byteArray (not the String based on it) to match the first one:

sendMail {
    from "sender@somedomain.com"
    to "receiver@somedomain.com"
    subject "Report - Sample"
    html htmlStr
    inline 'imageId', 'image/png', byteArray
}
Paweł Piecyk
  • 2,749
  • 15
  • 17
1

Two things to note here, multipart has to be set to true and inline image should take byte[] or File or InputStreamSource as the last param :

sendMail {
    multipart true //has to be there as first line

    from "sender@somedomain.com"
    to "receiver@somedomain.com"
    subject "Report - Sample"
    html htmlStr

    //Use raw byte array instead of string
    inline 'imageId', 'image/png', byteArray
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117