carrier – petersanchez.com/x/carrier Index | Examples | Files

package carrier

import "petersanchez.com/x/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:play 

package main

import (
	"log"
	"os"

	"petersanchez.com/x/carrier"
)

func main() {
	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

Functions

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

Types

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) AddPGPKeys

func (m *Message) AddPGPKeys(keys *PGPKeys) error

AddPGPKeys sets the PGP keys for this message. If you plan to sign or encrypt the final email than this MUST be called before writing any message body. An error is raised if you try to assign keys after the body has been opened.

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 OutboxService

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

OutboxService is a simple email service to store messages in a slice for use in tests, etc.

func NewOutboxService

func NewOutboxService() *OutboxService

NewOutboxService returns an smtp service instance

func (*OutboxService) Send

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

Send match email Service interface signature

type PGPKeys

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

PGPKeys holds signer and recipient PGP keys

Example

Code:play 

package main

import (
	"log"
	"os"

	"petersanchez.com/x/carrier"
)

func main() {

	// You should handle errors properly in all instances below
	privkey, err := os.Open("privkey.asc")
	if err != nil {
		panic(err)
	}
	defer privkey.Close()

	// public key is optional. If present, the email will encrypted
	// for the recipient using the public key
	pubkey, err := os.Open("pubkey.asc")
	defer pubkey.Close()

	// Give pubkey if you want the email encrypted. If just providing
	// the private key, the email will be signed with the given key.
	keys, err := carrier.NewPGPKeys(privkey, nil)

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

	// You MUST assign keys before setting the body.
	err = msg.AddPGPKeys(keys)

	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.")
}

func NewPGPKeys

func NewPGPKeys(skey, rkey io.Reader) (*PGPKeys, error)

NewPGPKeys returns a PGPKey instance. If rkey is empty then the final email this PGPKeys instance is used for will only be signed.

type Service

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

Service interface

Source Files

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

Version
v0.2.3 (latest)
Published
Jan 14, 2025
Platform
linux/amd64
Imports
12 packages
Last checked
18 hours ago

Tools for package owners.