0

I am attempting to identify the format of attachments which come in emails entering my Email Service in order to determine if an attachment is a .csv file.

I was guided to use this as my solution :

email.binaryAttachments[i].mimeTypeSubType.contains('text/csv')

But when I use that the csv file is identified as 'application/vnd.ms-excel'.

What do I use to identify the csv file as 'text/csv' ?

Here is my current code :

global class FunderApprovalEmailServiceCtr implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope envelop)
{        
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();     

    Integer version = 0;                
    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) 
    {
       Map<Id, Program_Contact_Role__c> mapPCRsWithProgram = new Map<Id, Program_Contact_Role__c>();

        List<ContentVersion> contentVersions = new List<ContentVersion>();            

       for(integer i =0, s = email.binaryattachments.size(); i < s; i++) {         

        if(email.binaryAttachments[i].mimeTypeSubType.equals('application/vnd.ms-excel','text/csv')) {

Somebody originally pointed out ContentType to me which appears to be EmailFileAttachment but I was told that doesn't apply to inbound email. But if that's the proper way to go could you please show me how I would fit that into this code ?

Thank you very much for your help.

1 Answer 1

0

Either (a) your OS is mis-configured to flag CSV files as Excel files, or (b) you actually saved this as an Excel file, which is not a CSV file. Try opening your "Excel" file in Notepad (Start > Accessories > Notepad on your computer). If it looks like a CSV, then you'll need to do some troubleshooting to figure out why that's happening (probably outside the scope of this network, try Stack Overflow). If it looks like "garbage", you need to save it as a CSV first, found in the "Save As" dropdown. Excel makes it rather non-obvious how to actually save a CSV file, so it's understandable that you'd accidentally save as the wrong type. Try using a different product, like Open Office, to create your CSV file.

1
  • I forgot to mention : It actually works if I just allow the code to run for an attachment identified as 'application/vnd.ms-excel'. So, if I just let it run it properly treats it like a csv and parses etc. I have saved it as a .csv (several times). I even copy and pasted it into Notepad, saved it as a .csv file - and then sent in an email and it was still identified as an excel file. (but it did work as if it was csv). At this point I'm just gonna allow the code to run for that file type (and csv). I changed my code to allow for multiple types - is that the proper syntax ?
    – Zoom_v
    Commented Jun 2, 2020 at 20:16

You must log in to answer this question.

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