We're getting the following error when receiving an email containing a Base64 encoded image directly in the html:

Invalid data specified, the provided data does not seem to be a valid image: [B@41eddd5f

Is there something specific we need to do to handle these types of images in an email?

  • 1
    Can you please describe steps how to reproduce your bug. I have html file with image src='data:image/png;base64,i... I'm trying to send it to email-to-case service. Should I see error in case layout? Screenshot will be helpful. Jun 1 '16 at 22:50
  • I can reproduce your bug if additional formatting has been added to image itself. Jun 2 '16 at 6:17

Following trigger parses case description for base64 images and creates attachments for case.

Email-to-Case functionality stores email body in case description. Let's assume we have next html:

        <title>Base64 image</title>
    <h5>How to store (or ignore) Base64 images in emails processed by email-to-case?</h5>
     <img style='display:block; ' id='pic1' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABKUlEQVRYhe1XzQ6CMAwupnsOSFCjz2Hiybf1RGJIeAYPaoxPAole3NwqXcffuPCd6D6ydqVf6ZLicn7DF1mag0IFz9cdYgFt59v13hCxgjABKFS/RetZ43g4OXZZFdA0dRBPM2vbJgBNIiq4Pa79j0RAM1s3tWOj/XJtnWgs+DKLqKavAS6z2g6ugbIqHLsh2fLx9DC2HVwD1GFXnoNTAzH1r7GK7pHAZEDSOYDcKX08tz/+vclAUklfFQV/AkklEs8hOAOSSvp20kT/DWnUfWXFgdsf6cJU4PafXYazB7D0gaUPLH0gqgyzNIdNvnPWgmtgDOdtKkFNcnO7xpB7AQCvEpTm9smnY2luHxNt9w6U5vax4K0B39yuMeReAOCpATn29g278lymP0STAG4R87RyAAAAAElFTkSuQmCC' />
     <img style='display:block; ' id='pic2' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY3growIAAycBLhVrvukAAAAASUVORK5CYII=' />

Second image is 1x1px red dot

In trigger we can use regex to catch all pictures:

or regex with image extension

After we got our pictures, we can store them as attachments in case:

trigger caseTrigger on Case (after insert) {
list <Attachment> attachmentToInsert = new list<Attachment>();
Pattern pngPattern = Pattern.compile('[0-9a-zA-Z\\+/=]{20,}');
for (case cs :trigger.new) {
    Matcher pictures = pngPattern.matcher(cs.description);
    while (pictures.find()) { 
        Attachment attach = new Attachment();
        attach.contentType = 'image/png';
        attach.name = 'emailToCase';
        attach.parentId = cs.id;
        attach.body = EncodingUtil.base64Decode(pictures.group());
insert attachmentToInsert;

If we send HTML specified above to our email to case, we'll got new case and 2 attachments for it:

base64 email to case

In the very end I've found that:

If spaces and new lines has been added for image data - HTML could concatenates it automatically, but email service is adding additional characters. Trigger will create 5 invalid attachment instead of 2.

I think this is your problem, if yes - you should replace spaces and newlines in description e.g. txt = txt.replaceAll( '\\s+', '').replaceAll( '\\n+', '');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.