Home > Back-end >  Parsing emails using net/mail
Parsing emails using net/mail

Time:01-18

I'm currently parsing an email using net/mail in golang.

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))
}

This works fine for plain text emails. But when I send an email using the Apple Mail app containing html, the following body was 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></pre><div cla=
ss=3D""><br class=3D""></div><img src=3D"https://u26515437.ct.sendgrid.net/=
wf/open?upn=3DDIOvMy23aag1zrlqvNJSXvalij334tYGiXBPjhGDZmVFp8I6wml2yWuZJN5Gy=
bSje8vz4sPJIshSAHwJ3q0VXXT-2Bc6PQlllUxVtR29EWnCSN5hiChQAIjXAqR6Wybp-2BX4xjr=
0G6ey9dIx77zxVAowA1r-2FRITFD4Og2jn-2FC3wCWfBUGLplPfTxygFPM8q8w0tCivLExebLwa=
m7q-2Flq-2B4-2FZM1Ekzac-2BOWr4XOH8pFo9-2B4-3D" alt=3D"" width=3D"1" height=
=3D"1" border=3D"0" style=3D"height:1px !important;width:1px !important;bor=
der-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;m=
argin-right:0 !important;margin-left:0 !important;padding-top:0 !important;=
padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !impo=
rtant;"/></body></html>
--Apple-Mail=3D_4A1E75FB-9514-439D-B922-8526851CA743--

And when sending this body to myself using SendGrid, I get the following email: email received

Something similar will happen for attachements. How do I properly parse this email so I can send it again to another email-address?

CodePudding user response:

If you want to reuse a message content in another mail (like with a redirect) it is not enough to include the body but you also need to include the mail header too.

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-type (i.e. something like multipart/mixed, multipart/related, multipart/alternative) and the boundary which separates the parts in the mail body. If this is not a multipart body the Content-Type contains the charset, which determines the text encoding used, i.e. utf-8, iso-8859-15, ....

For a non-multipart body 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, ...

  • Related