Golang net/mail.Message type and ReadMessage() function example
package net/mail
Golang net/mail.Message type and ReadMessage() function usage example
package main
import (
"bytes"
"fmt"
"net/mail"
)
var emailData = `From: Adam <adam@earth.com>
To: Eve <eve@earth.com>
Subject: Do not eat the apple from that tree!
Date: Fri, 21 Nov 0000 09:55:06 -0600
Message-ID: <1234@garden.earth>
Don't listen to that snake. Don't eat the apple!
`
func main() {
msg, err := mail.ReadMessage(bytes.NewBuffer([]byte(emailData)))
if err != nil {
panic(err)
}
fmt.Println(msg.Header)
// convert io.Reader type to string
buf := new(bytes.Buffer)
buf.ReadFrom(msg.Body)
s := buf.String()
fmt.Println(s)
}
Output :
map[Date:[Fri, 21 Nov 0000 09:55:06 -0600] Message-Id:[1234@garden.earth] From:[Adam adam@earth.com] To:[Eve eve@earth.com] Subject:[Do not
eat the apple from that tree!]]
Don't listen to that snake. Don't eat the apple!
References :
Advertisement
Something interesting
Tutorials
+7.1k Golang : Set or add headers for many or different handlers
+15.2k Golang : XML to JSON example
+14.8k Golang : How to implement two-factor authentication?
+4.5k Fix fatal error: evacuation not done in time problem
+7.4k Golang : HTTP Routing with Goji example
+14k Golang : How to check if input from os.Args is integer?
+12.8k Golang : Accurate and reliable decimal calculations
+7.5k Golang : Play .WAV file from command line
+12.4k Golang : convert(cast) string to float value
+8.3k Golang : Embed secret text string into binary(executable) file
+6.4k Golang : Get today's weekday name and calculate target day distance example
+3.5k Linux/MacOSX : How to symlink a file?