sender

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

📮 Go Email Sender

Go Reference

Simple (but useful) email sender written in pure Go v1.17. Yes, yet another email package here! 😅

Support HTML templates and attachments.

Send HTML email

Method signature:

func (s *Sender) SendHTMLEmail(
    templatePath string,
    to []string,
    cc []string,
    subject string,
    data interface{},
    files []string,
) error

Example:

// Create a new struct for the email data.
type HTMLEmailData struct {
    Name    string
    Website string
}

// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("mail@test.com", "secret", "smtp.test.com", 25)

// Send the email with an HTML template.
if err := sender.SendHTMLEmail(
    "my/templates/welcome.html", // path to the HTML template
    []string{
        "mail@example.com",      // slice of emails to send
    },
    []string{
        "copy-mail@example.com", // slice of emails to send message copy
    },
    "It's a test email!",        // subject of the email
    &HTMLEmailData{
        Name:    "Vic",
        Website: "https://shostak.dev/",
    },
    []string{
        "my/files/image.jpg",    // slice of files to send
    },
); err != nil {
    // Throw error message, if something went wrong.
    return fmt.Errorf("Something went wrong: %v", err)
}

Send plain text email

Method signature:

func (s *Sender) SendPlainEmail(
    to []string,
    cc []string,
    subject string,
    data interface{},
    files []string,
) error

Example:

// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("mail@test.com", "secret", "smtp.test.com", 25)

// Send the email with a plain text.
if err := sender.SendPlainEmail(
    []string{
        "mail@example.com",       // slice of emails to send
    },
    []string{
        "copy-mail@example.com",  // slice of emails to send message copy
    },
    "It's a test email!",         // subject of the email
    "Here is a plain text body.", // body of the email
    []string{
        "my/files/image.jpg",     // slice of files to send
    },
); err != nil {
    // Throw error message, if something went wrong.
    return fmt.Errorf("Something went wrong: %v", err)
}

⚠️ License

Apache-2.0 © Vic Shóstak & True web artisans.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTemplate

func ParseTemplate(file string, data interface{}) (string, error)

ParseTemplate func for parsing and validating templates. Includes given data to template.

Types

type Sender

type Sender struct {
	Login          string
	Password       string
	SMTPServer     string
	SMTPServerPort int
}

Sender struct for describe auth data for sending emails.

func NewEmailSender

func NewEmailSender(login, password, server string, port int) *Sender

NewEmailSender func for create new email sender. Includes login, password, SMTP server and port.

func (*Sender) SendHTMLEmail

func (s *Sender) SendHTMLEmail(templatePath string, to, cc []string, subject string, data interface{}, files []string) error

SendHTMLEmail func for send email with given HTML template and data. If template is empty string, it will throw error.

func (*Sender) SendPlainEmail

func (s *Sender) SendPlainEmail(to, cc []string, subject, data string, files []string) error

SendPlainEmail func for send plain text email with data.

Jump to

Keyboard shortcuts

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