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
+6k Golang : Generate multiplication table from an integer example
+6.2k Golang : Extract XML attribute data with attr field tag example
+21.7k Golang : GORM create record or insert new record into database example
+20.4k Golang : Determine if directory is empty with os.File.Readdir() function
+5.3k PHP : See installed compiled-in-modules
+21.2k Golang : Convert(cast) string to rune and back to string example
+5.4k Golang : Intercept, inject and replay HTTP traffics from web server
+5.3k Golang : Customize scanner.Scanner to treat dash as part of identifier
+11.5k Golang : Delay or limit HTTP requests example
+7.2k Golang : Null and nil value
+15k Golang : How to check for empty array string or string?
+13.5k Golang : Verify token from Google Authenticator App