imapclient

package module
v0.15.3 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 21 Imported by: 1

README

imapclient

imapclient is a helper library for speaking with IMAP4rev1 servers: list and select mailboxes, search for mails and download them.

imapdump

./cmd/imapdump is a usable example program for listing mailboxes and downloading mail in tar format.

Install

go get github.com/tgulacsi/imapclient/cmd/imapdump

Usage

imapdump -H imap.gmail.com -p 993 -U myloginname -P mysecretpassword tree %

will list all mailboxes

imapdump -H imap.gmail.com -p 993 -U myloginname -P mysecretpassword list -a Trash

will list the contents (UID, size and subject) of the Trash folder, even seen mails,

imapdump -H imap.gmail.com -p 993 -U myloginname -P mysecretpassword save -m Trash 3602

will dump the message from Trash folder with UID of 3602,

imapdump -H imap.gmail.com -p 993 -U myloginname -P mysecretpassword save -m Trash -o trash.tar

will save all the mails under Trash into trash.tar.

Documentation

Overview

Package imapclient is for listing folders, reading messages and moving them around (delete, unread, move).

Index

Constants

View Source
const (
	NoTLS    = tlsPolicy(-1)
	MaybeTLS = tlsPolicy(0)
	ForceTLS = tlsPolicy(1)
)
View Source
const LogAll = LogMask(true)

Variables

View Source
var (

	// Timeout is the client timeout - 30 seconds by default.
	Timeout = 30 * time.Second

	// TLSConfig is the client's config for DialTLS.
	// nosemgrep
	TLSConfig = tls.Config{InsecureSkipVerify: true} //nolint:gas
)
View Source
var (
	// ShortSleep is the duration which ised for sleep after successful delivery.
	ShortSleep = 1 * time.Second
	// LongSleep is the duration which used for sleep between errors and if the inbox is empty.
	LongSleep = 5 * time.Minute

	// ErrSkip from DeliverFunc means leave the message as is.
	ErrSkip = errors.New("skip move")
)

Functions

func CramAuth

func CramAuth(username, password string) sasl.Client

CramAuth returns an sasl.Client usable for CRAM-MD5 authentication.

func DeliverOne

func DeliverOne(c Client, inbox, pattern string, deliver DeliverFunc, outbox, errbox string) (int, error)

DeliverOne does one round of message reading and delivery. Does not loop. Returns the number of messages delivered.

func DeliverOneC

func DeliverOneC(ctx context.Context, c Client, inbox, pattern string, deliver DeliverFuncC, outbox, errbox string) (int, error)

DeliverOneC does one round of message reading and delivery. Does not loop. Returns the number of messages delivered.

func DeliveryLoop

func DeliveryLoop(c Client, inbox, pattern string, deliver DeliverFunc, outbox, errbox string, closeCh <-chan struct{})

DeliveryLoop periodically checks the inbox for mails with the specified pattern in the subject (or for any unseen mail if pattern == ""), tries to parse the message, and call the deliver function with the parsed message.

If deliver did not returned error, the message is marked as Seen, and if outbox is not empty, then moved to outbox. Except when the error is ErrSkip - then the message is left there as is.

deliver is called with the message, UID and hsh.

func DeliveryLoopC

func DeliveryLoopC(ctx context.Context, c Client, inbox, pattern string, deliver DeliverFuncC, outbox, errbox string) error

DeliveryLoopC periodically checks the inbox for mails with the specified pattern in the subject (or for any unseen mail if pattern == ""), tries to parse the message, and call the deliver function with the parsed message.

If deliver did not returned error, the message is marked as Seen, and if outbox is not empty, then moved to outbox. Except when the error is ErrSkip - then the message is left there as is.

deliver is called with the message, UID and hsh.

func GetLogger

func GetLogger(ctx context.Context) logr.Logger

func SetLogger added in v0.8.0

func SetLogger(lgr logr.Logger)

Types

type Client

type Client interface {
	MinClient
	Connect() error
	MoveC(ctx context.Context, msgID uint32, mbox string) error
	MarkC(ctx context.Context, msgID uint32, seen bool) error
	List(mbox, pattern string, all bool) ([]uint32, error)
	ReadTo(w io.Writer, msgID uint32) (int64, error)
	SetLogger(logr.Logger)
	SetLogMaskC(context.Context, LogMask) LogMask
}

Client interface declares the needed methods for listing messages, deleting and moving them around.

func FromServerAddress

func FromServerAddress(sa ServerAddress) Client

FromServerAddress returns a new (not connected) Client, using the ServerAddress.

func NewClient

func NewClient(host string, port int, username, password string) Client

