I have put together an Apex class for a Salesforce Email Service. This is taking the email that is sent to the address, taking the subject and email body and creating a record for a custom object I created. I have this functioning almost exactly how I want it to. The only issue I am up against is if I send an email with inline images, it won't display the images inline, it attaches them in the attachments section. Which is fine. Is there a way for the Apex Class, when parsing the email body, to see where an image tag is, and place it with text that says "See [image file name] in attachments below". Or is there a way to get the images to display inline in the rich text field?
Thanks!
Here is my class:
global class createAnnouncement implements
Messaging.InboundEmailHandler {
// Creates new announcement object
Announcements__c[] myAnnouncement = new Announcements__c[0];
global Messaging.InboundEmailResult handleInboundEmail(
Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result =
new Messaging.InboundEmailresult();
// Records the senders email address
String SendersEmail = envelope.fromAddress;
String AnnouncementName = email.subject;
// Retrieves and parses content from the email.
// Takes email body and puts it into a variable to place it into the body field of the new record
String AnnouncementBody = email.HtmlBody;
// Creates a new announcement from the information
// retrieved from the inbound email
try {
myAnnouncement.add(new Announcements__c(
Senders_Email__c = SendersEmail,
Name = AnnouncementName,
Body__c = AnnouncementBody));
insert myAnnouncement;
} catch (System.DmlException e) {
System.debug('ERROR: Not able to create announcement' + e);
}
// Searches the email for binary attachments and
// associates them with the new announcement record
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0; i < email.binaryAttachments.size(); i++) {
Attachment a = new Attachment(
ParentId = myAnnouncement[0].Id,
Name = email.binaryAttachments[i].filename,
Body = email.binaryAttachments[i].body);
insert a;
}
}
return result;
}
}