+ Start a Discussion
Marc BehrMarc Behr 

How do I get an InboundEmailHandler class to handle replies?

I have created a custom Apex Messaging.InboundEmailHandler class that we are using to handle our incoming emails. The program is working as designed with one exception that I am unable to resolve.

When a message is sent out of a case, a SF reference number is attached so that any replies to the message can be added back into the case. This works OK if I were to configure a Routing Address via the Email-to-case functionality and send all replies to this new address, but what I really want to do is use the exact same local email address for both cases.

As an example, my Email Service Apex code receives messages sent to something@mydomain.com. We also use this address as our Organization-Wide reply address on messages sent from within a case. So the outgoing message has a From (and thus reply) address of something@mydomain.com and the outgoing message has a SF reference number [ ref: XXXXXXX :ref ] embedded in the subject of the message. What I need to know, is if my custom email handler finds this reference in the reply from the customer, how/where do I look up this reference (I am not sure what Object this lives in) and then have the reply added to the existing case?

At this point, my only option appears to be to create a different email address for outgoing (from Salesforce) messages, and then forward those replies to a new 'Email-to-case' address so that they are properly handled, but I would prefer not to have to do that.





You'd need to extend the class used as your "email-to-case" class.  If you're using the native email to case, you'll need a new inbound address to handle custom stuff.


We opted to split them off to make troubleshooting easier, so we have support@ (blind inbound email from customers), and support-replies@ (only for replies, will not do anything if tracking code is not found).


I implemented our inbound email handled ahead of SF's revamp of it, so what I do is a regex looking for :::{!case.id}::: in the body of the email, and I've further taken that a step ahead by handling the same for the subject line.  I then only attach emails where it found these ID's, and the case is existing.


After that, I process attachments, and attach to the case instead of the task, which I believe is the default for email to case.


If you want more granular control over email to case, you're almost always going to need to build a custom email service for the whole thing, at which point, the sky is the limit.