email

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 8 Imported by: 0

README

email

Minimal helpers to render templates and send multi‑part emails via an interface (types.Emailer) with an SMTP implementation.

Install

import (
  "github.com/aatuh/email"
  "github.com/aatuh/email/types"
)

Quick start

ctx := context.Background()
sender := email.NewSMTP()

smtpCfg := &types.SMTPConfig{SMTPHost: "smtp.example.com", SMTPPort: 587, UseTLS: true}
auth := &types.SMTPAuth{Username: "user", Password: "pass"}

mail := &types.Mail{
  FromName:  "App",
  From:      "[email protected]",
  ToName:    "User",
  To:        "[email protected]",
  Subject:   "Welcome",
  PlainBody: "Hello {{.Name}}",
  HTMLBody:  "<p>Hello <b>{{.Name}}</b></p>",
}

err := email.SendEmailWithTemplate(ctx, sender, mail.To, map[string]any{"Name": "Ada"}, mail, smtpCfg, auth)
if err != nil { /* handle */ }
Retry helper
err := email.SendEmailWithRetries(ctx, sender, smtpCfg, auth, mail, 3, time.Second)

Notes

  • SendEmailWithTemplate renders both plain and HTML bodies with text/template/html/template.
  • SMTP sender builds multi-part/alternative messages when both bodies are provided.
  • Implement types.Emailer to plug in any email backend.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderTemplate

func RenderTemplate(
	templateString string, templateVariables map[string]any,
) (*bytes.Buffer, error)

RenderTemplate parses and renders an email template with the provided variables.

func SendEmailWithRetries

func SendEmailWithRetries(
	ctx context.Context,
	sender types.Emailer,
	smtpConfig *types.SMTPConfig,
	smtpAuth *types.SMTPAuth,
	mail *types.Mail,
	maxTryCount int,
	sleepTime time.Duration,
) error

SendEmailWithRetries sends an email with retry logic. It respects context cancellation between retries.

func SendEmailWithTemplate

func SendEmailWithTemplate(
	ctx context.Context,
	emailer types.Emailer,
	toEmail string,
	templateData map[string]any,
	mail *types.Mail,
	smtpConfig *types.SMTPConfig,
	smtpAuth *types.SMTPAuth,
) error

SendEmailWithTemplate renders both email templates and sends the email.

Types

type SMTP

type SMTP struct{}

SMTP is an SMTP-based sender that sends multi-part emails.

func NewSMTP

func NewSMTP() *SMTP

NewSMTP creates a new SMTP instance.

func (*SMTP) Send

func (s *SMTP) Send(
	ctx context.Context,
	smtpConfig *types.SMTPConfig,
	smtpAuth *types.SMTPAuth,
	mail *types.Mail,
) error

Send sends an email via SMTP.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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