mail

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2025 License: MIT Imports: 12 Imported by: 2

README

Mailing Module for Golang

A Golang module for sending emails seamlessly using AWS or Send Grid.

Features

  • AWS SES Integration: Send transactional emails via AWS SES.
  • Send Grid Support: Integrate with SendGrid for email delivery.
  • Simple API: Easy-to-use interface for developers.

Installation

Get started by installing the package:

go get github.com/fsobh/mail

Usage

Initialize the Mailer

Before sending emails, configure the mailer with your provider's credentials.

Ensure that the sender email has been verified with its respective provider :
  1. AWS SES guide here
  2. Send Grid guide here
package main

import (
	"fmt"
	"github.com/fsobh/mail"
)

func main() {

	/*
	 Never paste your credentials directly in the code for safety reasons
	 Recommended : Store these in environment variables using [viper](https://github.com/spf13/viper)
	*/

	senderEmail := "example@sender.com"
	sesRegion :=  "us-east-1"
	sgAPIKey := "<your-api-key>"
	sgAppName := "<your-app-name>"

	//Initialize a new AWS SES mailer
	mailerSES := mail.NewSESSender(sesRegion, senderEmail)

	//Initialize a new Send Grid mailer
	mailerSendGrid := mail.NewSendGridSender(sgAPIKey, sgAppName, senderEmail)

	subject := "Email Subject"
	contentHtml := fmt.Sprintf("<h1>Email HTML content</h1>")
	to := []string{"reciever@email.com"}
	cc := []string{"cc@email.com"}
	bcc := []string{"bcc@email.com>"}
	attachments := []string{"../files/example.txt"}

	// SES example
	err := mailerSES.SendMail(subject, contentHtml, to, cc, bcc, nil) //can't do attachments on SES

	if err != nil {
		_ = fmt.Errorf("error sending email via SES: %s", err)
	}

	// Send Grid example
	err = mailerSendGrid.SendMail(subject, contentHtml, to, cc, bcc, attachments)

	if err != nil {
		_ = fmt.Errorf("error sending email via Send Grid: %s", err)
	}

	fmt.Println("Email sent successfully")

}

Notes

To test AWS SES sends locally, you must export your IAM roles Access ID & Secret :
  1. Follow this guide to generate the credentials for your IAM account (make sure you give it the right access policies to send Emails via SES).
  2. Export your credentials locally in your terminal
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
export AWS_DEFAULT_REGION="us-west-2" # Change to your region

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

This section is empty.

Types

type EmailSender

type EmailSender interface {
	SendMail(
		subject string,
		content string,
		to []string,
		cc []string,
		bcc []string,
		attachFiles []string,
	) error
}

func NewSESSender

func NewSESSender(region string, senderEmail string) EmailSender

NewSESSender initializes a new SESSender with optional credentials. If AccessKeyID and SecretKey are empty, the AWS SDK will use the default credentials chain.

func NewSendGridSender

func NewSendGridSender(apiKey string, appName string, appEmail string) EmailSender

NewSendGridSender initializes a new SendGridSender.

type SESSender

type SESSender struct {
	Region      string
	SenderEmail string
}

SESSender implements the EmailSender interface for AWS SES.

func (*SESSender) SendMail

func (s *SESSender) SendMail(
	subject string,
	content string,
	to []string,
	cc []string,
	bcc []string,
	attachFiles []string,
) error

type SMSSender added in v1.0.1

type SMSSender interface {
	SendSMS(
		message string,
		to []string,
	) error

	SendBulkSMS(
		message string,
		to []string,
	) error
}

SMSSender interface defines methods for sending SMS messages

func NewSNSSender added in v1.0.1

func NewSNSSender(region string, senderPhoneNumber string) SMSSender

NewSNSSender initializes a new SNSSender

func NewTwilioSender added in v1.0.1

func NewTwilioSender(accountSID string, authToken string, senderPhoneNumber string) SMSSender

NewTwilioSender initializes a new TwilioSender

type SNSSender added in v1.0.1

type SNSSender struct {
	Region            string
	SenderPhoneNumber string
}

SNSSender implements the SMSSender interface for AWS SNS

func (*SNSSender) SendBulkSMS added in v1.0.1

func (s *SNSSender) SendBulkSMS(message string, to []string) error

SendBulkSMS sends SMS messages to multiple recipients using AWS SNS

func (*SNSSender) SendSMS added in v1.0.1

func (s *SNSSender) SendSMS(message string, to []string) error

SendSMS sends an SMS message to a single recipient using AWS SNS

type SendGridSender

type SendGridSender struct {
	APIKey   string
	AppName  string
	AppEmail string
}

SendGridSender implements the EmailSender interface for SendGrid.

func (*SendGridSender) SendMail

func (s *SendGridSender) SendMail(
	subject string,
	content string,
	to []string,
	cc []string,
	bcc []string,
	attachFiles []string,
) error

type TwilioSender added in v1.0.1

type TwilioSender struct {
	AccountSID        string
	AuthToken         string
	SenderPhoneNumber string
}

TwilioSender implements the SMSSender interface for Twilio

func (*TwilioSender) SendBulkSMS added in v1.0.1

func (t *TwilioSender) SendBulkSMS(message string, to []string) error

SendBulkSMS sends SMS messages to multiple recipients using Twilio

func (*TwilioSender) SendSMS added in v1.0.1

func (t *TwilioSender) SendSMS(message string, to []string) error

SendSMS sends an SMS message to a single recipient using Twilio

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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