Giter VIP home page Giter VIP logo

go-smtp's Introduction

go-smtp

godocs.io builds.sr.ht status

An ESMTP client and server library written in Go.

Features

  • ESMTP client & server implementing RFC 5321
  • Support for SMTP AUTH and PIPELINING
  • UTF-8 support for subject and message
  • LMTP support

Usage

Client

package main

import (
	"log"
	"strings"

	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"
)

func main() {
	// Setup authentication information.
	auth := sasl.NewPlainClient("", "[email protected]", "password")

	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	to := []string{"[email protected]"}
	msg := strings.NewReader("To: [email protected]\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
	err := smtp.SendMail("mail.example.com:25", auth, "[email protected]", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

If you need more control, you can use Client instead. For example, if you want to send an email via a server without TLS or auth support, you can do something like this:

package main

import (
	"log"
	"strings"

	"github.com/emersion/go-smtp"
)

func main() {
	// Setup an unencrypted connection to a local mail server.
	c, err := smtp.Dial("localhost:25")
	if err != nil {
		return err
	}
	defer c.Close()

	// Set the sender and recipient, and send the email all in one step.
	to := []string{"[email protected]"}
	msg := strings.NewReader("To: [email protected]\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n")
	err := c.SendMail("[email protected]", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

Server

package main

import (
	"errors"
	"io"
	"io/ioutil"
	"log"
	"time"

	"github.com/emersion/go-smtp"
)

// The Backend implements SMTP server methods.
type Backend struct{}

func (bkd *Backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
	return &Session{}, nil
}

// A Session is returned after EHLO.
type Session struct {
	auth bool
}

func (s *Session) AuthPlain(username, password string) error {
	if username != "username" || password != "password" {
		return smtp.ErrAuthFailed
	}
	s.auth = true
	return nil
}

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
	if !s.auth {
		return ErrAuthRequired
	}
	log.Println("Mail from:", from)
	return nil
}

func (s *Session) Rcpt(to string) error {
	if !s.auth {
		return ErrAuthRequired
	}
	log.Println("Rcpt to:", to)
	return nil
}

func (s *Session) Data(r io.Reader) error {
	if !s.auth {
		return ErrAuthRequired
	}
	if b, err := ioutil.ReadAll(r); err != nil {
		return err
	} else {
		log.Println("Data:", string(b))
	}
	return nil
}

func (s *Session) Reset() {}

func (s *Session) Logout() error {
	return nil
}

func main() {
	be := &Backend{}

	s := smtp.NewServer(be)

	s.Addr = ":1025"
	s.Domain = "localhost"
	s.ReadTimeout = 10 * time.Second
	s.WriteTimeout = 10 * time.Second
	s.MaxMessageBytes = 1024 * 1024
	s.MaxRecipients = 50
	s.AllowInsecureAuth = true

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

You can use the server manually with telnet:

$ telnet localhost 1025
EHLO localhost
AUTH PLAIN
AHVzZXJuYW1lAHBhc3N3b3Jk
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Hey <3
.

Relationship with net/smtp

The Go standard library provides a SMTP client implementation in net/smtp. However net/smtp is frozen: it's not getting any new features. go-smtp provides a server implementation and a number of client improvements.

Licence

MIT

go-smtp's People

Contributors

bradfitz avatar code-lhorn avatar drdaeman avatar eddsalkield avatar emersion avatar foxcpp avatar ggriffiths avatar grahamtonysmith avatar grimreaper avatar gusted avatar igorzhilianin avatar ikedas avatar iredmail avatar jalehotsky avatar johnnyluo avatar kayrus avatar kevinburke avatar koukas avatar ly020044 avatar lyda avatar mback2k avatar mschneider82 avatar nberlee avatar norguhtar avatar nsmith5 avatar petersanchez avatar sandeepone avatar thara avatar tomedharris avatar tuck1s 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.