email

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: MIT Imports: 4 Imported by: 0

README

Simple email utilities for Go

Basic example

import "github.com/ejuju/email"

func main() {
	// init smtp client
	smtpClient, err := NewClient(
		os.Getenv("SMTP_HOST"),
		os.Getenv("SMTP_USERNAME"),
		os.Getenv("SMTP_PASSWORD"),
	)
	if err != nil {
		log.Println(err)
		return
	}

	recipients := []string{
		"test1@test.com",
		"test2@test.com",
		"test3@test.com",
	}

	// send email(s)
	for _, recipient := range recipients {
		e := Email{
			From: mail.Address{
				Name:    os.Getenv("EMAIL_NAME"),
				Address: os.Getenv("EMAIL_ADDR"),
			},
			To: []mail.Address{
				mail.Address{
					Address: recipient,
				},
			},
			Subject: "This is a subject",
			Body: Body{
				HTML:      "<html><h1>This is the HTML body</h1></html>",
				PlainText: "This is a plain text fallback",
			},
		}

		err = Send(smtpClient, e)

		if err != nil {
			log.Println(err)
			return
		}
	}

	// disconnect client
	err = smtpClient.Quit()
	if err != nil {
		log.Println(err)
		return
	}
}

Providers

Works with:

  • Mailjet (SMTP relay)

Doesn't seem to work with:

  • Gmail (smtp.google.com:465 and smtp.google.com:587)

Roadmap

  • Support file attachments
  • Support Content-ID MIME field

Documentation

Overview

Package email provides utilities to connect to an SMTP server via TLS and send emails The goal of this package is to extend the smtp and mail packages from the Go standard library

This package helps you do 3 things: 1. Connecting to a SMTP server (using func email.NewClient) 2. Generating an email message string from a structured data (using struct email.Email and func email.ToMessageStr) 3. Sending emails over the same connection to the SMTP server (using func email.Send)

Index

Constants

View Source
const (
	TextPlain            = "text/plain; " + CharsetUTF8
	TextHTML             = "text/html; " + CharsetUTF8
	CharsetUTF8          = "charset=\"UTF-8\""
	MultipartAlternative = "multipart/alternative; boundary=\"" + BoundaryVal + "\""
	BoundaryVal          = "part"             // the boundary value chosen here is arbitrary but should be consistent with the email's alternative body parts
	QuotedPrintable      = "quoted-printable" //
)

Content type values

Variables

This section is empty.

Functions

func NewClient

func NewClient(host, username, password string) (*smtp.Client, error)

NewClient returns an authenticated SMTP client or an error if the process fails When successfull, make sure to call Quit on the smtp client struct after you're done sending emails to close the connection to the SMTP server

func Send

func Send(client *smtp.Client, e Email) error

Send sends the email to the client without calling client.Quit() so that the connection can be used to send several emails A call to Send should be preceded by a call to NewClient() to connect to the server and authenticate A call to Send should ultimately be followed by a call to client.Quit() so that the SMTP client can close the server connection

func ToMessageStr

func ToMessageStr(e Email) string

ToMessageStr generates the message string that will be sent to the SMTP server

Types

type Body

type Body struct {
	PlainText string
	HTML      string
}

Body holds the parts that make up the email body

type Email

type Email struct {
	From       mail.Address
	ReplyTo    mail.Address
	ReturnPath mail.Address
	To         []mail.Address
	CC         []mail.Address
	BCC        []mail.Address
	Subject    string
	Body       Body
}

Email holds necessary information to build an email message (= header fields + body)

Jump to

Keyboard shortcuts

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