Apex Code :
global class InboundEmailHandler implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
system.debug('==================');
system.debug(string.join(email.ccAddresses,','));
system.debug(email.fromAddress);
system.debug(email.fromName);
system.debug(email.inReplyTo);
system.debug(email.messageId);
system.debug(email.plainTextBody);
system.debug(email.references);
system.debug(email.replyTo);
system.debug(email.subject);
system.debug(string.join(email.toAddresses,','));
//system.debug(email.headers);
//system.debug(email.htmlBodyIsTruncated);
//system.debug(string.join(email.textAttachments,','));
//system.debug(string.join(email.headers,','));
//system.debug(email.htmlBody);
//system.debug(string.join(email.binaryAttachments,','));
system.debug('==================');
Lead lead = new Lead();
lead.LastName =email.fromName;
lead.Description = string.join(email.ccAddresses,',') +'\n'+ email.messageId +'\n'+ email.references +'\n'+ email.replyTo +'\n'+ email.htmlBodyIsTruncated +'\n'+ string.join(email.headers,',') +'\n'+ string.join(email.binaryAttachments,',');
lead.recordtypeId='0120Y0000002qJe';
lead.Email = email.fromAddress;
lead.Company = 'N/A';
lead.HTML_Body__c = email.htmlBody;
insert lead;
Map< String, Attachment > mapAttachments = new Map< String, Attachment >();
for(Messaging.InboundEmail.BinaryAttachment bA : email.binaryAttachments) {
System.debug('binary attachment :: '+email.binaryAttachments.size());
System.debug('BA header size attachment :: '+bA.headers.size()+'---'+bA.headers);
for(integer i = 0; i < bA.headers.size(); i++) {
String headerValue = bA.headers[i].value;
System.debug('headerValue :: '+headerValue);
if(headerValue.startsWith('ii') || headerValue.startsWith('<image')) {
headerValue = headerValue.replaceAll('<', '').replaceAll('>', '');
system.debug('final header value :: '+headerValue);
mapAttachments.put(headerValue, new Attachment(Name = bA.fileName, body = bA.body,ParentId = lead.Id, ContentType = bA.mimeTypeSubType));
}
}
}
//system.debug('mapAttachments :: '+mapAttachments);
insert mapAttachments.values();
//system.debug('Attachement List :: '+mapAttachments);
for(String headerValue : mapAttachments.keySet()) {
//system.debug('headerValue List :: '+headerValue);
String refLink = '/servlet/servlet.FileDownload?file=' + mapAttachments.get(headerValue).Id;
//system.debug('refLink List :: '+refLink);
lead.HTML_Body__c = lead.HTML_Body__c.replaceAll('cid:' + headerValue, refLink);
//system.debug('replaceAll List :: '+lead.HTML_Body__c.replaceAll('cid:' + headerValue, refLink));
}
update lead;
return result;
}
}
Screenshot :