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.