0

I am using apex class and email service to create records on a custom object. The problem is when a client sends an email with inline images, it does not appear in the email body in Salesforce. However, the image does appear in the 'Notes & Attachments' but not in the mail body. See the screenshot below:

enter image description here

Below is my apex class:

public class TT_CaseEmailService implements Messaging.InboundEmailHandler {
    
    public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                           Messaging.InboundEnvelope envelope) 
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();   
        Custom_Object__c ttCase = new Custom_Object__c();
        
        if(email.references == null){
            ttCase = createTTCase(email);
        } 
        
        if (ttCase.Id != null) {
            createTTEmailMessage(ttCase.Id, email);
        }
        
        return result;
    }
    
    @testVisible  
    private static Custom_Object__c createTTCase(Messaging.InboundEmail inboundMessage){
                
        Custom_Object__c tt = new Custom_Object__c();
        
        try {
            tt.Requestor_Email__c = inboundMessage.fromAddress;
            tt.Requestor_Name__c = inboundMessage.fromName;
            tt.Subject__c = String.isNotBlank(inboundMessage.subject) ? inboundMessage.subject : 'No Subject';

            insert tt;
                        
        } catch(Exception e) {
            System.debug('Exception has occured on createTTCase : ' + e.getMessage());
        }
        
        return tt;
    }
  
    @testVisible
    private static void createTTEmailMessage(String ttCaseId, Messaging.InboundEmail email) {
        
        EmailMessage em = new EmailMessage();
        Database.DMLOptions dml = new Database.DMLOptions();
        dml.allowFieldTruncation = true;
        
        try {
            em.HtmlBody = email.htmlBody;
            em.TextBody = email.plainTextBody;
            em.Incoming = true;
            em.Status = '1';
            em.ToAddress = (email.ToAddresses == null || email.ToAddresses.isEmpty()) ? '' : String.join(email.ToAddresses, ',');
            em.FromAddress = email.fromAddress;
            em.FromName = email.fromName;
            em.RelatedToId = ttCaseId;
            em.CcAddress = (email.ccAddresses == null || email.ccAddresses.isEmpty()) ? '' : String.join(email.ccAddresses, ',');
            em.Subject = String.isNotBlank(email.subject) ? email.Subject : 'No Subject';
            
            // Allow the truncation of emails if the content is too large
            em.setOptions(dml);
            
            insert em;
            
            if(em.Id != null && (email.textAttachments != null || email.binaryAttachments != null)) {
                createContentAttachment(ttCaseId, em.Id, email);
            }
            
        } catch (Exception e) {
            System.debug('Exception has occured on createTTEmailMessage : ' + e.getMessage());
        }
    }
    
    @testVisible
    private static void createContentAttachment(Id caseID, Id emailId, Messaging.InboundEmail email) {
                
        List<ContentVersion> cvList = new List<ContentVersion>();
        List<ContentDocumentLink> cdlList = new List<ContentDocumentLink>();
        List<Id> recordIds = new List<Id>{caseID,emailId};
        
        try {
            for (Messaging.Inboundemail.BinaryAttachment binAttach : email.binaryAttachments) {
                ContentVersion conVer = new ContentVersion();
                conVer.Title = binAttach.fileName;
                conVer.PathOnClient = binAttach.fileName;
                conVer.VersionData = binAttach.Body;
                conVer.ContentLocation = 'S'; 
                cvList.add(conVer);
            }
            insert cvList;
            
            cvList = [select id, ContentDocumentId from ContentVersion WHERE Id in :cvList];
            for (Id recID : recordIds) {
                for (ContentVersion cv : cvList) {
                    ContentDocumentLink cl = new ContentDocumentLink();
                    cl.ContentDocumentId = cv.ContentDocumentId;
                    cl.LinkedEntityId = recID;
                    cl.ShareType = 'V';
                    cl.Visibility = 'AllUsers';
                    cdlList.add(cl);
                }
            }
            insert cdlList;
            
        } catch (Exception e) {
            System.debug('Exception has occured on createContentAttachment : ' + e.getMessage());
        }
    }
    
}
2
  • 1
    You need to update your HtmlBody to reference the files you've created, otherwise what in your HtmlBody is pointing to those images? It can't know anything about them.
    – Nick C
    Jul 10 at 12:10
  • Hi @Nick, can you please explain in more details? How should should I update the HTML body? thanks
    – Marjan
    Jul 10 at 14:34

0

You must log in to answer this question.