2

With each email, We get the email Headers, We have Content- Language field it the Header that describes the content language. I want to get the value of the Content - Language field from an incoming email on Case object.

We have InboundEmail Class and EmailMessage Class with Headers Property present in both. But I am not able to extract content language from that. Please suggest.

I have written

    Global class EmailLanguage implements Messaging.inboundEmailHandler{

 Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,  Messaging.InboundEnvelope env ) {

 Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

 //Get the header from the incoming email
       System.debug('Email Headers : ' + email.Headers);
    for (Messaging.InboundEmail.Header header : email.headers) {
      if (header.name == 'Content-Language') {
          System.debug('Email Headers : ' + header.value);
      }  
}

// Set the result to true, no need to send an email back to the user
// with an error message
 result.success = true;
 // Return the result for the Apex Email Service
 return result;
}
}

Do I need a trigger to fire it or will it automatically get fired on receiving an email.

I am not getting anything in debug logs.

2 Answers 2

2

For getting the headers, we can use the given class and this can be fired only if Apex Email Service is configured.

Setup -> Build -> Develop -> Email Services.

Configure you apex class here and you are done!

Global class EmailLanguage implements Messaging.inboundEmailHandler{

 Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,  Messaging.InboundEnvelope env ) {

 Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

 //Get the header from the incoming email
       System.debug('Email Headers : ' + email.Headers);
    for (Messaging.InboundEmail.Header header : email.headers) {
      if (header.name == 'Content-Language') {
          System.debug('Email Headers : ' + header.value);
      }  
}

// Set the result to true, no need to send an email back to the user
// with an error message
 result.success = true;
 // Return the result for the Apex Email Service
 return result;
}
}
0

I don't have such header in my inbound emails. Is this something custom you're adding to your own email messages?

In my job we rely on what externally appears to be several different addresses that end up being processed by same Apex class. These addresses have different SF running user set. It's a bit weak but I'm just going "somebody sent this email to [email protected] and the running user is somebody from Netherlands, okies, let's set preferred language to Dutch". Works good enough for my users.

Here's how I fetch headers (I don't do much with them at the moment, just save on the message):

// Messaging.InboundEmail email
// EmailMessage newEmail
// ...

if(email.headers != null && !email.headers.isEmpty()){
    List<String> temp = new List<String>();
    for(Messaging.InboundEmail.Header h : email.headers){
        temp.add(h.name + ': ' + h.value);
    }
    newEmail.headers = String.join(temp, '\n').abbreviate(32000);
}

// ...
insert newEmail;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .