emailparser

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 10 Imported by: 0

README

emailparser

Parse raw email into email struct with text/plain, text/html and attachments
based on https://github.com/kirabou/parseMIMEemail.go

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"`
}

Documentation

Overview

Parse raw email into email struct with text/plain, text/html and attachments based on https://github.com/kirabou/parseMIMEemail.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachments added in v0.1.2

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

type Email added in v0.1.2

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"`
}

func ExtractBodyFromHtmlAttachment added in v0.1.1

func ExtractBodyFromHtmlAttachment(eml Email, attachmentID ...int) (*Email, int)

Some mail clients sends email with empty body, and the body itself comes as an HTML attachment. Check for length of email.bodyText, and if empty you can extract the body from one of the attachments. attachmentID is optional. If not set, function will search for the first attachment of content type HTML and extract the email from there. Function returns a new email and the attachment that was used to extract body (so you can discard it if you wish). If no attachment used it will return -1

func Parse

func Parse(inp []byte) *Email

Read a MIME multipart email from stdio and explode its MIME parts into separated files, one for each part.

Jump to

Keyboard shortcuts

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