petersanchez.com/carrier Index | Examples | Files

package carrier

import "petersanchez.com/carrier"

Package carrier implements sending email via various services with a single message object.

This package will panic if you pass in email addresses that are invalid. Please be sure to validate your input before passing it in.

Example

Code:

svc := carrier.NewConsoleService()
msg := carrier.NewMessage()
msg.SetFrom("me@mydomain.com").
    SetTo("recipient@theirdomain.com").
    SetCc("copy@somedomain.com")
msg.SetSubject("Sending email from Go!")

// You should handle errors properly in all instances below
file, err := os.Open("funny.jpg")
if err != nil {
    log.Fatal(err)
}
msg.AddAttachment("funny.jpg", file, "")
file.Close()

err = msg.SetBody("This is the text email body.")
err = msg.SetBodyHTML("This is the HTML email body.")

// Send email
err = svc.Send(msg)
log.Println("Successfully sent email.")

Index

Examples

Package Files

carrier.go console.go message.go send.go

func SendMail

func SendMail(to string, from string, subject string,
    text string, html string, svc Service) error

SendMail is a quick helper to send out an email using a given service

type ConsoleService

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

ConsoleService is a simple email service to print email to the set writer

func NewConsoleService

func NewConsoleService() *ConsoleService

NewConsoleService returns an smtp service instance

func (*ConsoleService) Send

func (c *ConsoleService) Send(msg *Message) error

Send match email Service interface signature

type Message

type Message struct {
    mail.Header
    // contains filtered or unexported fields
}

Message object which holds the entire email message. Each service will take a *Message object and use it to parse out the required data to send the email.

func NewMessage

func NewMessage() *Message

NewMessage returns a new *Message object

func (*Message) AddAttachment

func (m *Message) AddAttachment(filename string, data io.Reader, ctype string) error

AddAttachment Adds an attachment to the message. If ctype is blank the func will try to guess the content-type

func (*Message) Buffer

func (m *Message) Buffer() *bytes.Buffer

Buffer Returns the raw email buffer

func (*Message) Bytes

func (m *Message) Bytes() []byte

Bytes Returns full email as bytes

func (*Message) Close

func (m *Message) Close()

Close Closes the mail.Writer

func (*Message) GetAllRcpts

func (m *Message) GetAllRcpts() []string

GetAllRcpts Returns all email recipients

func (*Message) SendMail

func (m *Message) SendMail(svc Service) error

SendMail Sends the message as an email via the given service.

func (*Message) SetBcc

func (m *Message) SetBcc(address string) *Message

SetBcc Set the Bcc: header

func (*Message) SetBccCSV

func (m *Message) SetBccCSV(address string) *Message

SetBccCSV Set Bcc: header. See SetToCSV comment

func (*Message) SetBccList

func (m *Message) SetBccList(addresses []string) *Message

SetBccList Set the Bcc: header to multiple addresses

func (*Message) SetBody

func (m *Message) SetBody(body string) error

SetBody Set the text/plain part of the email

func (*Message) SetBodyHTML

func (m *Message) SetBodyHTML(body string) error

SetBodyHTML Set the text/html part of the email

func (*Message) SetCc

func (m *Message) SetCc(address string) *Message

SetCc Set the Cc: header

func (*Message) SetCcCSV

func (m *Message) SetCcCSV(address string) *Message

SetCcCSV Set Cc: header. See SetToCSV comment.

func (*Message) SetCcList

func (m *Message) SetCcList(addresses []string) *Message

SetCcList Set the Cc: address header to multiple addresses

func (*Message) SetFrom

func (m *Message) SetFrom(address string) *Message

SetFrom Set the From: header

func (*Message) SetHTML

func (m *Message) SetHTML(msg io.Reader) error

SetHTML Set the text/html part of the email, via io.Reader instance

func (*Message) SetText

func (m *Message) SetText(msg io.Reader) error

SetText Set the text/plain part of the email, via io.Reader instance

func (*Message) SetTo

func (m *Message) SetTo(address string) *Message

SetTo Set the To: header

func (*Message) SetToCSV

func (m *Message) SetToCSV(address string) *Message

SetToCSV Set the To: header from a comma separated list of emails. Ie, user1@foobar.com, user2@foobar.com, etc.

func (*Message) SetToList

func (m *Message) SetToList(addresses []string) *Message

SetToList Set the To: header to multiple addresses

func (*Message) String

func (m *Message) String() string

String Returns full email as string

type Service

type Service interface {
    Send(msg *Message) error
}

Service interface

Version
v0.1.0 (latest)
Published
Feb 3, 2022
Platform
linux/amd64
Imports
10 packages (graph)
Last checked
3 weeks ago

Tools for package owners.