goemail

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2021 License: MIT Imports: 11 Imported by: 0

README

goemail Build Status Go Report Card License

A wrapper over net/smtp to make sending email easier in go.

Features

  • HTML email
  • HTML template email
  • Attach file ([]byte, os.File or by file path)

Usage

1. Get Client:

Use your smtp server credentials to get client object.

client := goemail.NewClient(&goemail.Config{
    Host:     "smtp.gmail.com",
    Port:     587,
    Email:    "user@example.com",
    Password: "password",
})
2. Get Mailer:

Get a new mailer object from the client for each new email to send.

mailer := client.NewMailer("Test Email", "This is an email for testing.")
})
3. Build Email and Send:

Add recipients of your email. Attach a file if you need to. And send the email.

mailer.AddRecipients([]mail.Address{
    {
        Name:  "Random Guy",
        Address: "randomguy123@example.com",
    },
})
mailer.AddBlindCopyRecipients([]mail.Address{
    {
        Address: "secret01@example.com",
    },
})
mailer.SetSender(mail.Address{
    Name:  "Faizan Khalid",
})

mailer.SetReplyToEmail("no-reply@example.com")

_ = mailer.AttachFile("../Downloads/my_file.pdf")

_ = mailer.Send()

Html Template Example

package main

import (
    "github.com/IamFaizanKhalid/goemail"
    "log"
    "net/mail"
)

func main() {
    client := goemail.NewClient(&goemail.Config{
        Host:     "smtp.gmail.com",
        Port:     587,
        Email:    "user@example.com",
        Password: "password",
    })
    
    templateValues := struct {
        Name  string
        Url   string
        Title string
    }{
        Name:  "John Doe",
        Url:   "http://johndoe.com",
        Title: "Welcome to my Homepage",
    }
        
    mailer, err := client.NewHtmlMailerFromTemplate("JohnDoe.com", "welcome.html", templateValues)
    if err != nil {
        log.Println(err)
        return
    }

    mailer.AddRecipients([]mail.Address{{Address: "IamFaizanKhalid@gmail.com"}})

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

Known Issues

  • AddInlineFile() adds gibberish in the email.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MessageFromHtmlTemplate

func MessageFromHtmlTemplate(templateFilePath string, values interface{}) (string, error)

MessageFromHtmlTemplate parses html template file and fill the given values in the variables accordingly

Types

type Client

type Client interface {
	// NewMailer returns a Mailer object to send an email with the given subject and body
	NewMailer(subject string, body string) Mailer

	// NewHtmlMailer returns a Mailer object to send an email with the given subject and html body
	NewHtmlMailer(subject string, body string) Mailer

	// NewHtmlMailerFromTemplate returns a Mailer object to send an email with the given subject
	// and by using the given html template as the body.
	NewHtmlMailerFromTemplate(subject string, templateFile string, templateValues interface{}) (Mailer, error)
}

Client is the mail client which holds the connection details to the smtp server

func NewClient

func NewClient(config *Config) Client

NewClient returns mail Client using smtp credentials

type Config

type Config struct {
	// Host represents the smtp server's host
	Host string
	// Port represents the smtp server's port
	Port int
	// Email is the email used to sign in to the smtp server
	Email string
	// Password is the password used to sign in to the smtp server
	Password string
}

Config to get Client using smtp credentials

type Mailer

type Mailer interface {
	// AddRecipients adds `To` to the email
	AddRecipients(emails []mail.Address)

	// AddCopyRecipients adds `Cc` to the email
	AddCopyRecipients(emails []mail.Address)

	// AddBlindCopyRecipients adds `Bcc` to the email
	AddBlindCopyRecipients(emails []mail.Address)

	// AddHeader adds a custom header to the email
	AddHeader(key string, value string)

	// AddInlineFile embeds a file in the email body
	AddInlineFile(filePath string) error

	// SetSender sets `From` in the email
	SetSender(u mail.Address)

	// SetReplyToEmail sets `Reply-To` in the email
	SetReplyToEmail(email string)

	// UpdateSubject update the subject of the email
	UpdateSubject(subject string)

	// AttachFile opens the given file and adds it as an attachment to the email
	AttachFile(filePath string) error

	// AttachFile adds the given file as an attachment to the email
	AttachOpenedFile(file *os.File) error

	// AttachFileBytes puts the given bytes as an attached file to the email
	AttachFileBytes(fileName string, binary []byte)

	// Send sends the email
	Send() error
	// contains filtered or unexported methods
}

Mailer can be used to build and send an email

Jump to

Keyboard shortcuts

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