mailparser

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: MIT Imports: 12 Imported by: 0

README

go-mailparser

Go lib for parsing email in simple way.

Features

  • Support parsing emails with content types of text/* and multipart/*.
  • Support parsing Chinese content, such as Chinese characters in email address aliases, email subject, and email content.
  • Support parsing email attachments.
  • Support parsing emails with content encoded in base64.
  • Support parsing email headers and email content separately, or parse them all at once.

Examle

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/bytbox/go-pop3"

	"github.com/windvalley/go-mailparser"
)

func main() {
	c, err := pop3.Dial("mail.xxx.com:110")
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := c.Quit(); err != nil {
			fmt.Println(err)
		}
	}()

	if err := c.Auth("xxx@xxx.com", "yourpassword"); err != nil {
		log.Fatal(err)
	}

	msgs, _, err := c.ListAll()
	if err != nil {
		log.Fatal(err)
	}

	for _, v := range msgs {
		msg, err := c.Retr(v)
		if err != nil {
			fmt.Println(err)
			continue
		}

		// string to io.Reader
		msgReader := strings.NewReader(msg)

		// parse email
		res, err := mailparser.Parse(msgReader)
		if err != nil {
			fmt.Println(err)
			continue
		}

		// check MailMessage
		fmt.Printf("result: %+v\n", res)

		// check attachments
		for _, v := range res.Attachments {
			// You can handle the file data (v.Data) appropriately based on the content type.
			fmt.Printf("filename: %s, content-type: %s\n", v.Filename, v.ContentType)
		}
	}
}

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment added in v0.2.0

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

Attachment mail attachment.

func ParseBody

func ParseBody(r io.Reader) (string, []*Attachment, error)

ParseBody mail message body.

type Header struct {
	From        string    `json:"From"`         // 发件人的电子邮件地址
	To          string    `json:"To"`           // 收件人的电子邮件地址
	Cc          string    `json:"Cc"`           // 抄送(Carbon Copy)的收件人的电子邮件地址
	Bcc         string    `json:"Bcc"`          // 密送(Blind Carbon Copy)的收件人的电子邮件地址
	ReplyTo     string    `json:"Reply-To"`     // 回复的电子邮件地址
	Subject     string    `json:"Subject"`      // 邮件的主题
	MessageID   string    `json:"Message-ID"`   // 邮件的唯一标识符
	ContentType string    `json:"Content-Type"` // 邮件的内容类型
	Date        time.Time `json:"Date"`         // 邮件的日期和时间
}

Header mail message header.

func ParseHeader

func ParseHeader(r io.Reader) (*Header, error)

ParseHeader mail message headers.

type MailMessage

type MailMessage struct {
	Header

	Body        string        `json:"Body"`
	Attachments []*Attachment `json:"Attachments"`
}

MailMessage mail message.

func Parse

func Parse(r io.Reader) (*MailMessage, error)

Parse mail message.

Jump to

Keyboard shortcuts

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