2

I have developed an email service handler in which I am noticing some some of them showing null in the plainTextBody field although there is a body present. Below is my sample code

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    try{            
       System.debug('from email address = '+email.fromAddress);
        System.debug('email subject = '+email.subject);
        System.debug('plainTextBody = '+email.plainTextBody); //this is showing blank in logs

The forwarding inbox has a copy of the email forwarded to the email handler address and I see the body is present in the email. Initially I thought it was due to HTML content in the email but I tried a couple of emails with HTML content and they seem to show the body fine(with html markup as text).

Could it be because the email was encrypted by the sender?

  • 2
    Did you also check the htmlBody? – Adrian Larson May 6 '17 at 16:38
  • This really depends on the mail client you're using, and basically is down to the client's implementation of RFC 821 and its successors. The client may choose to send an HTML-only version without text, in which case the plainTextBody will indeed be blank/null. You'll need to look at the original source to verify if there was a plain text body. – sfdcfox May 7 '17 at 0:50
  • I have tested it further and confirmed that it is due to email encryption of outlook. When email is sent to a company's outlook inbox which gets forwarded to the email handler, outlook is encrypting some emails which have sensitive data and attachments. Did anyone face this type of issue, are there any workarounds to capture body? thanks! – sfdcdev May 9 '17 at 6:02
1

This code works if plainTextBody is blank, but htmlBody is not. There is a regex express to strip all html tags from the html body. The result may not be pretty, but the text should survive.

        String plainText = '';
    if (string.isNotBlank(email.plainTextBody)) {
        try {
              plainText = email.plainTextBody.substring(0, email.plainTextBody.indexOf('<stop>'));
        }
        catch (System.StringException e) {
             plainText = email.plainTextBody;
        }          
    } 
    else if (string.isNotBlank(email.htmlBody)) {
            // Process html body only if plain text body is blank      
        try {
            plainText = email.htmlBody.substring(0, email.htmlBody.indexOf('<stop>'));
        }
        catch (System.StringException e) {
             plainText = email.htmlBody;
        }
            // Strip all html tags from html body
        Pattern htmlPattern = Pattern.compile('</?[a-z][a-z0-9]*[^<>]*>'); 
        plainText = htmlPattern.matcher(plainText).replaceAll('');                     
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.