emlparser

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 17 Imported by: 0

README

emlparser - simple email parsing Go library

This package is based on DusanKasan/parsemail and k3a/parsemail.

This library allows for parsing an email message into a more convenient form than the net/mail provides. Where the net/mail just gives you a map of header fields and a io.Reader of its body, emlparser allows access to all the standard header fields set in RFC5322, html/text body as well as attachements/embedded content as binary streams with metadata.

Simple usage

You just parse a io.Reader that holds the email data. The returned Email struct contains all the standard email information/headers as public fields.

var reader io.Reader // this reads an email message
email, err := emlparser.Parse(reader) // returns Email struct and error
if err != nil {
    // handle error
}

fmt.Println(email.Subject)
fmt.Println(email.From)
fmt.Println(email.To)
fmt.Println(email.HTMLBody)

Retrieving attachments

Attachments are a easily accessible as Attachment type, containing their mime type, filename and data stream.

var reader io.Reader
email, err := emlparser.Parse(reader)
if err != nil {
    // handle error
}

for _, a := range(email.Attachments) {
    fmt.Println(a.Filename)
    fmt.Println(a.ContentType)
    //and read a.Data
}

Retrieving embedded files

You can access embedded files in the same way you can access attachments. They contain the mime type, data stream and content id that is used to reference them through the email.

var reader io.Reader
email, err := emlparser.Parse(reader)
if err != nil {
    // handle error
}

for _, a := range(email.EmbeddedFiles) {
    fmt.Println(a.CID)
    fmt.Println(a.ContentType)
    //and read a.Data
}

Issues

Submit the issues if you find any bug or have any suggestion.

Contribution

Fork the repo and submit pull requests.

License

This extension is licensed under the MIT License.

Documentation

Overview

Based on: https://github.com/k3a/parsemail/blob/master/parsemail.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Filename    string
	MediaType   string
	ContentType string
	Data        io.Reader
}

Attachment with filename, content type and data (as a io.Reader)

type Email

type Email struct {
	Header          mail.Header
	Subject         string
	Sender          *mail.Address
	From            []*mail.Address
	ReplyTo         []*mail.Address
	To              []*mail.Address
	Cc              []*mail.Address
	Bcc             []*mail.Address
	Date            time.Time
	MessageId       string
	InReplyTo       []string
	References      []string
	ResentFrom      []*mail.Address
	ResentSender    *mail.Address
	ResentTo        []*mail.Address
	ResentDate      time.Time
	ResentCc        []*mail.Address
	ResentBcc       []*mail.Address
	ResentMessageId string
	ContentType     string
	Content         io.Reader
	BodyHtml        string
	BodyText        string
	Attachments     []Attachment
	EmbeddedFiles   []EmbeddedFile
}

Email with fields for all the headers defined in RFC5322 with it's attachments and

func Parse

func Parse(r io.Reader) (email Email, err error)

Parse an email message read from io.Reader into parsemail.Email struct

type EmbeddedFile

type EmbeddedFile struct {
	Cid         string
	MediaType   string
	ContentType string
	Data        io.Reader
}

EmbeddedFile with content id, content type and data (as a io.Reader)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL