mailbot

package module
v0.0.0-...-1cbecc5 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2017 License: MIT Imports: 24 Imported by: 0

README

Mailbot

Mailbot receives emails with IMAP and dispatch them to handlers registerd by user.

GoDoc

Example

A program that detects mail with subject "What time is it" and replies the time.

package main

import (
	"log"
	"mime"
	"net/mail"
	"strings"
	"time"

	"github.com/wxdao/mailbot"
)

func main() {
	config := &mailbot.Config{
		IMAPAddress: "imap.mail.com:993",
		IMAPUseTLS:  true,

		SMTPAddress: "smtp.mail.com:994",
        SMTPUseTLS:  true,

        IgnoreExisting: false,
        MarkSeen: true,
        UnseenOnly: true,

		User: "bot@mail.com",
		Pass: "I'm a bot.",
	}

	daemon := mailbot.NewDaemon(config)

	daemon.RegisterHandler(func(m *mailbot.Mail) {
		reply := "Not sure what you are looking for."

		text := strings.Join(m.Texts, "\n")
		if strings.Contains(strings.ToLower(m.Subject), "what time is it") {
			if strings.Contains(text, "UTC") {
				reply = time.Now().UTC().String()
			} else {
				reply = time.Now().String()
			}
		}

		header := mail.Header{}
		header["Subject"] = []string{mime.QEncoding.Encode("utf-8", "Re: "+m.Subject)}
		header["From"] = []string{config.User}
		header["Message-Id"] = []string{mailbot.GenerateMessageID(config.User)}
		header["Message-Id"] = []string{mailbot.GenerateMessageID(config.User)}
		header["In-Reply-To"] = []string{m.MessageID}
		header["To"] = []string{m.FromAddr.String()}
		if replyTo := m.Header.Get("Reply-To"); replyTo != "" {
			header["To"] = []string{replyTo}
		}
		err := daemon.SendPlainTextMail(header, reply)
		if err != nil {
			log.Println(err)
		}
	})

	for {
		if err := daemon.Serve(); err == mailbot.ErrInterrupted {
			break
		}
		log.Println(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInterrupted means the loop is interrupted by signal.
	ErrInterrupted = errors.New("interrupted")
)
View Source
var UniAddressParser = func() (parser *mail.AddressParser) {
	parser = new(mail.AddressParser)
	parser.WordDecoder = UniWordDecoder
	return
}()

UniAddressParser wraps a mail.AddressParser with multiple encodings support.

View Source
var UniWordDecoder = func() (dec *mime.WordDecoder) {
	dec = new(mime.WordDecoder)
	dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
		e, err := htmlindex.Get(charset)
		if err != nil {
			return nil, err
		}
		return e.NewDecoder().Reader(input), err
	}
	return
}()

UniWordDecoder wraps a mime.WordDecoder with multiple encodings support.

Functions

func BuildMail

func BuildMail(header mail.Header, body []byte) (msg []byte)

BuildMail combines header and body part into email payload.

func GenerateMessageID

func GenerateMessageID(user string) string

GenerateMessageID generates a unique message-id.

Types

type Config

type Config struct {
	IMAPAddress string
	IMAPUseTLS  bool

	SMTPAddress string
	SMTPUseTLS  bool

	User string
	Pass string

	// IgnoreExisting determines whether to ignore existing unseen mails.
	IgnoreExisting bool
	// MarkSeen determines whether to mark fetched mails seen.
	MarkSeen bool
	// UnseenOnly determines whether to look for unseen mails only.
	UnseenOnly bool

	Debug bool
}

Config ...

type Daemon

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

Daemon ...

func NewDaemon

func NewDaemon(config *Config) *Daemon

NewDaemon ...

func (*Daemon) RegisterHandler

func (d *Daemon) RegisterHandler(fun HandlerFunc)

RegisterHandler registers a handler.

func (*Daemon) SendMail

func (d *Daemon) SendMail(header mail.Header, body []byte) (err error)

SendMail sends an email.

func (*Daemon) SendPlainTextMail

func (d *Daemon) SendPlainTextMail(header mail.Header, text string) (err error)

SendPlainTextMail sends a simple text email.

func (*Daemon) Serve

func (d *Daemon) Serve() (err error)

Serve serves.

type HandlerFunc

type HandlerFunc func(m *Mail)

HandlerFunc ...

type Mail

type Mail struct {
	Header    mail.Header
	Result    *imap.FetchResult
	MessageID string
	InReplyTo string
	FromAddr  *mail.Address
	Subject   string
	Date      time.Time
	Texts     []string
	Parts     []*Part
}

Mail contains raw mail data and some extracted essential info from it.

type Part

type Part struct {
	Header textproto.MIMEHeader
	Data   []byte
}

Part is like multipart.Part but it provides raw data bytes.

Jump to

Keyboard shortcuts

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