pop3

package module
v0.0.0-...-72f6fae Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2014 License: MIT Imports: 9 Imported by: 0

README

go-pop3

This is a simple POP3 client package in Go language.

##Usage

if err := pop3.ReceiveMail(address, user, pass,
	func(number int, uid, data string, err error) (bool, error) {
		log.Printf("%d, %s\n", number, uid)

    // implement your own logic here

		return false, nil
	}); err != nil {
	log.Fatalf("%v\n", err)
}

or use the method that implements the command

client, err := pop3.Dial(address)

if err != nil {
	log.Fatalf("Error: %v\n", err)
}

defer func() {
	client.Quit()
	client.Close()
}()

if err = client.User(user); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

if err = client.Pass(pass); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

var count int
var size uint64

if count, size, err = client.Stat(); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

log.Printf("Count: %d, Size: %d\n", count, size)

if count, size, err = client.List(6); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

log.Printf("Number: %d, Size: %d\n", count, size)

var mis []pop3.MessageInfo

if mis, err = client.ListAll(); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

for _, mi := range mis {
	log.Printf("Number: %d, Size: %d\n", mi.Number, mi.Size)
}

var number int
var uid string

if number, uid, err = client.Uidl(6); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

log.Printf("Number: %d, Uid: %s\n", number, uid)

if mis, err = client.UidlAll(); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

for _, mi := range mis {
	log.Printf("Number: %d, Uid: %s\n", mi.Number, mi.Uid)
}

var content string

if content, err = client.Retr(8); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

log.Printf("Content:\n%s\n", content)

if err = client.Dele(6); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

if err = client.Noop(); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

if err = client.Rset(); err != nil {
	log.Printf("Error: %v\n", err)
	return
}

##License MIT License

Documentation

Overview

Package pop3 provides simple POP3 client.

Index

Constants

This section is empty.

Variables

View Source
var (
	EOF = errors.New("skip the all mail remaining")
)

Functions

func ReceiveMail

func ReceiveMail(addr, user, pass string, receiveFn ReceiveMailFunc) error

ReceiveMail connects to the server at addr, and authenticates with user and pass, and calling receiveFn for each mail.

Types

type Client

type Client struct {
	// Text is the pop3.Conn used by the Client.
	Text *Conn
	// contains filtered or unexported fields
}

A Client represents a client connection to an POP server.

func Dial

func Dial(addr string) (*Client, error)

Dial returns a new Client connected to an POP server at addr. The addr must include a port number.

func NewClient

func NewClient(conn net.Conn) (*Client, error)

NewClient returns a new Client using an existing connection.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Dele

func (c *Client) Dele(number int) error

Dele issues a DELE command to the server using the provided mail number.

func (*Client) List

func (c *Client) List(number int) (int, uint64, error)

List issues a LIST command to the server using the provided mail number and returns mail number and size.

func (*Client) ListAll

func (c *Client) ListAll() ([]MessageInfo, error)

List issues a LIST command to the server and returns array of MessageInfo.

func (*Client) Noop

func (c *Client) Noop() error

Noop issues a NOOP command to the server.

func (*Client) Pass

func (c *Client) Pass(pass string) error

Pass issues a PASS command to the server using the provided password.

func (*Client) Quit

func (c *Client) Quit() error

Quit issues a QUIT command to the server.

func (*Client) Retr

func (c *Client) Retr(number int) (string, error)

Retr issues a RETR command to the server using the provided mail number and returns mail data.

func (*Client) Rset

func (c *Client) Rset() error

Rset issues a RSET command to the server.

func (*Client) Stat

func (c *Client) Stat() (int, uint64, error)

Stat issues a STAT command to the server and returns mail count and total size.

func (*Client) Uidl

func (c *Client) Uidl(number int) (int, string, error)

Uidl issues a UIDL command to the server using the provided mail number and returns mail number and unique id.

func (*Client) UidlAll

func (c *Client) UidlAll() ([]MessageInfo, error)

Uidl issues a UIDL command to the server and returns array of MessageInfo.

func (*Client) User

func (c *Client) User(user string) error

User issues a USER command to the server using the provided user name.

type Conn

type Conn struct {
	Reader
	Writer
	// contains filtered or unexported fields
}

A Conn represents a textual network protocol connection for POP3.

func NewConn

func NewConn(conn io.ReadWriteCloser) *Conn

NewConn returns a new Conn using conn for I/O.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

type MessageInfo

type MessageInfo struct {
	Number int
	Size   uint64
	Uid    string
}

MessageInfo has Number, Size, and Uid fields, and used as a return value of ListAll and UidlAll. When used as the return value of the method ListAll, MessageInfo contain only the Number and Size values. When used as the return value of the method UidlAll, MessageInfo contain only the Number and Uid values.

type Reader

type Reader struct {
	R *textproto.Reader
}

A Reader implements convenience methods for reading requests or responses from a text protocol network connection.

func NewReader

func NewReader(r *bufio.Reader) *Reader

NewReader returns a new Reader reading from r.

func (*Reader) ReadLine

func (r *Reader) ReadLine() (string, error)

ReadLine reads a single line from r, eliding the final \n or \r\n from the returned string. This calls textproto.Reader.ReadLine simply.

func (*Reader) ReadLines

func (r *Reader) ReadLines() ([]string, error)

ReadLines reads a multiline until the last line of the only period, and returns a each line at slice. it does not contain last period.

func (*Reader) ReadResponse

func (r *Reader) ReadResponse() (string, error)

ReadResponse reads a single line from r, and parses reponse. if the response is -ERR or has some other errors, it returns error.

func (*Reader) ReadToPeriod

func (r *Reader) ReadToPeriod() (string, error)

ReadToPeriod reads a multiline until the last line of the only period, and returns as a string. it does not contain last period.

type ReceiveMailFunc

type ReceiveMailFunc func(number int, uid, data string, err error) (bool, error)

ReceiveMailFunc is the type of the function called for each mail. Its arguments are mail's number, uid, data, and mail receiving error. if this function returns false value, the mail will be deleted, if its returns EOF, skip the all mail of remaining. (after deleting mail, if necessary)

type ResponseError

type ResponseError string

A ResponseError describes a protocol violation such as an invalid response or a hung-up connection.

func (ResponseError) Error

func (r ResponseError) Error() string

type Writer

type Writer struct {
	W *bufio.Writer
}

A Writer implements convenience methods for writing requests or responses to a text protocol network connection.

func NewWriter

func NewWriter(w *bufio.Writer) *Writer

NewWriter returns a new Writer writing to w.

func (*Writer) WriteLine

func (w *Writer) WriteLine(format string, args ...interface{}) error

WriteLine writes the formatted output followed by \r\n.

Jump to

Keyboard shortcuts

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