Skip to content

adambg/emailparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

emailparser

Parse raw email into email struct with text/plain, text/html and attachments

Usage

Execute with go run . < email.txt

import (
    "github.com/adambg/emailparser"
)

func main() {
    in := bufio.NewReader(os.Stdin)
    rawEmail, _ := io.ReadAll(in)

    eml := emailparser.Parse([]byte(rawEmail))
    
    fmt.Println("From: ", eml.From)
    fmt.Println("To: ", eml.To)
    fmt.Println("Subject: ", eml.Subject)
    fmt.Println("Date: ", eml.Date)
    fmt.Println("BodyHtml: ", eml.BodyHtml)
    fmt.Println("BodyText: ", eml.BodyText)
    fmt.Println("ContentType: ", eml.ContentType)
    fmt.Println("Attchments: ", len(eml.Attachments))
}

The return object is this:

type email struct {
	From        string        `json:"from"`
	To          string        `json:"to"`
	Subject     string        `json:"subject"`
	Date        string        `json:"date"`
	ContentType string        `json:"contenttype"`
	BodyText    string        `json:"bodytext"`
	BodyHtml    string        `json:"bodyhtml"`
	Attachments []attachments `json:"attachments"`
	Error       error         `json:"error"`
}

type attachments struct {
	Mimetype string `json:"mimetype"`
	Filename string `json:"filename"`
	Data     []byte `json:"data"`
}

Notes

based on https://github.com/kirabou/parseMIMEemail.go

About

Parse raw email into email struct with text/plain, text/html and attachments

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages