Categorygithub.com/mailgun/go-imap
modulepackage
1.0.1
Repository: https://github.com/mailgun/go-imap.git
Documentation: pkg.go.dev

# README

go-imap

GoDoc Build Status Codecov Go Report
Card Unstable Gitter chat

An IMAP4rev1 library written in Go. It can be used to build a client and/or a server.

go get github.com/mailgun/go-imap/...

Why?

Other IMAP implementations in Go:

  • Require to make many type assertions or conversions
  • Are not idiomatic or are ugly
  • Are not pleasant to use
  • Implement a server xor a client, not both
  • Don't implement unilateral updates (i.e. the server can't notify clients for new messages)
  • Do not have a good test coverage
  • Don't handle encoding and charset automatically

Usage

Client GoDoc

package main

import (
	"log"

	"github.com/mailgun/go-imap/client"
	"github.com/mailgun/go-imap"
)

func main() {
	log.Println("Connecting to server...")

	// Connect to server
	c, err := client.DialTLS("mail.example.org:993", nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Connected")

	// Don't forget to logout
	defer c.Logout()

	// Login
	if err := c.Login("username", "password"); err != nil {
		log.Fatal(err)
	}
	log.Println("Logged in")

	// List mailboxes
	mailboxes := make(chan *imap.MailboxInfo, 10)
	done := make(chan error, 1)
	go func () {
		done <- c.List("", "*", mailboxes)
	}()

	log.Println("Mailboxes:")
	for m := range mailboxes {
		log.Println("* " + m.Name)
	}

	if err := <-done; err != nil {
		log.Fatal(err)
	}

	// Select INBOX
	mbox, err := c.Select("INBOX", false)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Flags for INBOX:", mbox.Flags)

	// Get the last 4 messages
	from := uint32(1)
	to := mbox.Messages
	if mbox.Messages > 3 {
		// We're using unsigned integers here, only substract if the result is > 0
		from = mbox.Messages - 3
	}
	seqset := new(imap.SeqSet)
	seqset.AddRange(from, to)

	messages := make(chan *imap.Message, 10)
	done = make(chan error, 1)
	go func() {
		done <- c.Fetch(seqset, []string{imap.EnvelopeMsgAttr}, messages)
	}()

	log.Println("Last 4 messages:")
	for msg := range messages {
		log.Println("* " + msg.Envelope.Subject)
	}

	if err := <-done; err != nil {
		log.Fatal(err)
	}

	log.Println("Done!")
}

Server GoDoc

package main

import (
	"log"

	"github.com/mailgun/go-imap/server"
	"github.com/mailgun/go-imap/backend/memory"
)

func main() {
	// Create a memory backend
	be := memory.New()

	// Create a new server
	s := server.New(be)
	s.Addr = ":1143"
	// Since we will use this server for testing only, we can allow plain text
	// authentication over unencrypted connections
	s.AllowInsecureAuth = true

	log.Println("Starting IMAP server at localhost:1143")
	if err := s.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

You can now use telnet localhost 1143 to manually connect to the server.

Extending go-imap

Extensions

Commands defined in IMAP extensions are available in other packages. See the wiki to learn how to use them.

Server backends

Related projects

  • go-message - parsing and formatting MIME and mail messages
  • go-pgpmail - decrypting and encrypting mails with OpenPGP
  • go-sasl - sending and receiving SASL authentications
  • go-smtp - building SMTP clients and servers
  • go-dkim - creating and verifying DKIM signatures

License

MIT

# Packages

Package backend defines an IMAP server backend interface.
Package client provides an IMAP client.
Package commands implements IMAP commands defined in RFC 3501.
IMAP responses defined in RFC 3501.
Package server provides an IMAP server.
Modified UTF-7 encoding defined in RFC 3501 section 5.1.3.

# Functions

Returns the canonical form of a flag.
Returns the canonical form of a mailbox name.
Format an address list to fields.
No description provided by the author
Convert a string list to a field list.
IsParseError returns true if the provided error is a parse error produced by Reader.
Parse a body section name.
No description provided by the author
NewConn creates a new IMAP connection.
NewDebugWriter creates a new io.Writer that will write local network activity to local and remote network activity to remote.
Create a new mailbox status that will contain the specified items.
Create a new empty message that will contain the specified items.
No description provided by the author
No description provided by the author
NewSearchCriteria creates a new search criteria.
NewSeqSet returns a new SeqSet instance after parsing the set string.
No description provided by the author
Create a new untagged response.
No description provided by the author
Parse an address list from fields.
Convert a field to a number.
No description provided by the author
Convert a field list to a string list.
Read a single response from a Reader.

# Constants

AddFlags adds new flags.
Message flags, defined in RFC 3501 section 2.3.2.
IMAP4rev1 commands.
IMAP4rev1 commands.
In the authenticated state, the client is authenticated and MUST select a mailbox to access before commands that affect messages will be permitted.
Non-extensible form of BODYSTRUCTURE.
MIME body structure of the message.
IMAP4rev1 commands.
IMAP4rev1 commands.
IMAP4rev1 commands.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
Status response codes defined in RFC 3501 section 7.1.
ConnectedState is either NotAuthenticatedState, AuthenticatedState or SelectedState.
In the connecting state, the server has not yet sent a greeting and no command can be issued.
IMAP4rev1 commands.
IMAP4rev1 commands.
Defined in RFC 3501 as date-text on page 83.
Defined in RFC 3501 as date-time on page 83.
IMAP4rev1 commands.
Message flags, defined in RFC 3501 section 2.3.2.
Message flags, defined in RFC 3501 section 2.3.2.
Refers to the entire part, including headers.
The envelope structure of the message.
IMAP4rev1 commands.
IMAP4rev1 commands.
IMAP4rev1 commands.
Message flags, defined in RFC 3501 section 2.3.2.
The flags that are set for the message.
Refers to the header of the part.
The primary mailbox, as defined in RFC 3501 section 5.1.
The internal date of the message.
IMAP4rev1 commands.
IMAP4rev1 commands.
IMAP4rev1 commands.
In the logout state, the connection is being terminated.
IMAP4rev1 commands.
Mailbox status items.
Defined in RFC 3501 section 6.3.10.
Mailbox status items.
Mailbox status items.
Mailbox status items.
Mailbox status items.
Mailbox status items.
The mailbox has been marked "interesting" by the server; the mailbox probably contains messages that have been added since the last time the mailbox was selected.
Refers to the MIME Internet Message Body header.
It is not possible for any child levels of hierarchy to exist under this\ name; no child levels exist now and none can be created in the future.
IMAP4rev1 commands.
It is not possible to use this name as a selectable mailbox.
In the not authenticated state, the client MUST supply authentication credentials before most commands will be permitted.
Message flags, defined in RFC 3501 section 2.3.2.
RemoveFlags removes existing flags.
IMAP4rev1 commands.
IMAP4rev1 commands.
Message flags, defined in RFC 3501 section 2.3.2.
IMAP4rev1 commands.
In a selected state, a mailbox has been selected to access.
SetFlags replaces existing flags by new ones.
SilentOp can be appended to a FlagsOp to prevent the operation from triggering unilateral message updates.
The RFC 822 size of the message.
IMAP4rev1 commands.
IMAP4rev1 commands.
The BAD response indicates an error message from the server.
The BYE response is always untagged, and indicates that the server is about to close the connection.
The NO response indicates an operational error message from the server.
The OK response indicates an information message from the server.
The PREAUTH response is always untagged, and is one of three possible greetings at connection startup.
IMAP4rev1 commands.
IMAP4rev1 commands.
Refers to the text body of the part, omitting the header.
IMAP4rev1 commands.
The unique identifier for the message.
The mailbox does not contain any additional messages since the last time the mailbox was selected.
IMAP4rev1 commands.

# Variables

CharsetReader, if non-nil, defines a function to generate charset-conversion readers, converting from the provided charset into UTF-8.

# Structs

An address.
A body part name.
A body section name.
A body structure.
A command.
An IMAP connection.
A continuation request.
A message envelope, ie.
Basic mailbox info.
A mailbox status.
A message.
A RespHandlerFrom that forwards responses to multiple RespHandler.
An IMAP reader.
A response.
A response that can be either accepted or rejected by a handler.
SearchCriteria is a search criteria.
Seq represents a single seq-number or seq-range value (RFC 3501 ABNF).
SeqSet is used to represent a set of message sequence numbers or UIDs (see sequence-set ABNF rule).
A status response.
An IMAP writer.

# Interfaces

A value that can be converted to a command.
A literal, as defined in RFC 3501 section 4.3.
Logger is the behaviour used by server/client to report errors for accepting connections and unexpected behavior from handlers.
No description provided by the author
Handles responses from a handler.
A value that can be converted to a Resp.
A string reader.
No description provided by the author

# Type aliases

A connection state.
A function that upgrades a connection.
time.Time with a specific layout.
time.Time with a specific layout.
ErrBadSeqSet is used to report problems with the format of a sequence set value.
FlagsOp is an operation that will be applied on message flags.
A string that will be quoted.
A status response type.