mailer

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: BSD-3-Clause Imports: 7 Imported by: 2

README

codecov GoDoc Go Report Card

Mailer

Package mailer is a functional wrapper around the standard "net/smtp" and "html/template" packages.

Documentation

Overview

Package mailer is a functional wrapper around the standard "net/smtp" and "html/template" packages.

Index

Constants

View Source
const (
	// GlobalHeaders is set on every message.
	GlobalHeaders = "MIME-Version: 1.0\r\nContent-type: text/html; charset=\"UTF-8\"\r\n\r\n"
)

Variables

View Source
var Debug bool

Debug mode prints the outgoing mail before sending

Functions

This section is empty.

Types

type Header struct {
	Key    string
	Values []string
}

Header for mail message

func (Header) String

func (h Header) String() string

type Mailer

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

Mailer holds a html template, server and authentication information for efficient reuse.

func New

func New(tmpl *template.Template, addr, from string, auth smtp.Auth) *Mailer

New returns a reusable mailer. Tmpl should hold a collection of parsed templates. Addr is the hostname and port used by smtp.SendMail. For example:

"mail.host.com:587"

From is used in every subsequent SendMail invocation and set as message header. If auth is nil, connections will omit authentication.

func (*Mailer) Send

func (m *Mailer) Send(headers []Header, tmplName string, data interface{}, recipients ...string) error

Send renders the headers and named template with passed data. The rendered message is sent using smtp.SendMail, to all the recipients.

Headers keys are rendered Title cased, and the values are joined with a comma separator. Each entry becomes a CRLF separated line. For example:

{"to", []string{"test@test.mailu.io", "admin@test.mailu.io"}}

Results in:

To: test@test.mailu.io,admin@test.mailu.io

The current time is automatically appended as timestamp header.

Jump to

Keyboard shortcuts

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