9

Context: e-mails are sent with text attachments from a Dynamics SL/SQL Database to a Salesforce e-mail processor. The text attachments contain a formatted file with data to be processed and imported into Salesforce.

However, despite having the '.txt' file extension, Salesforce processes them as binaryAttachments, complete with a MIMEType of application/octet-stream. The 'toString' method fails due to the MIMEType, and EncodingUtil.base64Encode(Blob body) spits out gibberish.

The files are not corrupted. I have Salesforce sending me alerts on failure, and the attachments open fine on my local machine.

Does anyone know if there is a way to change the 'mimeTypeSubType' value of an attachment so that the toString() method will operate? Or is there another way to extract plain text (ascii encoded) content from an application/octet-stream attachment?

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    if(email.textAttachments != null && !email.textAttachments.isEmpty() && email.textAttachments.size() >= 1){
        System.debug(email.textAttachments.size());
        System.debug(email.textAttachments[0].mimeTypeSubType);
        System.debug(email.textAttachments[0].body);
        processFile(email.textAttachments[0].body, email.subject);
    }else if(email.binaryAttachments != null &&!email.binaryAttachments.isEmpty() && email.binaryAttachments.size() >= 1){
        //processFile(email.binaryAttachments[0].body.toString(), email.subject);
        System.debug(String.valueOf(email.binaryAttachments[0].body));
        System.debug(email.binaryAttachments[0].fileName);
        System.debug(email.binaryAttachments.size());
        System.debug(email.binaryAttachments[0].mimeTypeSubType);
        email.binaryAttachments[0].mimeTypeSubType = 'text/csv';
        System.debug(email.binaryAttachments[0].body);
        System.debug(EncodingUtil.base64Encode(email.binaryAttachments[0].body));
        processFile(EncodingUtil.base64Encode(email.binaryAttachments[0].body), email.subject);
    }else 
        return result;

    return result;
}
8
  • 1
    In your example you're using base64Encode. But you should receive encoded file. Try to use base64Decode. Commented May 29, 2016 at 4:19
  • What is the mime type on the attachments in the email? Are they 'text/csv' in the email itself? Commented May 31, 2016 at 0:32
  • @IlyaLepesh base64Encode is the method which converts a Blob to an unencoded String. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented May 31, 2016 at 3:41
  • 1
    @LucidLunatic Try CC'ing the original email to another email address when it is first sent to Salesforce. For example, you can see the raw email in Gmail. You should be able to see the defined headers there. I suspect that the mime type isn't being set as expected in the source email. Commented May 31, 2016 at 8:56
  • 1
    What error do you get when you say "The 'toString' method fails due to the MIMEType" Commented Jun 1, 2016 at 1:58

1 Answer 1

3
+25

There are multiple things that can cause this behavior.

1) You might have set the email service to accept attachments in only 'Binary attachments only'. If you that is the case then your issue may resolved when you set it to 'text attachments only'

2) The option 'Convert Text Attachments to Binary Attachments' might be checked.

3) There is known issue with some client (including outlook) which is due to MIME type not getting set properly. You can try sending test email email with gmail and check if works. http://help.salesforce.com/HTViewSolution?id=000193349

Can you try above options and let us know if it resolve your issue?

When I run your code in my dev org it gave proper output So now it make me suspicious about configuration or email client as stated above.

22:56:41.0 (11210148)|SYSTEM_METHOD_ENTRY|[5]|List<Messaging.InboundEmail.TextAttachment>.isEmpty()
22:56:41.0 (11245194)|SYSTEM_METHOD_EXIT|[5]|List<Messaging.InboundEmail.TextAttachment>.isEmpty()
22:56:41.0 (11261358)|SYSTEM_METHOD_ENTRY|[5]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11267037)|SYSTEM_METHOD_EXIT|[5]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11276564)|STATEMENT_EXECUTE|[5]
22:56:41.0 (11278654)|STATEMENT_EXECUTE|[6]
22:56:41.0 (11284723)|SYSTEM_METHOD_ENTRY|[6]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11305254)|SYSTEM_METHOD_EXIT|  [6]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11461203)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
22:56:41.0 (11477121)|USER_DEBUG|[6]|DEBUG|1
22:56:41.0 (11484874)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
22:56:41.0 (11490304)|STATEMENT_EXECUTE|[7]
22:56:41.0 (11504075)|HEAP_ALLOCATE|[7]|Bytes:28
22:56:41.0 (11516923)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
22:56:41.0 (11540265)|USER_DEBUG|[7]|DEBUG|text/plain
22:56:41.0 (11546063)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
22:56:41.0 (11550799)|STATEMENT_EXECUTE|[8]
22:56:41.0 (11559314)|HEAP_ALLOCATE|[8]|Bytes:28
22:56:41.0 (11570610)|SYSTEM_METHOD_ENTRY|[8]|System.debug(ANY)
22:56:41.0 (11589259)|USER_DEBUG|[8]|DEBUG|07853426
22:56:41.0 (11594914)|SYSTEM_METHOD_EXIT|[8]|System.debug(ANY)
3
  • Sorry, meant to comment on this two days ago. The e-mail service is set to accept both binary and text attachments, and does not have "Convert Text Attachments to Binary Attachments" checked. The issue is certainly some variation on #3, as discussed in the question and comments. The MIME type is not set properly, but that system is out of my control. The question is how to extract the plain text from the blob despite the MIME type. Commented Jun 3, 2016 at 20:52
  • The code given is question is correct code. There is no issue with that and that is how we covert blob data into text. Now its not working due to the client. just to make sure that its client issue try sending emails with gmail and verify it code works. Commented Jun 6, 2016 at 4:49
  • But given that the file is not corrupt- it can be saved as an Attachment and opened, etc. there must be a way to programmatically extract the text content. Commented Jun 8, 2016 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .