mailhog / MailHog

Web and API based SMTP testing
MIT License
13.87k stars 1.05k forks source link

html email rendering as plain text #22

Open adamaltman opened 9 years ago

adamaltman commented 9 years ago

When MailHog receives an email, which is html, it renders it as "plain text". I don't know why this happens, and how to render it as html.

Here is an example of part of the headers, as well as a screenshot:

Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=utf-8 

selection_218

cluongdp commented 1 year ago

I'm seeing this issue still as of 1/12/2023, although my Plain text tab doesnt even populate:

image

datmt commented 1 year ago

For people who are using javax.mail, make sure to set the content type right before Transport.send.

msg.setHeader("Content-Type", "text/html; charset=utf-8");
Transport.send(msg);

This is probably it's your mail library problem, not mailhog's problem. Make sure to debug and find out where the content type header is reset to text/plain.

Best of luck!

jeff-sweetjacket commented 1 year ago

In case it helps - I was experiencing the same using spring and JavaMailSender. Mailhog would not render the html.

The email was being sent as Content-Transfer-Encoding: quoted-printable which ends up inserting a bunch of "3D"s into the html which I initially thought was the problem. After doing some more reading I realized that quoted-printable was correct and wasn't the issue.

I finally realized there was a leftover thymeleaf tag in the html as I previously had been using thymeleaf to populate it. Looking at the source of the email in mailhog this is what the offending tag looked like:

<div th:text=3D"${subject}" th:remove=3D"tag"/>

After removing it mailhog was able to render it as html without issue. So perhaps there's a problem if your html has either:

Just a guess but at least removing the tag got the html rendering.

jeff-sweetjacket commented 1 year ago

Actually after doing a little bit more investigation I think it may be an iframe rendering issue. Mailhog (correctly) iframes the email body and inspecting the iframe in chrome developer tools shows that the html is there and being rendered inside the iframe. However, the height of the iframe is 0. So perhaps it's just chrome refusing to display some invalid html inside the iframe? Again, just a guess.

AnmolVerma404 commented 11 months ago

I was having the same issue, my HTML was not rendering but when I add space between Content-type and text/html it started rendering HTML too. Old

$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

New

$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
e-belair commented 11 months ago

Mailhog is not actively maintained, I recommend switching to https://github.com/axllent/mailpit

jeff-hrzap commented 11 months ago

Agreed with e-belair. Mailpit.