Heim >Backend-Entwicklung >Golang >Analysieren Sie E-Mails mit net/mail
Ich verwende derzeit net/mail
in Golang, um E-Mails zu analysieren.
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)) }
Dies funktioniert hervorragend für Nur-Text-E-Mails. Wenn ich jedoch eine E-Mail mit der Apple-Mail-App sende, die HTML enthält, wird der folgende Text zurückgegeben:
--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>