smtpsrv

package module
v0.0.0-...-8d4ba37 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 17 Imported by: 4

README

A SMTP Server Package

a simple smtp server library for writing email servers like a boss.

Quick Start

go get github.com/alash3al/go-smtpsrv

package main

import (
	"fmt"

	"github.com/alash3al/go-smtpsrv/v3"
)

func main() {
	handler := func(c smtpsrv.Context) error {
		// ...
		return nil
	}

	cfg := smtpsrv.ServerConfig{
		BannerDomain:  "mail.my.server",
		ListenAddr: ":25025",
		MaxMessageBytes: 5 * 1024,
		Handler:     handler,
	}

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

Thanks

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func ListenAndServe

func ListenAndServe(cfg *ServerConfig) error

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@addre.ss 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