libemail

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2019 License: BSD-3-Clause Imports: 12 Imported by: 0

README

libemail

Go Report Card Build Status GoDoc NetflixOSS Lifecycle GitHub

Project Overview

Package libemail provides utilities for composing plaintext or HTML email messages with attachments that can be sent through a typical SMTP server.

Getting Started

The libemail module can be installed like any other Go module, e.g.

$ go get oss.indeed.com/go/libemail

This is a typical example for sending a basic plaintext email to a locally running SMTP server, e.g.

m := &libemail.TextEmail{
    From:    "alice@example.com",
    To:      []string{"bob@example.com"},
    ReplyTo: "alice@example.com",
    Subject: "this is a test",
    Body:    "hello world!",
}

smtpSender := libemail.NewSMTPSender(libemail.SMTPSenderOptions{
    Address:      "localhost:10025",
    SendMailFunc: smtp.SendMail,
    Auth:         nil,
})

_ = smtpSender.Send(m)

Asking Questions

For technical questions about libemail, just file an issue in the GitHub tracker.

For questions about Open Source in Indeed Engineering, send us an email at opensource@indeed.com

Contributing

We welcome contributions! Feel free to help make libemail better.

Process
  • Open an issue and describe the desired feature / bug fix before making changes. It's useful to get a second pair of eyes before investing development effort.
  • Make the change. If adding a new feature, remember to provide tests that demonstrate the new feature works, including any error paths. If contributing a bug fix, add tests that demonstrate the erroneous behavior is fixed.
  • Open a pull request. Automated CI tests will run. If the tests fail, please make changes to fix the behavior, and repeat until the tests pass.
  • Once everything looks good, one of the indeedeng members will review the PR and provide feedback.

Maintainers

The oss.indeed.com/go/libemail module is maintained by Indeed Engineering.

While we are always busy helping people get jobs, we will try to respond to GitHub issues, pull requests, and questions within a couple of business days.

Code of Conduct

oss.indeed.com/go/libemail is governed by the Contributer Covenant v1.4.1

For more information please contact opensource@indeed.com.

License

The oss.indeed.com/go/libemail module is open source under the BSD-3-Clause license.

Documentation

Overview

Package libemail provides utilities for composing plaintext or HTML email messages with attachments that can be sent through a typical SMTP server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAttachmentAlreadySet = errors.New("attachment already exists")
	ErrMissingFromField     = errors.New("'from' field not set")
	ErrMissingToField       = errors.New("'to' field not set")
)

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Filename string
	Content  []byte
	Header   textproto.MIMEHeader
}

Attachment is a representation of an email attachment. There is currently no support for inline attachments.

type HTMLEmail

type HTMLEmail struct {
	From     string
	To       []string
	ReplyTo  string
	Subject  string
	HtmlBody string
	TextBody string
}

HTMLEmail is a representation of a html encoded email. You can specify the sender, a list of recipients, the subject, and an html encoded body as well as a default utf-8 plain text body.

func (*HTMLEmail) Compile

func (e *HTMLEmail) Compile() ([]byte, error)

func (*HTMLEmail) Recipients

func (e *HTMLEmail) Recipients() []string

func (*HTMLEmail) Sender

func (e *HTMLEmail) Sender() string

func (*HTMLEmail) String

func (e *HTMLEmail) String() string

String returns a human readable representation of a HTMLEmail.

type Message

type Message interface {
	Sender() string
	Recipients() []string
	Compile() ([]byte, error)
}

A Message represents something which can be formatted into an RFC-compatible email document, including headers, text, html, and attachments.

type MessageMock

type MessageMock struct {
	CompileMock mMessageMockCompile

	RecipientsMock mMessageMockRecipients

	SenderMock mMessageMockSender
	// contains filtered or unexported fields
}

MessageMock implements Message

func NewMessageMock

func NewMessageMock(t minimock.Tester) *MessageMock

NewMessageMock returns a mock for Message

func (*MessageMock) Compile

func (mmCompile *MessageMock) Compile() (ba1 []byte, err error)

Compile implements Message

func (*MessageMock) CompileAfterCounter

func (mmCompile *MessageMock) CompileAfterCounter() uint64

CompileAfterCounter returns a count of finished MessageMock.Compile invocations

func (*MessageMock) CompileBeforeCounter

func (mmCompile *MessageMock) CompileBeforeCounter() uint64

CompileBeforeCounter returns a count of MessageMock.Compile invocations

func (*MessageMock) MinimockCompileDone

func (m *MessageMock) MinimockCompileDone() bool

MinimockCompileDone returns true if the count of the Compile invocations corresponds the number of defined expectations

func (*MessageMock) MinimockCompileInspect

func (m *MessageMock) MinimockCompileInspect()

MinimockCompileInspect logs each unmet expectation

func (*MessageMock) MinimockFinish

func (m *MessageMock) MinimockFinish()

MinimockFinish checks that all mocked methods have been called the expected number of times

func (*MessageMock) MinimockRecipientsDone

func (m *MessageMock) MinimockRecipientsDone() bool

MinimockRecipientsDone returns true if the count of the Recipients invocations corresponds the number of defined expectations

func (*MessageMock) MinimockRecipientsInspect

func (m *MessageMock) MinimockRecipientsInspect()

MinimockRecipientsInspect logs each unmet expectation

func (*MessageMock) MinimockSenderDone

func (m *MessageMock) MinimockSenderDone() bool

MinimockSenderDone returns true if the count of the Sender invocations corresponds the number of defined expectations

func (*MessageMock) MinimockSenderInspect

func (m *MessageMock) MinimockSenderInspect()

MinimockSenderInspect logs each unmet expectation

func (*MessageMock) MinimockWait

func (m *MessageMock) MinimockWait(timeout mm_time.Duration)

MinimockWait waits for all mocked methods to be called the expected number of times

func (*MessageMock) Recipients

func (mmRecipients *MessageMock) Recipients() (sa1 []string)

Recipients implements Message

func (*MessageMock) RecipientsAfterCounter

func (mmRecipients *MessageMock) RecipientsAfterCounter() uint64

RecipientsAfterCounter returns a count of finished MessageMock.Recipients invocations

func (*MessageMock) RecipientsBeforeCounter

func (mmRecipients *MessageMock) RecipientsBeforeCounter() uint64

RecipientsBeforeCounter returns a count of MessageMock.Recipients invocations

func (*MessageMock) Sender

func (mmSender *MessageMock) Sender() (s1 string)

Sender implements Message

func (*MessageMock) SenderAfterCounter

func (mmSender *MessageMock) SenderAfterCounter() uint64

SenderAfterCounter returns a count of finished MessageMock.Sender invocations

func (*MessageMock) SenderBeforeCounter

func (mmSender *MessageMock) SenderBeforeCounter() uint64

SenderBeforeCounter returns a count of MessageMock.Sender invocations

type MessageMockCompileExpectation

type MessageMockCompileExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

MessageMockCompileExpectation specifies expectation struct of the Message.Compile

type MessageMockCompileResults

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

MessageMockCompileResults contains results of the Message.Compile

type MessageMockRecipientsExpectation

type MessageMockRecipientsExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

MessageMockRecipientsExpectation specifies expectation struct of the Message.Recipients

type MessageMockRecipientsResults

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

MessageMockRecipientsResults contains results of the Message.Recipients

type MessageMockSenderExpectation

type MessageMockSenderExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

MessageMockSenderExpectation specifies expectation struct of the Message.Sender

type MessageMockSenderResults

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

MessageMockSenderResults contains results of the Message.Sender

type SMTPAuth

type SMTPAuth interface {
	smtp.Auth
}

SMTPAuth enables generating mock for smtp.Auth

type SMTPAuthMock

type SMTPAuthMock struct {
	NextMock mSMTPAuthMockNext

	StartMock mSMTPAuthMockStart
	// contains filtered or unexported fields
}

SMTPAuthMock implements SMTPAuth

func NewSMTPAuthMock

func NewSMTPAuthMock(t minimock.Tester) *SMTPAuthMock

