Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Created April 7, 2014 18:45
Show Gist options
  • Save joshbirk/10028897 to your computer and use it in GitHub Desktop.
Save joshbirk/10028897 to your computer and use it in GitHub Desktop.
ELEVATE Extra Credit: Apex Email Services
/* Various features of the Salesforce1 platform require Apex to utilize specific interfaces so the a level of
functionality can be assumed. One of these is the inbound email interface. This allows Apex to be used as an
email service which can process incoming emails.
To complete this extra credit:
1. Create an Apex class that uses the Inbound Email interface using the code below
2. Complete the acceptEmail function so that it returns an email with more information on the product
3. Add the Apex class as an email service and test it manually.
*/
global with sharing class WarehouseEmailRequest implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
WarehouseEmailRequest.acceptEmail(email.fromAddress,email.Subject);
result.success = true;
return result;
}
global static void acceptEmail(String bEmail, String mname) {
//Search for existing merchandise based on mname
//Format result that shows:
//Name
//Price
//Quantity
//Send Email back to sender with search result
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> tos = new List<String>();
if(bEmail.contains(',')) {
tos = bEmail.split(',');
} else { tos.add(bEmail); }
mail.setToAddresses(tos);
mail.setSubject ('[WAREHOUSE] Result for ' + mname);
mail.setPlainTextBody(response);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
Email Services allow administrators to define how the Apex class will interact with the real world. Many of these definitions are simple things, like if the Apex class should be allowed to accept attachments or how to handle specific error conditions, but probably the most important is the ability to delineate exactly which domains or emails can send information to the class.
Even so - it is important to note that email can be an inherently insecure means of communication, so do all proper due diligence when designing these kinds of mechanisms.
To set up the new Email Service:
1.Go to Setup | Develop | Email Services.
2.Click New Email Service.
3.For the name, enter Merchandise Search.
4.For the class name, enter WarehouseEmailRequest.
5.Enter your email or email domain in the Accept From box.
6.Click Save and New Email Address.
7.Re-renter your email or email domain in the Accept From box.
On the last page, you’ll have an email address that was randomly generated from the system. You can send that address a sample email as a test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment