[SalesForce] Retrieve Inbound Email Service address in Apex

I need to create a system to allow customers to submit documents for approval automatically. The design is:

  • A button on the lead/contact page to send a request for a document.
  • That buttons sends an email asking the user to reply directly with their attachment, with the Reply-To header set to the address of the inbound email service*.
  • At the same time, the Message-ID header is saved** on the lead/contact.
  • When the lead replies, their email client automatically sets the reply to field to the inbound email service address due to the header.
  • The reply email arrives at the inbound email service and if the Message-ID*** header matches one saved on a lead, the attachments are processed and attached to that lead.

I have run in to two problems and a query here though:

1 – * – How do I get the address of the inbound email service in Apex? I can't hard code it because as soon as I deploy from a sandbox to a live environment the address will need to be changed.

2 – ** – How do I retrieve the Message-ID header? I know how to do this when working with an inbound email, but can't find any way to do it for an outbound one.

3 – *** – Does anyone know off-hand whether a Message-ID header will stay the same in a reply? I heard it would because that's how email clients group threads together.

If the answer to any of my questions is "it can't be done", what alternatives do I have?

Best Answer

As an alternative to the message header approach, you could:

  1. Place the Lead Id in the subject or body of the outbound email
  2. When the inbound email reply comes in, your email handler looks for the Lead Id and matches to the Lead in the database

If you don't like placing the LeadIds in the 'clear', you can obfuscate them similar to the way SFDC uses threadIds to identify the Case for email conversations. The Thread ID is constructed this way to give you an idea. The nice thing about threadIds is their visual format discourages an email recipient from deleting it. ThreadIds are also somewhat more reliable to look for than SObjectIds in text

Related Topic