NewSMTPAuthMock returns a mock for SMTPAuth

func (*SMTPAuthMock) MinimockFinish

func (m *SMTPAuthMock) MinimockFinish()

MinimockFinish checks that all mocked methods have been called the expected number of times

func (*SMTPAuthMock) MinimockNextDone

func (m *SMTPAuthMock) MinimockNextDone() bool

MinimockNextDone returns true if the count of the Next invocations corresponds the number of defined expectations

func (*SMTPAuthMock) MinimockNextInspect

func (m *SMTPAuthMock) MinimockNextInspect()

MinimockNextInspect logs each unmet expectation

func (*SMTPAuthMock) MinimockStartDone

func (m *SMTPAuthMock) MinimockStartDone() bool

MinimockStartDone returns true if the count of the Start invocations corresponds the number of defined expectations

func (*SMTPAuthMock) MinimockStartInspect

func (m *SMTPAuthMock) MinimockStartInspect()

MinimockStartInspect logs each unmet expectation

func (*SMTPAuthMock) MinimockWait

func (m *SMTPAuthMock) MinimockWait(timeout mm_time.Duration)

MinimockWait waits for all mocked methods to be called the expected number of times

func (*SMTPAuthMock) Next

func (mmNext *SMTPAuthMock) Next(fromServer []byte, more bool) (toServer []byte, err error)

Next implements SMTPAuth

func (*SMTPAuthMock) NextAfterCounter

func (mmNext *SMTPAuthMock) NextAfterCounter() uint64

NextAfterCounter returns a count of finished SMTPAuthMock.Next invocations

func (*SMTPAuthMock) NextBeforeCounter

func (mmNext *SMTPAuthMock) NextBeforeCounter() uint64

NextBeforeCounter returns a count of SMTPAuthMock.Next invocations

func (*SMTPAuthMock) Start

func (mmStart *SMTPAuthMock) Start(server *smtp.ServerInfo) (proto string, toServer []byte, err error)

Start implements SMTPAuth

func (*SMTPAuthMock) StartAfterCounter

func (mmStart *SMTPAuthMock) StartAfterCounter() uint64

StartAfterCounter returns a count of finished SMTPAuthMock.Start invocations

func (*SMTPAuthMock) StartBeforeCounter

func (mmStart *SMTPAuthMock) StartBeforeCounter() uint64

StartBeforeCounter returns a count of SMTPAuthMock.Start invocations

type SMTPAuthMockNextExpectation

type SMTPAuthMockNextExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

SMTPAuthMockNextExpectation specifies expectation struct of the SMTPAuth.Next

func (*SMTPAuthMockNextExpectation) Then

func (e *SMTPAuthMockNextExpectation) Then(toServer []byte, err error) *SMTPAuthMock

Then sets up SMTPAuth.Next return parameters for the expectation previously defined by the When method

type SMTPAuthMockNextParams

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

SMTPAuthMockNextParams contains parameters of the SMTPAuth.Next

type SMTPAuthMockNextResults

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

SMTPAuthMockNextResults contains results of the SMTPAuth.Next

type SMTPAuthMockStartExpectation

type SMTPAuthMockStartExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

SMTPAuthMockStartExpectation specifies expectation struct of the SMTPAuth.Start

func (*SMTPAuthMockStartExpectation) Then

func (e *SMTPAuthMockStartExpectation) Then(proto string, toServer []byte, err error) *SMTPAuthMock

Then sets up SMTPAuth.Start return parameters for the expectation previously defined by the When method

type SMTPAuthMockStartParams

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

SMTPAuthMockStartParams contains parameters of the SMTPAuth.Start

type SMTPAuthMockStartResults

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

SMTPAuthMockStartResults contains results of the SMTPAuth.Start

type SMTPSender

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

SMTPSender is an implementation of Sender that defers to the standard library smtp package for sending a Message.

func (*SMTPSender) Send

func (s *SMTPSender) Send(m Message) error

type SMTPSenderOptions

