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
+4.9k Nginx : How to block user agent ?
+8.4k Golang : Convert int(year) to time.Time type
+4.3k Golang : Transform lisp or spinal case to Pascal case example
+4.8k Golang : How to capture return values from goroutines?
+7.6k Golang : Wait and sync.WaitGroup example
+61.9k Golang : How to return HTTP status code?
+5.2k Golang : Handle Palindrome string with case sensitivity and unicode
+7.2k Golang : Get login name from environment and prompt for password
+8.6k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+8.7k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+3.2k Fix Google Analytics Redundant Hostnames problem
+16.7k Golang : convert rune to integer value