在电子邮件中未翻译的HTML代码(使用Golang)

huangapple go评论91阅读模式
英文:

html not translated in email golang

问题

我正在尝试使用Golang和net/smtp库发送包含HTML内容的电子邮件。但是当我发送电子邮件时,HTML内容没有被转换,我收到的电子邮件内容如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>

</head>

<body> <p>
    Hello Louis
    <a href="http://google.com/">Confirm email address</a> </p>

</body>

</html>

因为我的代码没有起作用,所以我尝试了[这个教程][1]。但是它也没有起作用。

所以,按照这个教程的示例,为什么我收到的电子邮件是字符串形式而不是格式化的HTML?

附注:我的代码与教程中的代码完全相同。

英文:

I'm actually trying to send email with html content with golang and net/smtp lib.
But when i send email, html is not translated and i receive an email like that :

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
        &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;html&gt;

&lt;/head&gt;

&lt;body&gt; &lt;p&gt;
    Hello Louis
    &lt;a href=&quot;http://google.com/&quot;&gt;Confirm email address&lt;/a&gt; &lt;/p&gt;

&lt;/body&gt;

&lt;/html&gt;

Because with my own code it did not work, i've tried with [this tutorial][1]
[1]: https://medium.com/@dhanushgopinath/sending-html-emails-using-templates-in-golang-9e953ca32f3d#.whzu9z3if
but it doesn't work either..

So with the example of this tutorial why i receive email in the form of string and not in formatted html ?

ps : My code is absolutely the same as in the tutorial

答案1

得分: 1

那个示例是错误的。在第46行,它有:

mime := "MIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\n\n"

也就是 Content-Type: text/plain。如果你发送的是 HTML 邮件,应该使用 Content-Type: text/html

英文:

That sample is wrong. On line 46, it has

mime := &quot;MIME-version: 1.0;\nContent-Type: text/plain; charset=\&quot;UTF-8\&quot;;\n\n&quot;

Namely the Content-Type: text/plain). If you're sending HTML mail, you should use Content-Type: text/html.

huangapple
  • 本文由 发表于 2016年12月26日 04:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/41324476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定