Parse email using net/mail

WBOY
Release: 2024-02-06 09:40:07
forward
825 people have browsed it

使用 net/mail 解析电子邮件

Question content

I am currently using net/mail in golang to parse emails.

import (
  "net/mail"
  "io"
  "strings"
)

func main() {
  email := "some email received"

  reader := strings.newreader(emailinput)
  msg, err := mail.readmessage(inputreader)
  check(err)

  body, err := io.readall(msg.body)
  check(err)

  fmt.println(string(body))
}
Copy after login

This works well for plain text emails. However, when I send an email using the apple mail app that contains html, the following body is returned:

--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
    charset=3Dus-ascii

fn main() {
  println!("Hello world!");
}


--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
    charset=3Dus-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
=3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
: space; line-break: after-white-space;" class=3D""><pre class=3D"" style=
=3D"color: rgb(209, 209, 209); background-color: rgb(0, 0, 0);">fn <span cl=
ass=3D"" style=3D"color: rgb(230, 97, 112); font-weight: bold;">main</span>=
<span class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=
=3D"" style=3D"color: rgb(210, 205, 134);">)</span> <span class=3D"" style=
=3D"color: rgb(176, 96, 176);">{</span>
  println<span class=3D"" style=3D"color: rgb(210, 205, 134);">!</span><spa=
n class=3D"" style=3D"color: rgb(210, 205, 134);">(</span><span class=3D"" =
style=3D"color: rgb(2, 208, 69);">"</span><span class=3D"" style=3D"color: =
rgb(0, 196, 196);">Hello world!</span><span class=3D"" style=3D"color: rgb(=
2, 208, 69);">"</span><span class=3D"" style=3D"color: rgb(210, 205, 134);"=
>)</span><span class=3D"" style=3D"color: rgb(176, 96, 176);">;</span>
<span class=3D"" style=3D"color: rgb(176, 96, 176);">}</span>

3D"" --Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743--
Copy after login

When using sendgrid to send this body to myself, I receive the following email:

A similar situation occurs with attachments. How do I parse this email correctly so I can send it again to another email address?


Correct answer


If you want to reuse the email content in another email (such as a redirect), it is not enough to include the body text, you also need to include the email tag. head.

Specifically, you need to include at least the original Content-Type header, which shows how the body should be interpreted. In your case it contains some multipart/* content types (i.e. things like multipart/mixed, multipart/related, multipart/alternative) as well as boundaries that separate parts in the body of the message. If this is not a multipart body, Content-Type contains the character set that determines the text encoding used, i.e. utf-8, iso-8859-15, ...

For non-multipart bodies, you also need to include the original Content-Transfer-Encoding header, which determines how the body is encoded for transfer, i.e. base64, quoted-printable, 7bit, .. .

The above is the detailed content of Parse email using net/mail. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!