Is it possible to include a style tag, either within the head tag or onto the style attribute of the body tag, send it with Messaging.sendEmail(), and have it actually send it with that styling?

I've tried both of the options I mentioned and it seems like Messaging.sendEmail() sanitizes the htmlBody value and removes certain style tags it does not like such as margin-block.

For example, I've sent the following Messaging.SingleEmailMessage object:

String html = '<html>';
html += '<body style="margin-block:0px !important;display:block;">';
html += '<p>test</p><p>tester</p><p>testing</p>';
html += '</body></html>';

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.settoAddresses(new String[] { /*your email*/ });
Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

Upon checking the html of the email, I see the following:

<body style="display: block;font-family: Verdana;font-size: 12.0px;">

It kept display: block but removed the others. Why?

  • Are you sure it's Salesforce, and not your email client?
    – sfdcfox
    Commented Jul 1, 2021 at 14:35
  • No I'm not, it could be my email clients. I've tried with multiple but the behaviors indicate it could be from the email client. Then how are we supposed to send styled emails through Apex? Commented Jul 1, 2021 at 14:50

1 Answer 1


It's definitely your mail client(s). I tested your code in my org, and then used Gmail's "view original" feature to take a look at the raw message, and I got this:

Content-Type: multipart/alternative; boundary="----=_Part_56793_897998198.1625151156936"

Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit


Brian Fear

Powered by Salesforce

<font color="red">Brian Fear</font>

Powered by Salesforce

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><body style="margin-block:0px !important;display:block;"><p>test</p><p>tester</p><p>testing</p><br><br><font color="red">Brian Fear</font>
<br>Powered by Salesforce
<br>http://www.salesforce.com/<br><img src="https://redacted.my.salesforce.com/servlet/servlet.ImageServer?oid=abc&esid=xyz&from=ext"></body></html>

(Note: reacted my image URL, but everything else is exactly as I received it.)

Curiously, in Gmail, the style isn't rendered, as you've observed:

<div style="display:block"><p>test</p><p>tester</p><p>testing</p><br><br><font color="red">Brian Fear</font>

I can only surmise that, for whatever reason, I don't see a font attribute here, nor the margin-block CSS; this clearly demonstrates that clients are going to what clients are going to do. Also, MDN suggests that not even all browsers support this particular style, so you might just be out of luck on that specific style. Other styles may or may not work, it's just going to be up to you to test clients or do some research on the Internet to figure out what will be supported by your potential clients.

You must log in to answer this question.

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