+ Start a Discussion

Processing email text attachment with Apex

Anyone out there ever do anything with processing the data in an email attachment.
Here is my scenario:  We want to create an inbound email service that receives emails from our old system, including a CSV file as an attachment.  The CSV file contains new and updated opportunity records.  I would like to be able to read the file out of the email, and process the records, doing validation and upserting them to the database.


Thanks in advance!

I know it's not very "cloud"-like, but your best bet may be to write a java client to periodically read an e-mail inbox and utilise the Dataloader.jar functionality.  It's a bit like Email-to-Case.  CSV parsing is not a trivial operation and considering you'd have to right it from scratch in Apex, in my opinion, you'd be served well by looking at non-Apex alternatives.

Just my 2p,

Just to close this out, I was able to solve this issue.
I created an inbound email service that accepted binary attachements.
My code checks the inbound message for binary attachments that are of mime type vnd.ms-excel.
I then extract the body of the attachment into a string by converting it from a blob.
by parsing for '\n\r' I can pull the records apart and then I wrote a CSV splitter that will pull the records apart into fields.

I can process about 500 opportunity records in this fashion, any more an the string extracted from the blob is too large.

Not perfect, but it is working for my environment.


Hello Jim,


I am looking to hear from you again :)


I am currently working on a similar scenario. Can you please paste the code where you are splitting the CSV and creating records for each row, which you have mentioned in your post?


I am tryin that out but somehow its not working for me :( Would be really helpful if i can get something to refer to.


Thanks a Ton!!


Message Edited by Cool_Devloper on 02-10-2009 09:16 PM
Message Edited by Cool_Devloper on 02-10-2009 09:24 PM

Hello Friends,


Just to give an update, i was successful in splitting the CSV rows and then parsing these to create records using APEX.


The issue now is with the String object which i am using to store the content of my CSV file. The size of the String is 100000 characters beyond which it leads to an exception and i cannot process my parsing futher, as the file is large. Is there any workaround for this?


Can we convert it into an XML document and parse this instead. In this case, does the string limit still holds true?


Would be really great if someone can answer these queries.......


Thanks a ton!!


Message Edited by Cool_Devloper on 02-11-2009 09:46 PM

Hi Jim,


I'm trying to do essentially the same thing. I'm taking in a binary attachment and converting it to a string from the blob. How is that you parsed for \n\r? I'm in a lucky situation that I know the format of my csv file so I can code very simply for it, but in converting from a string to a list and the last entry in each row blends with the first entry of the next row since there is no comma there in the csv file. My coding is very rusty at this point and Apex is completly new to me so any help is appreciated. I can think of one way to do this is to just parse the string for /n/r replacing it with a comma, although i don't know if the /n would even exist still after being converted from the blob.






EDIT: Nevermind. I think I got it, its not the most direct method but it works fine for me. Thanks anyways.

Message Edited by RCJesse on 04-27-2009 02:39 PM
I hate to start up an old thread, but anyone willing to post some code examples? I need to do exactly this but the file is in the form of a csv. I can account for that, but I can't seem to find much documentation on how to process an attachment. Every post/blog I read is how to attach it to a record.

I'm all for code-sharing, I don't know why others try to keep their code a secret.


This is an example of my parser. You can see I am looking for both types of attachments text and binary. Once i get them into string form I can parse the attachment in the same way. At the end of this code you have a string "csvbody" which is your attachment.


global class MasterLeadUpload implements Messaging.InboundEmailHandler {


global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope){



Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();


Messaging.InboundEmail.TextAttachment[] tAttachments = email.textAttachments;

Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.BinaryAttachments;

String csvbody='';


 if(bAttachments !=null){

for(Messaging.InboundEmail.BinaryAttachment btt : bAttachments){System.debug(

'this looks like a binary attachment'); if(btt.filename.endsWith('.csv')){

csvbody = btt.body.toString();





if(tAttachments !=null){

for(Messaging.InboundEmail.TextAttachment ttt : tAttachments){

System.debug('this looks like a text attachment');

if(ttt.filename.endsWith('.csv')){ csvbody = ttt.body; 







I need to process an xml file attached in an email. Can anybody tell me how can I process that xml file contents in my controller class.