“missing word in phrase: charset not supported”, when using the mail package

前端 未结 3 1840
名媛妹妹
名媛妹妹 2021-01-06 11:24

I\'m trying to parse emails and I get this kind of errors using the mail package. Is it a bug on the mail package or something I should handle myself ?

missing

3条回答
  •  清歌不尽
    2021-01-06 12:19

    I've been using github.com/jhillyerd/enmime which seems to have no trouble with this. It'll parse out both headers and body content. Given an io.Reader r:

    // Parse message body
    env, _ := enmime.ReadEnvelope(r)
    // Headers can be retrieved via Envelope.GetHeader(name).
    fmt.Printf("From: %v\n", env.GetHeader("From"))
    // Address-type headers can be parsed into a list of decoded mail.Address structs.
    alist, _ := env.AddressList("To")
    for _, addr := range alist {
      fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address)
    }
    fmt.Printf("Subject: %v\n", env.GetHeader("Subject"))
    
    // The plain text body is available as mime.Text.
    fmt.Printf("Text Body: %v chars\n", len(env.Text))
    
    // The HTML body is stored in mime.HTML.
    fmt.Printf("HTML Body: %v chars\n", len(env.HTML))
    
    // mime.Inlines is a slice of inlined attacments.
    fmt.Printf("Inlines: %v\n", len(env.Inlines))
    
    // mime.Attachments contains the non-inline attachments.
    fmt.Printf("Attachments: %v\n", len(env.Attachments))
    

提交回复
热议问题