emailer

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2019 License: MIT Imports: 6 Imported by: 0

README

emailer

GoDoc

This Go module simplifies a process of sending HTML emails to multiple users.

Example

Simple use case.

You have a recipients.csv file with user emails (imagine there are more lines):

ID, NAME, MAIL, PET
1,Anton,anton@example.com,cat

And you want to send an email to each one of them. Here is how you can do that:

package main

import (
    "fmt"

    "github.com/fabritsius/emailer"
    "github.com/fabritsius/envar"
    "github.com/fabritsius/csvier"
)

func main() {
    // Get a simple email template
    temp := simpleTemplate()

    // Get a slice of recipients from a file
    recipients, err := csvier.ReadFile("recipients.csv")
    if err != nil {
        panic(err)
    }
	
    cfg := emailer.Config{}
    // Fill mail config using environment variables
    if err := envar.Fill(&cfg); err != nil {
        // All envs has to be set:
        //  MAIL_NAME – sender name
        //  MAIL_ADDR – sender email address
        //  MAIL_PASS – sender email password
        //  MAIL_SERV – email server address
        //  MAIL_PORT – email server port
        panic(err)
	}

    // Create Mail object with template and subject
    mail := emailer.New(temp, "A letter from a Program")

    // Change fields which are used to set recipient Name and Address
    //  this is optional and in this case default values are used
    userFields := emailer.ChangeUserFields("NAME", "MAIL")

    // Send emails to recipients and collect errors
    errors := mail.SendToMany(recipients, &cfg, userFields)
    for i, err := range errors {
        fmt.Printf("[error %d] %s\n", i, err)
    }
}

func simpleTemplate() string {
    return `
        <p>Dear, <span style="border-bottom: 2px solid rgb(148, 66, 255)">{{ .NAME }}</span></p>
        <p>I hope you and your {{ .PET }} are doing fine =)</p>
        <p>Best wishes to you, <br>Program</p>`
}

Received email:

Dear, Anton

I hope you and your cat are doing fine =)

Best wishes to you, 
Program

TODO

  • Add core features
  • Make send loop concurrent
  • Add testing

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeUserFields added in v0.2.0

func ChangeUserFields(name string, mail string) func(f *userFields)

ChangeUserFields is an option for SendTo() and SendToMany() changes which fields of recipient map are used to set a Name and Address defaults are "NAME" and "MAIL"

Types

type Config

type Config struct {
	Name     string `env:"MAIL_NAME" default:""`
	Mail     string `env:"MAIL_ADDR"`
	Password string `env:"MAIL_PASS"`
	Server   string `env:"MAIL_SERV"`
	Port     string `env:"MAIL_PORT"`
}

Config defines parameters for SMTP connection

type Mail

type Mail struct {
	Subject  string
	Template string
}

Mail represents a mail content

func New

func New(template string, subject string) *Mail

New allocates a new Mail object with template and subject

func (*Mail) SendTo

func (m *Mail) SendTo(recipient map[string]string, cfg *Config,
	options ...func(*userFields)) error

SendTo sends Mail to a recipient

func (*Mail) SendToMany

func (m *Mail) SendToMany(recipients []map[string]string, cfg *Config,
	options ...func(*userFields)) []error

SendToMany sends Mail to every recipient

Jump to

Keyboard shortcuts

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