mailing

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 12 Imported by: 2

README

A Golang package for sending emails using SMTP, SparkPost, SendGrid and MailGun

Build Status Test Status Go Report Card GoDoc Coverage Status

Features

  • Multiple File attachments
  • Multiple recipients
  • Multiple CC
  • Multiple BCC
  • HTML content type support
  • Plain Text content type support
  • Easy integration with smtp4dev testing server for development
  • Multiple Drivers Support: SMTP, SparkPost, SendGrid and MailGun

Roadmap

  • SMTP Driver
  • SparkPost Driver
  • SendGrid Driver
  • MailGun Driver

Install

Here is how to add it to your project

go get github.com/harranali/mailing

Usage

Here is how to use it

// Initiate the package with SMTP Driver
mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
		Host:     "localhost", //the SMTP server host
		Port:     25, // The Port
		Username: "", 
		Password: "",
		TLSConfig: tls.Config{
			ServerName:         "localhost",
			InsecureSkipVerify: true, // (use true for development only) true accepts any certificate presented by the server
		},
	})

// Set the Sender email address
mailer.SetFrom(mailing.EmailAddress{
        Name: "from name", // name can be set to empty string ("")
        Address: "from@mail.com"
    })

// Set the Recipients email addresses
mailer.SetTo([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "first to name", Address: "theFirstTo@mail.com"},
        {Name: "second to name", Address: "theSecondTo@mail.com"},
    })

// Set CC email addresses
mailer.SetCC([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "cc name", Address: "cc1@mail.com"},
        {Name: "cc name", Address: "cc2@mail.com"},
    })

// Set BCC email addresses
mailer.SetBCC([]mailing.EmailAddress{
        // name can be set to empty string ("")
        {Name: "bcc name", Address: "bcc@mail.com"},
    })

// Set the subject
mailer.SetSubject("This is the subject")

// Set the body (pick one: either HTML or Plain Text)
mailer.SetHTMLBody("<h1>This is the email body</h1>")
// OR
mailer.SetPlainTextBody("This is the email body")

// Set the sttachments files
mailer.SetAttachments([]mailing.Attachment{
        {
            Name: "first file",
            Path: "./myfiles/first-file.jpg",
        },
        {
            Name: "second file",
            Path: "./myfiles/second-file.pdf",
        },
    })
        
// Send the email
err := mailer.Send()
if err != nil {
    panic(err.Error())
}

Testing you emails with smtp4dev SMTP Testing Server

While developing your app you might need to test your emails, for that a customized docker-compose.yaml from the SMTP testing server smtp4dev is included.

Running the testing server

Copy the docker-compose.yaml to your pc, then start the container by running

docker-compose up
The testing server configuration

Here is how to connect to the testing server

mailer := mailing.NewMailerWithSMTP(&mailing.SMTPConfig{
		Host:     "localhost", //the SMTP server host
		Port:     25, // The Port
		Username: "", 
		Password: "",
		TLSConfig: tls.Config{
			ServerName:         "localhost",
			InsecureSkipVerify: true, // (use true for development only) true accepts any certificate presented by the server
		},
	})
Accessing the testing server UI

The testing server UI allows you to check the emails, here is a link to the ui of the testing server

http://localhost:5000

smtp4dev server ui

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Name string // name of the file
	Path string // full path to the file
}

type Driver

type Driver interface {
	Send() error
	SetFrom(from mail.Address) *smtpDriver
	SetTo(toList []mail.Address) *smtpDriver
	SetCC(ccList []mail.Address) *smtpDriver
	SetBCC(bccList []mail.Address) *smtpDriver
	SetSubject(subject string) *smtpDriver
	SetHTMLBody(body string) *smtpDriver
	SetPlainTextBody(body string) *smtpDriver
	SetAttachments(attachments []Attachment) *smtpDriver
}

type EmailAddress

type EmailAddress struct {
	Name    string // the name can be empty
	Address string // ex: john@example.com
}

type Mailer

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

func NewMailerWithMailGun

func NewMailerWithMailGun(config *SMTPConfig) *Mailer

Initiate the mailer with MailGun driver

func NewMailerWithSMTP

func NewMailerWithSMTP(config *SMTPConfig) *Mailer

Initiate the mailer with SMTP driver

func NewMailerWithSendGrid

func NewMailerWithSendGrid(config *SMTPConfig) *Mailer

Initiate the mailer with SendGrid driver

func NewMailerWithSparkPost

func NewMailerWithSparkPost(config *SMTPConfig) *Mailer

Initiate the mailer with SparkPost driver

func (*Mailer) Send

func (m *Mailer) Send() error

Send the email

func (*Mailer) SetAttachments

func (m *Mailer) SetAttachments(attachments []Attachment) *Mailer

Add attachments to the email

func (*Mailer) SetBCC

func (m *Mailer) SetBCC(emailAddresses []EmailAddress) *Mailer

List of bcc of the email

func (*Mailer) SetCC

func (m *Mailer) SetCC(emailAddresses []EmailAddress) *Mailer

List of cc of the email

func (*Mailer) SetFrom

func (m *Mailer) SetFrom(emailAddress EmailAddress) *Mailer

Sender of the email

func (*Mailer) SetHTMLBody

func (m *Mailer) SetHTMLBody(body string) *Mailer

Set the body of the email in html format make sure to use only one version of the email body, either the html or the plain text to use the html, call the function SetHTMLBody(body string) and if you want to use the text, call the function SetPlainTextBody(body string)

func (*Mailer) SetPlainTextBody

func (m *Mailer) SetPlainTextBody(body string) *Mailer

Set the body of the email in plain text format make sure to use only one version of the email body, either the html or the plain text to use the html, call the function SetHTMLBody(body string) and if you want to use the text, call the function SetPlainTextBody(body string)

func (*Mailer) SetSubject

func (m *Mailer) SetSubject(subject string) *Mailer

Title of the email

func (*Mailer) SetTo

func (m *Mailer) SetTo(emailAddresses []EmailAddress) *Mailer

List of receivers of the email

type SMTPConfig

type SMTPConfig struct {
	Host      string
	Port      int
	Username  string
	Password  string
	TLSConfig tls.Config
}

Jump to

Keyboard shortcuts

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