Search code examples
nodemailer

Why is nodemailer corrupting my e-mail html?


I sent the following html via html with nodemailer:

<html>
    <div>
        <div>The Archimandrite<br /><img src="cid:The Archimandrite" /></div>
    </div>
</html>

But when I look at the message in Gmail with "Show Original," I see the following html:

<html>
    <div>
        <div>The Archimandrite<br /><img src=3D"cid:The Archimandrite" /></=
div>
    </div>
</html>

For some reason there's a random 3D in the middle and there's a = at the end of one line.

What's going on here and how can I make nodemailer behave normally?


Solution

  • tl;dr

    Use nodemailer's { textEncoding: 'base64' } option.

    More information

    I tried textEncoding at first, but for some reason it didn't seem to have an effect. Either I did something else wrong or I forgot to save the file before running the code, or something because it didn't work and I wrote off the option as useless.

    I tried using the libqp package, as mentioned in the question comments. This helped with the escaped characters, but not with the newline issue.

    I tried removing whitespace and replacing it with explicit \n characters.

    I tried varying line lengths for the content.

    I experimented with hard-wrapping my html to 75 characters so that it would fit and not get any = characters at all, but some of my html code required long lines.

    Then I tried making my cid tags really long by appending _xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx to the end of them, and for some reason it worked fine when I did that.

    I also tried adding in longer lines of text and those displayed properly.

    I noticed that when I ran the code through GitHub Actions, the raw e-mail said the encoding was 7bit, whereas the local runs said qouted-printable.

    After many many combinations of the above attempts, I finally came back to the textEncoding option to try it again and it worked just fine.