SimpleMailer

package module
v0.0.0-...-2ff0b02 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: MIT Imports: 8 Imported by: 0

README

alt tag

SimpleMailer Build Status Code Climate Coverage Status GoDoc

KISS principle SMTP emailing solution that allows variables and template HTML emails.

An extremely simple SMTP emailing solution for your Go Language application. This golang package allows you to create a HTML email templates, and have variables inside of it by including {{USERNAME}} to be replaced by the actual username in the array. Simple Mailer can also send bulk emails without variables, checkout this package and try it out!

1. Import the package

go get github.com/hunterlong/simplemailer
import "github.com/hunterlong/simplemailer"

2. Create 'emails' folder and create a new file called 'welcome.html'

You just got an email from SimpleMailer {{USERNAME}}. I hope you enjoyed how {{DIFFICULTY}} it was to do this.

Notice the {{USERNAME}} variable inside the HTML template. You'll be able to insert many of these variables in a simple array.

3. Setup SMTP Information

// SMTP host, port, username, password, name, send from address, email directory
SimpleMailer.SetSMTPInfo("emailserveraddress.com", "465", "info@domain.com", "passwordhere", "Sender Name", "from@domain.com", "./emails/")
Be sure to set the email directory for the last parameter (./emails/) ** Keep slash on end! **

alt tag

Send a Single Email with Variables
outVars := SimpleMailer.Variables{map[string]interface{}{"USERNAME":"gophers", "DIFFICULTY": "simple"}}
newOutgoing := SimpleMailer.Outgoing{
                    Email: "info@domain.com", 
                    Subject: "SimpleMailer in Golang", 
                    Template: "welcome.html", 
                    Variables: outVars }
sendSuccess := SimpleMailer.SendSingle(newOutgoing)
fmt.Println(sendSuccess)
// outputs true or false

####### This will replace {{USERNAME}} in your HTML template to 'gophers'

Send Multiple Emails with Variables
var newOutgoing []SimpleMailer.Outgoing

outVars := SimpleMailer.Variables{map[string]interface{}{"USERNAME":"billy", "DIFFICULTY": "simple"}}
newOutgoingMessage := SimpleMailer.Outgoing{Email: "firstuser@domain.com", Subject: "Welcome Email", Template: "welcome.html", Variables: outVars}
newOutgoing = append(newOutgoing, newOutgoingMessage)

secondoutVars := SimpleMailer.Variables{map[string]interface{}{"USERNAME":"l33tguy", "DIFFICULTY": "super easy"}}
secondnewOutgoingMessage := SimpleMailer.Outgoing{Email: "firstuser@domain.com", Subject: "Welcome Email", Template: "welcome.html", Variables: secondoutVars}
newOutgoing = append(newOutgoing, secondnewOutgoingMessage)

// here is where you send multiple emails from a Outgoing array
responses := SimpleMailer.SendMultiple(newOutgoing)

for _,successSend := range responses {
	userEmail := successSend["email"]
	sentStatus := successSend["status"].(bool)

	if sentStatus {
		fmt.Println("Email to "+userEmail.(string)+" was successfully sent")
	} else {
		fmt.Println("ERROR - Email to "+userEmail.(string)+" was not able to send")
	}
}
Send Multiple Emails without Variables
allEmails := []string{"info@emaildomain.com", "noreply@nodomain.com", "hey@gmail.com"}
bulkSend := SimpleMailer.BulkSend{Emails: allEmails, Subject: "Hello Bulk Sender", Template: "welcome.html"}

response := SimpleMailer.SendBulkEmails(bulkSend)
This function will send an email without variables to an array of emails
Full Example for Sending Single Email
package main

import (
	"github.com/hunterlong/simplemailer"
	"fmt"
)

func main() {

	SimpleMailer.SetSMTPInfo("emailserver-hidden.com", "465", "stmpusername", "passwordhere", "Sender Name", "info@sendfrom.com", "./emails/")

	outVars := SimpleMailer.Variables{map[string]interface{}{"USERNAME":"gophers", "DIFFICULTY": "simple"}}
	newOutgoing := SimpleMailer.Outgoing{
		Email: "info@sendtoemail.com",
		Subject: "SimpleMailer in Golang",
		Template: "welcome.html",
		Variables: outVars }
	sendSuccess := SimpleMailer.SendSingle(newOutgoing)

	fmt.Println(sendSuccess)
	// outputs true if it was sent, false if error

}
Google Gmail SMTP info
SimpleMailer.SetSMTPInfo("smtp.gmail.com", "465", "<gmail email>", "<gmail password>", "Sender Name", "<gmail email>", "./emails/")
Yahoo Mail SMTP info
SimpleMailer.SetSMTPInfo("smtp.mail.yahoo.com", "465", "<yahoo email>", "<yahoo password>", "Sender Name", "<yahoo email>", "./emails/")
Outlook Office 365 SMTP info
SimpleMailer.SetSMTPInfo("smtp.office365.com", "587", "<outlook email>", "<outlook password>", "Sender Name", "<outlook email>", "./emails/")
Awesome HTML Email Templates

https://github.com/leemunroe/responsive-html-email-template

https://github.com/mailgun/transactional-email-templates

https://github.com/mailchimp/email-blueprints

https://github.com/InterNations/antwort

https://github.com/g13nn/Email-Framework

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceContentText

func ReplaceContentText(array map[string]interface{}, content string) string

function to replace variables as: {{USERNAME}} to USERNAME inside the html template. input as array of {"USERNAME": "gopher"}

func SendBulkEmails

func SendBulkEmails(outgoing BulkSend) []map[string]interface{}

function to send multiple emails without any variables. good for sending a flat html email to many. uses BulkSend struct

func SendEmail

func SendEmail(outgoingEmail Outgoing) bool

function to send an email with TLS and PlainAuth this function is ran multiple times in some scripts

func SendMultiple

func SendMultiple(outgoing []Outgoing) []map[string]interface{}

function to send multiple emails with variables. uses and array of Outgoing structs

func SendSingle

func SendSingle(outgoing Outgoing) bool

function to send a single email from Outgoing struct

func SetSMTPInfo

func SetSMTPInfo(host string, port string, user string, password string, fromName string, fromAddress string, emailsDir string)

function to set the SMTP login information and email directory be sure to leave forward slash on end of email directory

Types

type BulkSend

type BulkSend struct {
	Emails   []string
	Subject  string
	Template string
}

Array of email addresses, subject for email, and .html template

type Config

type Config struct {
	SMTPuser  string
	SMTPpass  string
	SMTPhost  string
	SMTPname  string
	SMTPfrom  string
	SMTPport  string
	EmailsDir string
}

configs for SMTP login information and email template directory

type Outgoing

type Outgoing struct {
	Email     string
	Subject   string
	Template  string
	Variables Variables
}

A single email address, subject, .html template and array of Variables

type Variables

type Variables struct {
	Inputs map[string]interface{}
}

variables in an array that will be replaced inside template USERNAME with text: {{USERNAME}} in the .html template

Jump to

Keyboard shortcuts

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