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

# README

go-imap

godocs.io builds.sr.ht status

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

Usage

Client godocs.io

package main

import (
	"log"

	"github.com/artpar/go-imap/client"
	"github.com/artpar/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 subtract 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, []imap.FetchItem{imap.FetchEnvelope}, 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 godocs.io

package main

import (
	"log"

	"github.com/artpar/go-imap/server"
	"github.com/artpar/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.

Extensions

Support for several IMAP extensions is included in go-imap itself. This includes:

Support for other extensions is provided via separate packages. See below.

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-msgauth - handle DKIM, DMARC and Authentication-Results
  • go-pgpmail - decrypting and encrypting mails with OpenPGP
  • go-sasl - sending and receiving SASL authentications
  • go-smtp - building SMTP clients and servers

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.
Package utf7 implements modified UTF-7 encoding defined in RFC 3501 section 5.1.3.

# Functions

CanonicalFlag returns the canonical form of a flag.
CanonicalMailboxName returns the canonical form of a mailbox name.
Format an address list to fields.
FormatFlagsOp returns the StoreItem that executes the flags operation op.
No description provided by the author
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.
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.
NewLockedWriter - goroutine safe writer.
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
NewSearchCriteria creates a new search criteria.
No description provided by the author
NewUntaggedResp creates a new untagged response.
No description provided by the author
No description provided by the author
Parse an address list from fields.
ParseBodySectionName parses a body section name.
ParseFlagsOp parses a flags operation from StoreItem.
ParseNamedResp attempts to parse a named data response.
ParseNumber parses a number.
No description provided by the author
ParseSeqSet returns a new SeqSet instance after parsing the set string.
ParseString parses a string, which is either a literal, a quoted string or an atom.
Convert a field list to a string list.
ReadResp reads a single response from a Reader.

# Constants

AddFlags adds new flags.
This mailbox presents all messages in the user's message store.
System message flags, defined in RFC 3501 section 2.3.2.
This mailbox is used to archive messages.
In the authenticated state, the client is authenticated and MUST select a mailbox to access before commands that affect messages will be permitted.
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.
Defined in RFC 3501 as date-text on page 83.
Defined in RFC 3501 as date-time on page 83.
System message flags, defined in RFC 3501 section 2.3.2.
System message flags, defined in RFC 3501 section 2.3.2.
This mailbox is used to hold draft messages -- typically, messages that are being composed but have not yet been sent.
Refers to the entire part, including headers.
Macros.
Items.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
List of items that can be fetched.
This mailbox presents all messages marked in some way as "important".
System message flags, defined in RFC 3501 section 2.3.2.
The presence of this attribute indicates that the mailbox has child mailboxes.
The presence of this attribute indicates that the mailbox has no child mailboxes.
Refers to the header of the part.
This mailbox attribute is a signal that the mailbox contains messages that are likely important to the user.
ImportantFlag is a message flag to signal that a message is likely important to the user.
The primary mailbox, as defined in RFC 3501 section 5.1.
This mailbox is where messages deemed to be junk mail are held.
In the logout state, the connection is being terminated.
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.
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.
System message flags, defined in RFC 3501 section 2.3.2.
RemoveFlags removes existing flags.
System message flags, defined in RFC 3501 section 2.3.2.
In a selected state, a mailbox has been selected to access.
This mailbox is used to hold copies of messages that have been sent.
SetFlags replaces existing flags by new ones.
No description provided by the author
No description provided by the author
No description provided by the author
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.
No description provided by the author
No description provided by the author
No description provided by the author
Refers to the text body of the part, omitting the header.
This mailbox is used to hold messages that have been deleted or marked for deletion.
TryCreateFlag is a special flag in MailboxStatus.PermanentFlags indicating that it is possible to create new keywords by attempting to store those flags in the mailbox.
The mailbox does not contain any additional messages since the last time the mailbox was selected.

# 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.
Underlying connection state information.
ContinuationReq is a continuation request response.
DataResp is an IMAP response containing data.
A message envelope, ie.
ErrStatusResp can be returned by a server.Handler to replace the default status response.
LiteralLengthErr is returned when the Len() of the Literal object does not match the actual length of the byte stream.
No description provided by the author
Basic mailbox info.
A mailbox status.
A message.
An IMAP reader.
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.
No description provided by the author
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
Resp is an IMAP response.
A string reader.
No description provided by the author

# Type aliases

BodyStructureWalkFunc is the type of the function called for each body structure visited by BodyStructure.Walk.
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.
A FetchItem is a message data item that can be fetched.
FlagsOp is an operation that will be applied on message flags.
A PartSpecifier specifies which parts of the MIME entity should be returned.
No description provided by the author
A StatusItem is a mailbox status data item that can be retrieved with a STATUS command.
No description provided by the author
A status response type.
A StoreItem is a message data item that can be updated.