Giter Site home page Giter Site logo

go-imap-both's Introduction

go-imap

GoDoc Build Status

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

go get gopkg.in/emersion/go-imap.v0

Why?

Other IMAP implementations in Go:

  • Require to make many type assertions
  • Are not idiomatic or are ugly
  • Are not pleasant to use
  • Implement a server xor a client, not both

Implemented commands

This package will implement all commands specified in the RFC. Additional commands will be available in other packages.

Command Client Client tests Server Server tests
CAPABILITY
NOOP
LOGOUT
AUTHENTICATE
LOGIN
STARTTLS
SELECT
EXAMINE
CREATE
DELETE
RENAME
SUBSCRIBE
UNSUBSCRIBE
LIST
LSUB
STATUS
APPEND
CHECK
CLOSE
EXPUNGE
SEARCH
FETCH
STORE
COPY
UID

Usage

Client

package main

import (
	"log"

	"github.com/emersion/go-imap/client"
	imap "github.com/emersion/go-imap/common"
)

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)
	go (func () {
		err = c.List("", "%", mailboxes)
	})()

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

	if 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
	seqset, _ := imap.NewSeqSet("")
	seqset.AddRange(mbox.Messages - 3, mbox.Messages)

	messages := make(chan *imap.Message, 4)
	err = c.Fetch(seqset, []string{"ENVELOPE"}, messages)
	if err != nil {
		log.Fatal(err)
	}

	for msg := range messages {
		log.Println(msg.Envelope.Subject)
	}

	log.Println("Done!")
}

Server

package main

import (
	"log"

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

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

	// Create a new server
	s, err := server.Listen(":3000", bkd)
	if err != nil {
		log.Fatal(err)
	}

	// Since we will use this server for testing only, we can allow plain text
	// authentication over unencrypted connections
	s.AllowInsecureAuth = true

	log.Println("Server listening at", s.Addr())

	// Do something else to keep the server alive
	done := make(chan bool)
	<-done
}

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

License

MIT

go-imap-both's People

Contributors

emersion avatar

Watchers

James Cloos avatar Jiajin Yin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.