sendmail

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

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

Go to latest
Published: Aug 2, 2018 License: GPL-3.0 Imports: 12 Imported by: 0

README

Go sendmail

GoDoc Build Status

This package implements the classic method of sending emails, well known from PHP. It's stupid simple and it works not only with Sendmail, but also with other MTAs, like Postfix, sSMTP, or mhsendmail, which provide a compatible interface.

  • it separates email headers from email body,
  • encodes UTF-8 headers like Subject, From, To
  • makes it easy to use text/template
  • doesn't require any SMTP configuration,
  • can write email body to a custom io.Writer to simplify testing
  • by default, it just uses /usr/sbin/sendmail (but can be changed if need be)

Installation

go get -u github.com/meehow/sendmail

Usage

package main

import (
	"io"
	"log"
	"net/mail"

	"github.com/meehow/sendmail"
)

func main() {
	sm := sendmail.Mail{
		Subject: "Cześć",
		From:    &mail.Address{"Michał", "me@example.com"},
		To: []*mail.Address{
			{"Ktoś", "info@example.com"},
			{"Ktoś2", "info2@example.com"},
		},
	}
	io.WriteString(&sm.Text, ":)\r\n")
	if err := sm.Send(); err != nil {
		log.Println(err)
	}
}

Instead of io.WriteString, you can execute a template:

tpl := template.Must(template.New("email").Parse(`Hello {{.Name}}!`))
tpl.ExecuteTemplate(&sm.Text, "email", &struct{ Name string }{"Dominik"})

ToDo

  • HTML emails
  • multipart emails (HTML + Text)
  • attachments
  • inline attachments

Credits

This is a fork of sendmail by Michał Adamski. It adds changes documented in this PR, but so far they haven't been merged upstream.

Documentation

Overview

Package sendmail implements then classic method of sending emails, well known from PHP.

Index

Constants

View Source
const SendmailDefault = "/usr/sbin/sendmail"

SendmailDefault points to the default sendmail binary location.

Variables

This section is empty.

Functions

func Validate

func Validate(email string) error

Validate checks if email host has assigned IP address

Types

type Mail

type Mail struct {
	Subject string
	From    *mail.Address
	To      []*mail.Address
	CC      []*mail.Address
	BCC     []*mail.Address
	Header  http.Header
	Text    bytes.Buffer
	HTML    bytes.Buffer
	// contains filtered or unexported fields
}

Mail defines basic mail structure and headers

func New

func New(options ...Option) (m *Mail)

New creates a new Mail instance with the given options.

func (*Mail) AppendBCC

func (m *Mail) AppendBCC(bccAddress ...*mail.Address) *Mail

AppendBCC adds a blind carbon-copy recipient to the Mail.

func (*Mail) AppendCC

func (m *Mail) AppendCC(ccAddress ...*mail.Address) *Mail

AppendCC adds a carbon-copy recipient to the Mail.

func (*Mail) AppendTo

func (m *Mail) AppendTo(toAddress ...*mail.Address) *Mail

AppendTo adds a recipient to the Mail.

func (*Mail) Send

func (m *Mail) Send() error

Send sends an email, or prints it on stderr, when environment variable `DEBUG` is set.

func (*Mail) SetDebug

func (m *Mail) SetDebug(active bool) *Mail

SetDebug sets the debug output to stderr if active is true, else it removes the debug output. Use SetDebugOutput to set it to something else.

func (*Mail) SetDebugOutput

func (m *Mail) SetDebugOutput(w io.Writer) *Mail

SetDebugOutput sets the debug output to the given writer. If w is nil, this is equivalent to SetDebug(false).

func (*Mail) SetFrom

func (m *Mail) SetFrom(fromAddress *mail.Address) *Mail

SetFrom updates (replaces) the sender's address.

func (*Mail) SetSendmail

func (m *Mail) SetSendmail(path string, args ...string) *Mail

SetSendmail modifies the path to the sendmail binary. You can pass additional arguments, if you need to.

func (*Mail) SetSubject

func (m *Mail) SetSubject(subject string) *Mail

SetSubject sets the mail subject.

func (*Mail) WriteTo

func (m *Mail) WriteTo(wr io.Writer) (n int64, err error)

WriteTo writes headers and content of the email to io.Writer

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is used in the Mail constructor.

func Debug

func Debug(active bool) Option

Debug sets the debug output to stderr if active is true, else it removes the debug output. Use SetDebugOutput to set it to something else.

func DebugOutput

func DebugOutput(w io.Writer) Option

DebugOutput sets the debug output to the given writer. If w is nil, this is equivalent to SetDebug(false).

func From

func From(fromAddress *mail.Address) Option

From sets the sender's address.

func Sendmail

func Sendmail(path string, args ...string) Option

Sendmail modifies the path to the sendmail binary.

func Subject

func Subject(subject string) Option

Subject sets the mail subject.

func To

func To(address *mail.Address) Option

To adds a recipient to the Mail.

Jump to

Keyboard shortcuts

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