mail

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2022 License: Apache-2.0 Imports: 4 Imported by: 15

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Debug - Set true to write the HTTP requests in curl to stdout.
	// Additional information will also be displayed in the errors such as
	// method operations.
	Debug = false
	// ErrEmptyBody is returned by Send when there is nobody attached to the
	// request.
	ErrEmptyBody = errors.New("error, empty body")
)

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Filename string
	Bytes    []byte
}

Attachment defines an email attachment for Go Mail. It contains useful information for sending files via the mail driver.

func (Attachment) B64

func (a Attachment) B64() string

B64 returns the base 64 encoding of the attachment.

Example
svg := `
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>`

a := Attachment{
	Filename: "circle.svg",
	Bytes:    []byte(svg),
}

fmt.Println(a.B64())
Output:

Cjxzdmcgd2lkdGg9IjEwMCIgaGVpZ2h0PSIxMDAiPgogIDxjaXJjbGUgY3g9IjUwIiBjeT0iNTAiIHI9IjQwIiBzdHJva2U9ImdyZWVuIiBzdHJva2Utd2lkdGg9IjQiIGZpbGw9InllbGxvdyIgLz4KPC9zdmc+

func (Attachment) Mime

func (a Attachment) Mime() string

Mime returns the mime type of the byte data.

Example
svg := `
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>`

a := Attachment{
	Filename: "circle.svg",
	Bytes:    []byte(svg),
}

fmt.Println(a.Mime())
Output:

image/svg+xml

type Config

type Config struct {
	URL         string
	APIKey      string
	Domain      string
	FromAddress string
	FromName    string
	Password    string
	Port        int
	Client      *http.Client
}

Config represents the configuration passed when a new client is constructed. Dependant on what driver is used, different options are required to be present.

func (*Config) Validate

func (c *Config) Validate() error

Validate runs sanity checks of a Config struct. This is run before a new client is created to ensure there are no invalid API calls.

Example
cfg := Config{}
fmt.Println(cfg.Validate())
Output:

driver requires from address

type Mailer

type Mailer interface {
	// Send accepts a mail.Transmission to send an email through a particular
	// driver/provider. Transmissions will be validated before sending.
	//
	// A mail.Response or an error will be returned. In some circumstances
	// the body and status code will be attached to the response for debugging.
	Send(t *Transmission) (Response, error)
}

Mailer defines the sender for go-mail returning a Response or error when an email is sent.

Below is an example of creating and sending a transmission:

	cfg := mail.Config{
   		URL:         "https://api.eu.sparkpost.com",
   		APIKey:      "my-key",
   		FromAddress: "hello@gophers.com",
   		FromName:    "Gopher",
	}

	mailer, err := drivers.NewSparkPost(cfg)
	if err != nil {
		log.Fatalln(err)
	}

	tx := &mail.Transmission{
 		Recipients:  []string{"hello@gophers.com"},
   		Subject:     "My email",
   		HTML:        "<h1>Hello from Go Mail!</h1>",
	}

	result, err := mailer.Send(tx)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("%+v\n", result)

type Response

type Response struct {
	StatusCode int         // e.g. 200
	Body       []byte      // e.g. {"result: success"}
	Headers    http.Header // e.g. map[X-Ratelimit-Limit:[600]]
	ID         string      // e.g "100"
	Message    string      // e.g "Email sent successfully"
}

Response represents the data passed back from a successful transmission.

type Transmission

type Transmission struct {
	Recipients  []string
	CC          []string
	BCC         []string
	Subject     string
	HTML        string
	PlainText   string
	Attachments []Attachment
	Headers     map[string]string
}

Transmission represents the JSON structure accepted by and returned from the driver's API. Recipients, HTML and a subject is required to send the email.

func (Transmission) HasAttachments

func (t Transmission) HasAttachments() bool

HasAttachments determines if there are any attachments in the transmission.

Example
t := Transmission{
	Attachments: []Attachment{
		{
			Filename: "gopher.svg",
			Bytes:    []byte("svg"),
		},
	},
}
fmt.Println(t.HasAttachments())
Output:

true

func (*Transmission) HasBCC

func (t *Transmission) HasBCC() bool

HasBCC determines if there are any BCC recipients attached to the transmission.

Example
t := Transmission{
	BCC: []string{"bcc@gophers.com"},
}
fmt.Println(t.HasBCC())
Output:

true

func (*Transmission) HasCC

func (t *Transmission) HasCC() bool

HasCC determines if there are any CC recipients attached to the transmission.

Example
t := Transmission{
	CC: []string{"cc@gophers.com"},
}
fmt.Println(t.HasCC())
Output:

true

func (*Transmission) Validate

func (t *Transmission) Validate() error

Validate runs sanity checks of a Transmission struct. This is run before any email sending to ensure there are no invalid API calls.

Example
t := Transmission{}
fmt.Println(t.Validate())
Output:

transmission requires recipients

Jump to

Keyboard shortcuts

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