emlparser

package module
v0.0.0-...-17288a5 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2020 License: MIT Imports: 14 Imported by: 0

README

#EML(RFC5322) PARSER

example

	package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"os"
    	"path"
    	"strings"
    
    	"github.com/nelsonken/emlparser"
    )
    
    var (
    	Filename string
    )
    
    func init() {
    	if len(os.Args) < 2 || len(os.Args[1]) == 0 {
    		fmt.Println("you must set file")
    		os.Exit(255)
    	} else {
    		Filename = os.Args[1]
    	}
    }
    
    func main() {
    	emlraw, err := ioutil.ReadFile(Filename)
    	checkerr(err, "file "+Filename+" not found or can not be readed")
    
    	m, err := emlparser.Parse(emlraw)
    	checkerr(err, "failed parse file")
    
    	dir := Filename[0 : len(Filename)-len(path.Ext(Filename))] //crop extension
    
    	err = os.MkdirAll(dir, 0755)
    	checkerr(err, "failed create directory for save data")
    
    	for _, attachment := range m.Attachments {
    		err = ioutil.WriteFile(path.Join(dir, attachment.Filename), attachment.Data, 0755)
    		checkerr(err, "failed save attachment "+attachment.Filename)
    	}
    
    	if len(m.Html) > 0 {
    		err = ioutil.WriteFile(path.Join(dir, "body.html"), []byte(m.Html), 0755)
    		checkerr(err, "failed save html body")
    	}
    	if len(m.Text) > 0 {
    		err = ioutil.WriteFile(path.Join(dir, "body.txt"), []byte(m.Text), 0755)
    		checkerr(err, "failed save text body")
    	}
    
    	header := []string{m.Date.String(), m.Subject, m.From[0].Email(), m.To[0].Email()}
    	err = ioutil.WriteFile(path.Join(dir, "header.txt"), []byte(strings.Join(header, "\n")), 0755)
    	checkerr(err, "failed save headers")
    
    }
    
    func checkerr(err error, msg string) {
    	if err != nil {
    		fmt.Println(msg)
    		fmt.Println(err)
    		os.Exit(255)
    	}
    }

Documentation

Overview

Package mail implements a parser for electronic mail messages as specified in RFC5322.

We allow both CRLF and LF to be used in the input, possibly mixed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDate

func ParseDate(s string) time.Time

Types

type Address

type Address interface {
	String() string
	Name() string
	Email() string
}

func ParseAddress

func ParseAddress(bs []byte) (Address, error)

type Attachment

type Attachment struct {
	Filename string
	Data     []byte
}

type GroupAddr

type GroupAddr struct {
	// contains filtered or unexported fields
}

func (GroupAddr) Email

func (ga GroupAddr) Email() string

func (GroupAddr) Name

func (ga GroupAddr) Name() string

func (GroupAddr) String

func (ga GroupAddr) String() string
type Header struct {
	Key, Value string
}

type HeaderInfo

type HeaderInfo struct {
	FullHeaders []Header // all headers
	OptHeaders  []Header // unprocessed headers

	MessageId   string
	Id          string
	Date        time.Time
	From        []Address
	Sender      Address
	ReplyTo     []Address
	To          []Address
	Cc          []Address
	Bcc         []Address
	Subject     string
	Comments    []string
	Keywords    []string
	ContentType string

	InReply    []string
	References []string
}

type MailboxAddr

type MailboxAddr struct {
	// contains filtered or unexported fields
}

func (MailboxAddr) Email

func (ma MailboxAddr) Email() string

func (MailboxAddr) Name

func (ma MailboxAddr) Name() string

func (MailboxAddr) String

func (ma MailboxAddr) String() string

type Message

type Message struct {
	HeaderInfo
	Body        []byte
	Text        string
	Html        string
	Attachments []Attachment
	Parts       []Part
}

func Parse

func Parse(s []byte) (m Message, e error)

func Process

func Process(r RawMessage) (m Message, e error)

type Part

type Part struct {
	Type    string
	Charset string
	Data    []byte
	Headers map[string][]string
}

type RawHeader

type RawHeader struct {
	Key, Value []byte
}

type RawMessage

type RawMessage struct {
	RawHeaders []RawHeader
	Body       []byte
}

func ParseRaw

func ParseRaw(s []byte) (m RawMessage, e error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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