Standard library / net/mail
mail.Message
Hold parsed headers and the remaining body stream. Bound input externally when reading untrusted messages. Expected stdout: 'demo hello\n'.
Formal Syntax & Structure
go syntaxgo
type Message struct { Header Header Body io.Reader}Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net/mail" "strings")
func main() { m := must(mail.ReadMessage(strings.NewReader("From: Ada <ada@example.invalid>\r\nSubject: demo\r\nDate: Tue, 02 Jan 2024 03:04:05 +0000\r\n\r\nhello"))) fmt.Println(m.Header.Get("Subject"), string(must(io.ReadAll(m.Body))))}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}