type SMTPSenderOptions struct {
	Address      string       // e.g. localhost:10025 for postfix
	SendMailFunc SendMailFunc // e.g. smtp.SendMail
	Auth         SMTPAuth     // e.g. smtp.Auth, or nil
}

SMTPSenderOptions are used to configure an SMTPSender.

type SendMailFunc

type SendMailFunc func(addr string, a smtp.Auth, from string, to []string, msg []byte) error

SendMailFunc would typically be implemented by simply calling smtp.SendMail but is left as a configurable function for testing purposes.

type Sender

type Sender interface {
	Send(Message) error
}

Sender is an interface to be implemented by something which is able to Send a Message.

func NewSMTPSender

func NewSMTPSender(options SMTPSenderOptions) Sender

NewSMTPSender creates an SMTPSender configured with the given options.

The Address and SendMailFunc fields are required, and will cause a panic if left as zero values. The Auth fields can be left nil, if the underlying SMTP server does not have auth enabled.

type SenderMock

type SenderMock struct {
	SendMock mSenderMockSend
	// contains filtered or unexported fields
}

SenderMock implements Sender

func NewSenderMock

func NewSenderMock(t minimock.Tester) *SenderMock

NewSenderMock returns a mock for Sender

func (*SenderMock) MinimockFinish

func (m *SenderMock) MinimockFinish()

MinimockFinish checks that all mocked methods have been called the expected number of times

func (*SenderMock) MinimockSendDone

func (m *SenderMock) MinimockSendDone() bool

MinimockSendDone returns true if the count of the Send invocations corresponds the number of defined expectations

func (*SenderMock) MinimockSendInspect

func (m *SenderMock) MinimockSendInspect()

MinimockSendInspect logs each unmet expectation

func (*SenderMock) MinimockWait

func (m *SenderMock) MinimockWait(timeout mm_time.Duration)

MinimockWait waits for all mocked methods to be called the expected number of times

func (*SenderMock) Send

func (mmSend *SenderMock) Send(m1 Message) (err error)

Send implements Sender

func (*SenderMock) SendAfterCounter

func (mmSend *SenderMock) SendAfterCounter() uint64

SendAfterCounter returns a count of finished SenderMock.Send invocations

func (*SenderMock) SendBeforeCounter

func (mmSend *SenderMock) SendBeforeCounter() uint64

SendBeforeCounter returns a count of SenderMock.Send invocations

type SenderMockSendExpectation

type SenderMockSendExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

SenderMockSendExpectation specifies expectation struct of the Sender.Send

func (*SenderMockSendExpectation) Then

Then sets up Sender.Send return parameters for the expectation previously defined by the When method

type SenderMockSendParams

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

SenderMockSendParams contains parameters of the Sender.Send

type SenderMockSendResults

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

SenderMockSendResults contains results of the Sender.Send

type TextEmail

type TextEmail struct {
	From        string
	To          []string
	ReplyTo     string
	Subject     string
	Body        string
	Attachments map[string]*Attachment
}

TextEmail is a representation of a plaintext email. You can specify the sender, a list of recipients, the subject, the body, a set of attachments.

func (*TextEmail) Attach

func (e *TextEmail) Attach(filename string, content io.Reader) error

Attach adds creates an Attachment with filename and an io.Reader full of the attachment content.

Attach can be called multiple times to append more than one document.

If two attachments with the same filename are added, an error is returned.

An error is returned if the content of an attachment could not be read.

func (*TextEmail) Compile

func (e *TextEmail) Compile() ([]byte, error)

Compile returns turns the TextEmail into an RFC compatible set of bytes. If some required field is not present, an error is returned.

func (*TextEmail) Recipients

func (e *TextEmail) Recipients() []string

Recipients returns the complete list of recipients. Since TextEmail currently does not support CC or BCC, this is the same as the "To" field.

func (*TextEmail) Sender

func (e *TextEmail) Sender() string

Sender returns the configured address of the sender of the TextEmail.

func (*TextEmail) String

func (e *TextEmail) String() string

String returns a human readable representation of a TextEmail.

Jump to

Keyboard shortcuts

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