function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jhartjhart 

Email Services - attached emails?

The Email services API provides the BinaryAttachment and TextAttachment objects to represent attachments to emails.

If I send an email with an email attachment (Content-Type: message/rfc822, Content-Disposition: attachment), it doesn't end up in either collection - so it's not accessible from the Email services API.

Is there any thought to support email attachments?  Ideally they would be in a form easily transformable into a new Messaging.InboundEmail object.

thanks,
john

Rasmus MenckeRasmus Mencke
What is your use case for sending an email as an attachment? Right now we are not looking at email as an attachment type.
jhartjhart
Rasmus,

We're using Email Services a bit differently than the anticipated "process automated emails sent by external companies in your fulfillment chain"-type use case that I think was the primary motivating factor for Email Services.

We've got a product line that automatically logs emails into Salesforce.  It's used by a lot of people who find the Salesforce.com Outlook plugin to be too slow/repetitive for logging emails, or who are using non-Outlook email environments.

In the current version, our email servers receive copies of sent/received email & then use the API over SOAP to log emails to contacts/leads/cases/etc.  We have a variety of instructions and plugins for different email clients and environments to make sure that our servers get a copy of all sent & received email for possible logging to Salesforce. 

We're developing a 2nd version that is Apex & Visualforce-based.  It would be great to remove our email servers from the processing chain at all and have the copies go straight to Salesforce via Email Services addresses.

However, many email clients use attachments when you create a rule to "automatically forward all received email", so our mail servers unpack & log the attached email rather than the original "envelope" mail.


Thus, our code needs to unpack the original email from the attachment and then process the attachment, rather than the "envelope" email itself.  We can continue to use our own mailservers to do this, but it would be great if Salesforce.com's API gave us the ability to get at those "message/rfc822" attachments from within Messaging.InboundEmail.

thanks,
John

InfopiaInfopia
Code:
if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) {
 for (integer i = 0 ; i < email.binaryAttachments.size() ; ++i) {
   if(email.binaryAttachments[i].mimeTypeSubType == 'message/rfc822') {
       // name is null when attaching message/rfc822 sent from Outlook 2007
       String filename = email.binaryAttachments[i].filename;
       if (filename == null) { filename = 'NoNameAttachment'; }
       String attachmentParentId;
       if (attachToEmailMessage) { attachmentParentId = newCases[0].Id;
       } else { attachmentParentId = newCases[0].Id; }
           Attachment a = new Attachment(ParentId = attachmentParentId,
             Name = filename,
           Body = email.binaryAttachments[i].body);
           insert a;
             }
 }
}

 
That should do the trick!
jhartjhart
Infopia,

When I made the original post (using the Winter '80 build, I believe), rfc/822 attachments simply didn't show up in either collection - it's as if they didn't exist.

I assume from your post that they are now appearing in the binary attachments collection?

thanks,
john

Rasmus MenckeRasmus Mencke
We do support RFC/822 as attachments in the Spring '08 release. They will be handed over to Apex Code as Binary attachments.
DavidDDavidD
Hi Rasmus,
 
Is there existing Apex Classes which can transform an attachment (which is an email) to inboundEmail object?
 
Or is there any way to parse the body of this kind of attachment? as it's mimetype is "message/rfc822".
 
We need to parse the fromAddress, message body,etc.
 
Thanks a lot!
 
Best Regards,
 
David Deng