email

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 17 Imported by: 6

README

email sending library

Build Status Coverage Status Go Reference

The library is a wrapper around the stdlib net/smtp simplifying email sending. It supports authentication, SSL/TLS, user-specified SMTP servers, content-type, charset, multiple recipients and more.

Usage example:

client := email.NewSender("localhost", email.ContentType("text/html"), email.Auth("user", "pass"))
err := client.Send("<html>some content, foo bar</html>",
	email.Params{From: "me@example.com", To: []string{"to@example.com"}, Subject: "Hello world!",
		Attachments: []string{"/path/to/file1.txt", "/path/to/file2.txt"},
		InlineImages: []string{"/path/to/image1.png", "/path/to/image2.png"},
	})

options

NewSender accepts a number of options to configure the client:

  • Port: SMTP port (default: 25)
  • TLS: Use TLS SMTP (default: false)
  • STARTTLS: Use STARTTLS (default: false)
  • InsecureSkipVerify: skip certificate verification (default: false)
  • Auth(user, password): Username and password for SMTP authentication (default: empty, no authentication)
  • LoginAuth: Use LOGIN mechanism instead of PLAIN mechanism for SMTP authentication, e.g. this is relevant for Office 365 and Outlook.com
  • ContentType: Content type for the email (default: "text/plain")
  • Charset: Charset for the email (default: "utf-8")
  • TimeOut: Timeout for the SMTP connection (default: 30 seconds)
  • Log: Logger to use (default: no logging)
  • SMTP: Set custom smtp client (default: none)

See go docs for Option functions.

Options should be passed to NewSender after the mandatory first (host) parameter.

sending email

To send email user need to create a sender first and then use Send method. The method accepts two parameters:

  • email content (string)
  • parameters (email.Params)
    type Params struct {
        From            string   // From email field
        To              []string // From email field
        Subject         string   // Email subject
        UnsubscribeLink string   // POST, https://support.google.com/mail/answer/81126 -> "Use one-click unsubscribe"
        InReplyTo       string   // Identifier for email group (category), used for email grouping
        Attachments     []string // Attachments path
        InlineImages    []string // Embedding directly to email body. Autogenerated Content-Id (cid) equals to file name
    }
    

See go docs for Send function.

technical details

  • Content-Transfer-Encoding set to quoted-printable
  • Custom SMTP client (smtp.Client from stdlib) can be set by user with SMTP option. In this case it will be used instead of making a new smtp client internally.
  • Logger can be set with Log option. It should implement email.Logger interface with a single Logf(format string, args ...interface{}) method. By default, "no logging" internal logger is used. This interface is compatible with the go-pkgz/lgr logger.
  • The library has no external dependencies, except for testing. It uses the stdlib net/smtp package.
  • SSL/TLS supported with TLS option (usually on port 465) as well as with STARTTLS (usually on port 587).

limitations

This library is not intended to be used for sending a lot of massive emails with low latency requirements. The intended use case is sending simple messages, like alerts, notification and so on. For example, sending alerts from a monitoring system, or for authentication-related emails, i.e. "password reset email", "verification email", etc.

Documentation

Overview

Package email provides email sender

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Logf(format string, args ...interface{})
}

Logger is used to log errors and debug messages

type Option

type Option func(s *Sender)

Option func type

func Auth

func Auth(smtpUserName, smtpPasswd string) Option

Auth sets smtp username and password

func Charset

func Charset(charset string) Option

Charset sets content charset of the email

func ContentType

func ContentType(contentType string) Option

ContentType sets content type of the email

func InsecureSkipVerify added in v0.5.0

func InsecureSkipVerify(enabled bool) Option

InsecureSkipVerify skips certificate verification

func Log

func Log(l Logger) Option

Log sets the logger for the email package

func LoginAuth added in v0.4.0

func LoginAuth() Option

LoginAuth sets LOGIN auth method

func Port

func Port(port int) Option

Port sets SMTP port

func SMTP

func SMTP(smtp SMTPClient) Option

SMTP sets SMTP client

func STARTTLS added in v0.2.0

func STARTTLS(enabled bool) Option

STARTTLS enables STARTTLS support

func TLS

func TLS(enabled bool) Option

TLS enables TLS support

func TimeOut

func TimeOut(timeOut time.Duration) Option

TimeOut sets smtp timeout

type Params

type Params struct {
	From            string   // From email field
	To              []string // From email field
	Subject         string   // Email subject
	UnsubscribeLink string   // POST, https://support.google.com/mail/answer/81126 -> "Use one-click unsubscribe"
	InReplyTo       string   // Identifier for email group (category), used for email grouping
	Attachments     []string // Attachments path
	InlineImages    []string // InlineImages images path
}

Params contains all user-defined parameters to send emails

type SMTPClient

type SMTPClient interface {
	Mail(from string) error
	Auth(auth smtp.Auth) error
	Rcpt(to string) error
	Data() (io.WriteCloser, error)
	Quit() error
	Close() error
}

SMTPClient interface defines subset of net/smtp used by email client

type Sender

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

Sender implements email sender

func NewSender

func NewSender(smtpHost string, options ...Option) *Sender

NewSender creates email client with prepared smtp

func (*Sender) Send

func (em *Sender) Send(text string, params Params) error

Send email with given text If SMTPClient defined in Email struct it will be used, if not - new smtp.Client on each send. Always closes client on completion or failure.

func (*Sender) String added in v0.2.0

func (em *Sender) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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