smtpsrvr

package module
v0.0.0-...-c8fca89 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

A SMTP Server Package With TLS

Quick Start

go get github.com/batpool/golang-smtp-server

package main

import (
	"crypto/tls"
	"errors"
	"fmt"
	"log"


	"github.com/batpool/golang-smtp-server"
)

func main() {
	handler := func(c *smtpsrv.Context) error {
		username, password, _ := c.User()
		if username != "username" || password != "satya" {
			return errors.New("invalid username or password")
		}
		return nil
	}

	cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		log.Println(err)
		return
	}

	config := &tls.Config{
		Certificates: []tls.Certificate{cert},
	}

	cfg := smtpsrv.ServerConfig{
		BannerDomain:    "mail.batpool.com",
		ListenAddr:      ":1587",
		MaxMessageBytes: 5 * 1024,
		Handler:         handler,
		TLSConfig:       config,
	}

	fmt.Println(smtpsrv.ListenAndServeTLS(&cfg))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthDisabled = errors.New("auth is disabled")
)

Functions

func ListenAndServeTLS

func ListenAndServeTLS(cfg *ServerConfig) error

func SetDefaultServerConfig

func SetDefaultServerConfig(cfg *ServerConfig)

func SplitAddress

func SplitAddress(address string) (string, string, error)

SplitAddress split the email@batpool.com to <user>@<domain>

Types

type Attachment

type Attachment struct {
	Filename    string
	ContentType string
	Data        io.Reader
}

Attachment with filename, content type and data (as a io.Reader)

type AuthFunc

type AuthFunc func(username, password string) error

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

The Backend implements SMTP server methods.

func NewBackend

func NewBackend(auther AuthFunc, handler HandlerFunc) *Backend

func (*Backend) AnonymousLogin

func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error)

AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails

func (*Backend) Login

func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error)

Login handles a login command with username and password.

type Context

type Context struct {
	// contains filtered or unexported fields
}

func (Context) From

func (c Context) From() *mail.Address

func (Context) Mailable

func (c Context) Mailable() (bool, error)

func (Context) Parse

func (c Context) Parse() (*Email, error)

func (Context) Read

func (c Context) Read(p []byte) (int, error)

func (Context) RemoteAddr

func (c Context) RemoteAddr() net.Addr

func (Context) SPF

func (c Context) SPF() (SPFResult, string, error)

func (Context) TLS

func (c Context) TLS() *tls.ConnectionState

func (Context) To

func (c Context) To() *mail.Address

func (Context) User

func (c Context) User() (string, string, error)

type Email

type Email struct {
	Header mail.Header

	Subject    string
	Sender     *mail.Address
	From       []*mail.Address
	ReplyTo    []*mail.Address
	To         []*mail.Address
	Cc         []*mail.Address
	Bcc        []*mail.Address
	Date       time.Time
	MessageID  string
	InReplyTo  []string
	References []string

	ResentFrom      []*mail.Address
	ResentSender    *mail.Address
	ResentTo        []*mail.Address
	ResentDate      time.Time
	ResentCc        []*mail.Address
	ResentBcc       []*mail.Address
	ResentMessageID string

	ContentType string
	Content     io.Reader

	HTMLBody string
	TextBody string

	Attachments   []Attachment
	EmbeddedFiles []EmbeddedFile
}

Email with fields for all the headers defined in RFC5322 with it's attachments and

func ParseEmail

func ParseEmail(r io.Reader) (email *Email, err error)

Parse an email message read from io.Reader into parsemail.Email struct

type EmbeddedFile

type EmbeddedFile struct {
	CID         string
	ContentType string
	Data        io.Reader
}

EmbeddedFile with content id, content type and data (as a io.Reader)

type HandlerFunc

type HandlerFunc func(*Context) error

type SPFResult

type SPFResult = spf.Result

type ServerConfig

type ServerConfig struct {
	ListenAddr      string
	BannerDomain    string
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	Handler         HandlerFunc
	Auther          AuthFunc
	MaxMessageBytes int
	TLSConfig       *tls.Config
}

type Session

type Session struct {
	From *mail.Address
	To   *mail.Address
	// contains filtered or unexported fields
}

A Session is returned after successful login.

func NewSession

func NewSession(state *smtp.ConnectionState, handler HandlerFunc, username, password *string) *Session

NewSession initialize a new session

func (*Session) Data

func (s *Session) Data(r io.Reader) error

func (*Session) Logout

func (s *Session) Logout() error

func (*Session) Mail

func (s *Session) Mail(from string, opts smtp.MailOptions) (err error)

func (*Session) Rcpt

func (s *Session) Rcpt(to string) (err error)

func (*Session) Reset

func (s *Session) Reset()

Jump to

Keyboard shortcuts

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