mailit

package module
v0.0.0-...-7487a7e Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 3 Imported by: 2

README

Mailit

An email kit for golang to be reused across different projects.

Introduction

Mailit is an easy to use mail library to send emails.

Features

Mailit supports:

  • Plain text emails
  • Text / HTML template based emails
  • Attachments
  • Sending emails to multiple recipients

Requirements

  • Go 1.5

Documentation

https://pkg.go.dev/github.com/wisdommatt/mailit


We also recommend you use the how to guide on this page because Mailit does basically two things:

  • Send Plain Text Emails
  • Send Template Based Emails

How To Use Mailit

Feel free to copy and paste the codes

  • Step 1: Initialize a mailer variable and pass in SMTP configs.
    Note: This is usually done just once
        smtpConf := mailit.SMTPConfig{
            Host:     "domain.com",
            Port:     563,
            Username: "username@domain.com",
            Password: "*********",
        }
        mailer := mailit.NewMailer(smtpConf)
    
  • Step 2: Send email
    Sending plain text email
        textDep := mailit.TextDependencies{
            From:        "sender@domain.com",
            To:          []string{"user1@domain.com", "user2@domain.com", "user3@domain.com"},
            Subject:     "Welcome to Mailit",
            Body:        "Message Body",
            Attachments: []string{"attachments/1.txt", "attachments/2.txt"},
        }
        err := mailer.SendText(textDep)
    

    Sending HTML template email
        tempDep := mailit.TemplateDependencies{
            From:        "sender@domain.com",
            To:          []string{"user1@domain.com", "user2@domain.com", "user3@domain.com"},
            Subject:     "Welcome to Mailit",
            ContentType: "text/html",
            Template: "templates/welcome.html",
            TemplateData: "Any data",
            Attachments: []string{"attachments/1.txt", "attachments/2.txt"},
        }
        err := mailer.SendTemplate(tempDep)
    

    Sending Text template email
        tempDep := mailit.TemplateDependencies{
            From:        "sender@domain.com",
            To:          []string{"user1@domain.com", "user2@domain.com", "user3@domain.com"},
            Subject:     "Welcome to Mailit",
            ContentType: "text/plain",
            Template: "templates/sample.txt",
            TemplateData: struct{
                Name, Email string
            }{
                Name: "Wisdom Matt",
                Email: "user@example.com",
            },
            Attachments: []string{"attachments/1.txt", "attachments/2.txt"},
        }
        err := mailer.SendTemplate(tempDep)
    

NOTE: You will access the template data the way it is access usually in Go templates e.g {{ .Name }} or {{ .Email }}


Dependencies

Testing

This package uses manual testing because the gomail package which it depends on used core types instead of interfaces which in turn makes it difficult to test, a thorough test is done before any changed is made on the project so there is nothing to be scared of.

License

MIT

Author / Contact

Wisdom Matthew - github.com/wisdommatt

Feel free to email me at talk2wisdommatt@gmail.com

Documentation

Overview

Package mailit provides a simple interface that depends on gomail-v2 (https://github.com/go-gomail/gomail) to compose emails and to mail them efficiently without writing complex code.

More info on Github: https://github.com/wisdommatt/mailit

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mailer

type Mailer interface {
	TextMailer
	TemplateMailer
}

Mailer is the interface that wraps TextMailer and TemplateMailer.

func NewMailer

func NewMailer(config SMTPConfig) Mailer

NewMailer returns a new mailer which implements the Mailer interface.

NewMailer uses the informations in config to prefill the SMTP configurations for the mail.

type SMTPConfig

type SMTPConfig struct {
	Host, Username, Password string
	Port                     int
}

SMTPConfig holds smtp configurations that a required to send the mail.

type TemplateDependencies

type TemplateDependencies struct {
	// From is the email address of the sender
	// e.g no-reply@example.com
	From string

	// To is the receiver/receivers of the email
	// e.g []{"user@example.com", "user2@example.com"}.
	To []string

	// SenderName is the name fo the email sender.
	SenderName string

	// Subject is the subject of the email e.g Hello NewsLetter
	Subject string

	// ContentType is the content type of the template e.g
	// text/plain or text/html etc
	ContentType string

	// Template is the directory location of the template
	// e.g templates/welcome.html, templates/hello.txt
	Template string

	// TemplateData is the data/content you want to pass to the
	// template e.g User Info, Order Details etc
	TemplateData interface{}

	// Attachments should hold the list of attachments to send with
	// the email if there is any.
	// 		[]{"assets/docs/welcome.pdf", "assets/images/admin.jpeg"}
	Attachments []string
}

TemplateDependencies is the struct that holds dependencies for a template based email. e.g template directory, sender/receiver email etc.

type TemplateMailer

type TemplateMailer interface {
	SendTemplate(dep TemplateDependencies) error
}

TemplateMailer is the interface that wraps SendTemplate method.

type TextDependencies

type TextDependencies struct {
	// From is the email address of the sender
	// e.g no-reply@example.com
	From string

	// SenderName is the name fo the email sender.
	SenderName string

	// To is the receiver/receivers of the email
	// e.g []{"user@example.com", "user2@example.com"}.
	To []string

	// Subject is the subject of the email e.g Hello NewsLetter
	Subject string

	// Body is the body of the email e.g Thanks for signing up
	Body string

	// Attachments should hold the list of attachments to send with
	// the email if there is any.
	// 		[]{"assets/docs/welcome.pdf", "assets/images/admin.jpeg"}
	Attachments []string
}

TextDependencies is the struct that holds dependencies for a plain text email. e.g message body, sender/receiver email etc.

type TextMailer

type TextMailer interface {
	SendText(dep TextDependencies) error
}

TextMailer is the interface that wraps SendText method.

Jump to

Keyboard shortcuts

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