NewClient returns a new (not connected) Client, using TLS iff port == 143.

func NewClientNoTLS

func NewClientNoTLS(host string, port int, username, password string) Client

NewClientNoTLS returns a new (not connected) Client, without TLS.

func NewClientTLS

func NewClientTLS(host string, port int, username, password string) Client

NewClientTLS returns a new (not connected) Client, using TLS.

type DeliverFunc

type DeliverFunc func(r io.ReadSeeker, uid uint32, hsh []byte) error

DeliverFunc is the type for message delivery.

r is the message data, uid is the IMAP server sent message UID, hsh is the message's hash.

type DeliverFuncC

type DeliverFuncC func(ctx context.Context, r io.ReadSeeker, uid uint32, hsh []byte) error

DeliverFuncC is the type for message delivery.

r is the message data, uid is the IMAP server sent message UID, hsh is the message's hash.

func MkDeliverFuncC

func MkDeliverFuncC(ctx context.Context, deliver DeliverFunc) DeliverFuncC

type LogMask

type LogMask bool

type Mailbox

type Mailbox struct {
	Mailbox string
	ServerAddress
}

Mailbox is the ServerAddress with Mailbox info appended.

func ParseMailbox

func ParseMailbox(s string) (Mailbox, error)

ParseMailbox parses an imaps://user:passw@host:port/mailbox URL.

func (Mailbox) Connect

func (m Mailbox) Connect(ctx context.Context) (Client, error)

func (Mailbox) String

func (m Mailbox) String() string

type MaxClient

type MaxClient struct {
	MinClient
}

func (MaxClient) Connect

func (c MaxClient) Connect() error

func (MaxClient) List

func (c MaxClient) List(mbox, pattern string, all bool) ([]uint32, error)

func (MaxClient) MarkC

func (c MaxClient) MarkC(ctx context.Context, msgID uint32, seen bool) error

func (MaxClient) MoveC

func (c MaxClient) MoveC(ctx context.Context, msgID uint32, mbox string) error

func (MaxClient) ReadTo

func (c MaxClient) ReadTo(w io.Writer, msgID uint32) (int64, error)

func (MaxClient) SetLogMaskC

func (c MaxClient) SetLogMaskC(ctx context.Context, mask LogMask) LogMask

func (MaxClient) SetLogger

func (c MaxClient) SetLogger(logger logr.Logger)

type MinClient

type MinClient interface {
	ConnectC(context.Context) error
	Close(commit bool) error
	ListC(ctx context.Context, mbox, pattern string, all bool) ([]uint32, error)
	Mailboxes(ctx context.Context, root string) ([]string, error)
	ReadToC(ctx context.Context, w io.Writer, msgID uint32) (int64, error)
	FetchArgs(ctx context.Context, what string, msgIDs ...uint32) (map[uint32]map[string][]string, error)
	Peek(ctx context.Context, w io.Writer, msgID uint32, what string) (int64, error)
	Mark(msgID uint32, seen bool) error
	Delete(msgID uint32) error
	Move(msgID uint32, mbox string) error
	SetLogMask(mask LogMask) LogMask
	SetLoggerC(ctx context.Context)
	Select(ctx context.Context, mbox string) error
	Watch(ctx context.Context) ([]uint32, error)
	WriteTo(ctx context.Context, mbox string, msg []byte, date time.Time) error
}

MinClient is the minimal required methods for a client. You can make a full Client from it by wrapping in a MaxClient.

type ServerAddress

type ServerAddress struct {
	Host                   string
	Username, Password     string
	ClientID, ClientSecret string
	Port                   uint32
	TLSPolicy              tlsPolicy
}

ServerAddress represents the server's address.

func (ServerAddress) String

func (m ServerAddress) String() string

func (ServerAddress) URL

func (m ServerAddress) URL() *url.URL

URL representation of the server address.

Directories

Path Synopsis
cmd
Package o365 implements an imap client, using Office 365 Mail REST API.
Package o365 implements an imap client, using Office 365 Mail REST API.
v2
Package imapclient is for listing folders, reading messages and moving them around (delete, unread, move).
Package imapclient is for listing folders, reading messages and moving them around (delete, unread, move).
o365
Package o365 implements an imap client, using Office 365 Mail REST API.
Package o365 implements an imap client, using Office 365 Mail REST API.
Package xoauth2 is Go library for generating XOAuth2 strings (for use in XOAUTH2 SASL auth schemes for IMAP/SMTP)
Package xoauth2 is Go library for generating XOAuth2 strings (for use in XOAUTH2 SASL auth schemes for IMAP/SMTP)

Jump to

Keyboard shortcuts

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