eml

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 15 Imported by: 0

README

eml

邮件原始文件同大部分带有格式要求的文件相同,具备一颗抽象语法树结构(AST)。虽然,官方包 net/mailmime/multipart 均给出了相应格式的解析与构建,但是存在几个明显的问题:

  • 无法通过一个简单的入口直接解析邮件格式的抽象语法树
  • 在 mime/multipart 格式下的解析只提供了 qp 解码
  • 对邮件正文没有进行字符集的转化操作

本包就是针对以上问题进行的一次升级处理,让针对邮件原始文件的处理不再漏洞百出。

quick start

  • 解析 / Parse
import "github.com/x-mod/eml"

fd, err := os.Open("test/inline_attach.eml")
//checkErr

root, err := eml.Parse(fd)
//checkErr

//具体邮件头
root.From()
root.To()
root.Subject()

//头解码
eml.DecodeHeader(root.Subject())

//邮件体
root.Walk(func(n *Node) error {
        //内容
        body, err := n.Body()
		if err != nil {
			return err
		}
		defer body.Close()

        //解码
		dec, err := DecodingReader(n.ContentTransferEncoding(), body)
		if err != nil {
			return err
		}

        //text/html part
		if n.IsTextHtml() {
			log.Println("html charset", n.charset)
            //字符集转化 => utf-8
			chr, err := CharsetReader(n.charset, dec)
			if err != nil {
				return err
			}
			io.Copy(os.Stdout, chr)
            continue
		}

        //text/plain part
		if n.IsTextPlain() {
			log.Println("text charset", n.charset)
            //字符集转化 => utf-8
			chr, err := CharsetReader(n.charset, dec)
			if err != nil {
				return err
			}
			io.Copy(os.Stdout, chr)
            continue
		}

        //attachment part
		if filename, ok := n.Attachment(); ok {
			log.Println("filename", filename)
			fd, err := os.Create(filename)
			if err != nil {
				return err
			}
			defer fd.Close()
			io.Copy(fd, dec)
            continue
		}
		return nil
	})
  • 构建 / Compose

TODO

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CharsetReader func(charset string, input io.Reader) (io.Reader, error)
View Source
var MaxDataFileSize = int64(1 << 20)

Functions

func BEncodingHeader

func BEncodingHeader(s string, charset string) string

func DecodingHeader

func DecodingHeader(s string) (string, error)

func DecodingReader

func DecodingReader(enc string, r io.Reader) (io.Reader, error)

func EncodingWriter

func EncodingWriter(enc string, w io.Writer) (io.WriteCloser, error)

func NewRFC822

func NewRFC822(w io.Writer) io.Writer

func ParseAddress

func ParseAddress(addr string) (*mail.Address, error)

func ParseAddressList

func ParseAddressList(list string) ([]*mail.Address, error)

func ParseDate

func ParseDate(date string) (time.Time, error)

func ParseMediaType

func ParseMediaType(contentType string) (string, map[string]string, error)

func QEncodingHeader

func QEncodingHeader(s string, charset string) string

func RandomBoundary added in v0.1.1

func RandomBoundary() string

func TabWriter

func TabWriter(w io.Writer, sep string, l int) io.Writer

Types

type DataFile

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

A FileHeader describes a file part of a multipart request.

func ReadToDataFile

func ReadToDataFile(rd io.Reader) (*DataFile, error)

func (*DataFile) Open

func (datafile *DataFile) Open() (File, error)

Open opens and returns the FileHeader's associated File.

type File

type File interface {
	io.Reader
	io.ReaderAt
	io.Seeker
	io.Closer
}

File is an interface to access the file part of a multipart message. Its contents may be either stored in memory or on disk. If stored on disk, the File's underlying concrete type will be an *os.File.

type Node

type Node struct {
	Header textproto.MIMEHeader
	Parts  []*Node
	// contains filtered or unexported fields
}

func Parse

func Parse(rd io.Reader) (*Node, error)

func (*Node) Attachment

func (n *Node) Attachment() (string, bool)

func (*Node) Bcc

func (n *Node) Bcc() string

func (*Node) Body

func (n *Node) Body() (File, error)

func (*Node) BodyStructure added in v0.1.2

func (n *Node) BodyStructure() string

func (*Node) Cc

func (n *Node) Cc() string

func (*Node) ContentDescription

func (n *Node) ContentDescription() string

func (*Node) ContentDisposition

func (n *Node) ContentDisposition() string

func (*Node) ContentID

func (n *Node) ContentID() string

func (*Node) ContentTransferEncoding

func (n *Node) ContentTransferEncoding() string

func (*Node) ContentType

func (n *Node) ContentType() string

func (*Node) Date

func (n *Node) Date() string

func (*Node) From

func (n *Node) From() string

func (*Node) Get

func (n *Node) Get(key string) string

func (*Node) InReplyTo

func (n *Node) InReplyTo() string

func (*Node) IsInline

func (n *Node) IsInline() bool

func (*Node) IsTextHtml

func (n *Node) IsTextHtml() bool

func (*Node) IsTextPlain

func (n *Node) IsTextPlain() bool

func (*Node) MessageID

func (n *Node) MessageID() string

func (*Node) References

func (n *Node) References() string

func (*Node) ReplyTo

func (n *Node) ReplyTo() string

func (*Node) Section added in v0.1.2

func (n *Node) Section() string

func (*Node) Size added in v0.1.1

func (n *Node) Size() int64

func (*Node) Subject

func (n *Node) Subject() string

func (*Node) To

func (n *Node) To() string

func (*Node) Walk

func (n *Node) Walk(f func(n *Node) error) error

func (*Node) Write added in v0.1.1

func (n *Node) Write(wr io.Writer) error

before writing, node body already encoding data

Jump to

Keyboard shortcuts

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