Update: Summer15 now supports images in emails. However it only works currently in case feed and in the "click here to view HTML version" link on the email record.
Your issue isn't with the HTML of the email, it is the embedded images. It is a long known 'limitation' of salesforce.
https://success.salesforce.com/ideaView?id=08730000000Gp9dAAC
If you need a hacky workaround, the image place holders in the email message body can be matched to image attachments based on an Id order.
Expanded explanation: (please don't do this)
The place holders come in two forms
- cid:imageName.png@asdf
- cid:asdf
If the placeholder is in the first form, then you can just match the attachment by name.
If it is in the 2nd form, the place older position is the same as the order the image attachment was inserted
List<Attachment> attachments = [
SELECT Id
, Body
, ContentType
, Name
FROM Attachment
WHERE ParentId = :email.Id
and contentType like 'image/%'
order by Id asc];
Map<String, Attachment> attachmentMap = new Map<String, Attachment>();
for (Attachment a : attachments){
attachmentMap.put(a.Name, a);
}
//list all the content-ids (cids) in the email
List<String> cidList = ....
for (Integer i = 0; i < cidList.size() && i < attachments.size(); i++){
String cid = cidList[i];
if (cid is of first form){
.... get attachment by name
Attachment a = attachmentMap.get(imageName);
} else {
.... cid is of 2nd form
Attachment a = attachments[i];
}
}
Limitation
Sadly, there is no apex email support from platform events. Per the documentation:
Sending an email message from a platform event trigger using the Messaging.SingleEmailMessage class is not supported. The email can’t be sent because the sender is the Automated Process entity, which has no email address.
Workaround
You can, however, send emails via email alert where the From Email Address is NOT "Current User's Email Address" but rather some other organization-wide email or default workflow user.
The reason, as outlined in this answer, "emails sent by the Automated Process user was "autoproc@YOURORGID" with no ".com" appended. This meant that Gmail would ignore it and the email wasn't being delivered."
In Apex, you might be able to use the SingleEmailMessage.setOrgWideEmailAddressId
method to set the org-wide email and thereby the email be sent from a user other than the automated process user.
Best Answer
You can only view the email headers if they were saved when the email was received; doing so is the responsibility of your email handler (and will increase the storage space required for each email message).
Using Email-to-Case:
Configuration for whether email headers are saved or discarded is set for each routing address. To ensure that email headers are saved, go to Setup > Customize > Cases > Email-to-Case. Click "Edit" next to your routing address(es). Then, under "Email Settings," confirm that "Save Email Headers" is checked.
Using Apex Email Services
If you want to save the headers from an incoming message, your Apex Email Service is responsible for copying the headers from the incoming
InboundEmail.headers
and saving them to anEmailMessage.Headers
in the database.Using Email2CasePremium
This is a 3rd-party app that enhances the functionality of SFDC's Email-to-Case. To save email headers, go to All Tabs > Email to Case Premium Settings > Inbound Configuration. Confirm that "Save Inbound Email Headers" is selected.