Skip to content

elmyrockers/go-sesmailer

Repository files navigation

go-sesmailer

Go Reference Go Version License: MIT Release Build / CI Coverage AWS SES

Minimal wrapper around the AWS SES SDK for Go to simplify sending emails with a PHPMailer-like API.

go-sesmailer provides a simple and developer-friendly interface to send emails using AWS Simple Email Service (SES). Inspired by PHPMailer, it supports adding multiple recipients, CC, BCC, Reply-To addresses, and sending both HTML and plain text emails. Its fluent API makes it easy to integrate into Go projects, including web frameworks like Fiber.

Features

  • Lightweight and tiny wrapper around Amazon SES.
  • Built on top of the official AWS SDK for Go v2.
  • Uses the Amazon SES API instead of SMTP for better performance and faster delivery.
  • High-performance attachments via streaming. Uses io.Reader (32KB chunks) for low memory usage.
  • Provides improved security by using AWS IAM authentication instead of SMTP credentials.
  • PHPMailer-like API familiar to developers coming from PHP and traditional email libraries.
  • Fluent method chaining for clean and readable email construction.
  • Supports HTML and plain text emails.
  • Plain text fallback (AltBody) for HTML emails.
  • Supports multiple recipients: To, CC, and BCC.
  • Supports Reply-To headers.
  • Built-in debug logging with multiple verbosity levels.
  • Supports context-based sending (SendContext) for cancellation and timeouts.
  • Automatically loads AWS configuration from the default environment.

Security Highlights

  • Production-Ready: Fully tested and safe for use in production environments.
  • Header Injection Protection: All headers sanitized to remove CR, LF, null, and control characters.
  • RFC 5322 Compliant: Header lengths truncated at 998 bytes safely, UTF-8 aware
  • Safe Email Addresses: Validated and properly encoded display names.
  • Attachment Security: Filenames sanitized; data streamed and base64-encoded.
  • Body Encoding: Quoted-printable encoding ensures safe transmission of non-ASCII content.
  • AWS SES Secure Delivery: Uses official SDK v2 with TLS and signed requests.
  • Debug & Logging Control: Optional debug with minimal exposure of sensitive info.

Installation

go get github.com/elmyrockers/go-sesmailer

Make sure you have your AWS credentials set in the environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION) or through your AWS config. During development, you can create a .env file next to your main.go and load it using the joho/godotenv library:

go get github.com/joho/godotenv

Your .env file should contain:

AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=us-east-1

Then you can load it automatically in your code using the import:

import (
    _ "github.com/joho/godotenv/autoload" //Load your .env file automatically
    "github.com/elmyrockers/go-sesmailer"
)

Usage

1. Basic Example:

package main

import (
	_ "github.com/joho/godotenv/autoload"
    "github.com/elmyrockers/go-sesmailer"
    "log"
)

func main() {
    err := sesmailer.New().
        SetFrom("[email protected]", "Your Company").
        AddAddress("[email protected]", "Helmi Aziz").
        SetSubject("Test Email").
        SetBody("Hello! This is a test email.").
        IsHTML(false).
        Send()

    if err != nil {
        log.Fatalf("Failed to send email: %v", err)
    }

    log.Println("Email sent successfully")
}

2. Sending HTML Email with Plain Text Fallback:

err := sesmailer.New().
    SetFrom("[email protected]", "Your Company").
    AddAddress("[email protected]", "Helmi Aziz").
    SetSubject("HTML Email Example").
    SetBody("<h1>Hello</h1><p>This is an HTML email.</p>").
    SetAltBody("Hello! This is a plain text version.").
    IsHTML(true).
    Send()

if err != nil {
    log.Fatalf("Failed to send email: %v", err)
}

3. Adding CC, BCC, and Reply-To:

err := sesmailer.New().
    SetFrom("[email protected]", "Your Company").
    AddAddress("[email protected]", "Helmi Aziz").
    AddCC("[email protected]", "Administrator").
    AddBCC("[email protected]", "").
    AddReplyTo("[email protected]", "Administrator").
    SetSubject("Email with CC/BCC/ReplyTo").
    SetBody("This email has CC, BCC, and Reply-To addresses.").
	Send()

if err != nil {
    log.Fatalf("Failed to send email: %v", err)
}

4. Email with Attachments

err := sesmailer.New().
    SetFrom("[email protected]", "Your Company").
    AddAddress("[email protected]", "Helmi Aziz").
    SetSubject("Email with Attachments").
    SetBody("This email will include a few attachments").
    
    AddAttachment("docs/invoice_123.pdf", "Invoice.pdf").
    AddAttachment("images/logo.png", "CompanyLogo.png").
    Send()

if err != nil {
    log.Fatalf("Failed to send email: %v", err)
}

5. Enabling Debug Logging:

err := sesmailer.New().
    SetFrom("[email protected]", "Your Company").
    AddAddress("[email protected]", "Helmi Aziz").
    SetSubject("Debug Email").
    SetBody("This email will show debug info").
    SetDebug(2). // 0 = none, 1 = errors, 2 = verbose
	Send()

if err != nil {
    log.Fatalf("Failed to send email: %v", err)
}

API Reference

Method Description
New() *Mailer Creates a new Mailer instance and automatically initializes the AWS SES client using the default AWS configuration.
SetFrom(email string, name string) *Mailer Sets the sender email address and optional display name.
AddAddress(email string, name string) *Mailer Adds a recipient to the To list.
AddCC(email string, name string) *Mailer Adds a recipient to the CC list.
AddBCC(email string, name string) *Mailer Adds a recipient to the BCC list.
AddReplyTo(email string, name string) *Mailer Adds an email address to the Reply-To header.
SetSubject(subject string) *Mailer Sets the email subject line.
SetBody(body string) *Mailer Sets the main email body content.
SetAltBody(alt string) *Mailer Sets an alternative plain-text body when sending HTML emails.
AddAttachment(path string, name string) *Mailer Adds an attachment via streaming. Uses io.Reader (32KB chunks) for low memory usage and performance. Automatically closes file handles after sending.
IsHTML(isHtml bool) *Mailer Sets whether the email content type should be text/html or text/plain.
SetDebug(level int) *Mailer Enables debug logging. 0 = disabled, 1 = errors/retries, 2 = verbose request/response logs.
Send() error Sends the email using a default background context.
SendContext(ctx context.Context) error Sends the email using a custom context. Useful for timeouts, cancellations, or request tracing.

License

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

About

Minimal wrapper around the AWS SES SDK for Go to simplify sending emails with a PHPMailer-like API.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages