email

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

README

Email Package

This package provides email functionality for the Dracory framework. It includes:

  • SMTP email sending
  • Responsive HTML email templates
  • Plain text conversion from HTML

Usage

Sending Emails
import (
    "github.com/yourusername/dracory/base/email"
    "log/slog"
)

// Create a logger
logger := slog.Default()

// Create an email sender
sender := email.NewSMTPSender(email.Config{
    Host:     "smtp.example.com",
    Port:     "587",
    Username: "username",
    Password: "password",
    Logger:   logger,
})

// Send an email
err := sender.Send(email.SendOptions{
    From:     "sender@example.com",
    To:       []string{"recipient@example.com"},
    Subject:  "Test Email",
    HtmlBody: "<h1>Hello World</h1><p>This is a test email.</p>",
})

if err != nil {
    // Handle error
}
Using Email Templates
import "github.com/yourusername/dracory/base/email"

// Create email content
content := "<h1 style='" + email.StyleHeading + "'>Welcome!</h1>" +
           "<p style='" + email.StyleParagraph + "'>Thank you for registering.</p>" +
           "<a href='https://example.com/confirm' style='" + email.StyleButton + "'>Confirm Email</a>"

// Generate email template
template := email.DefaultTemplate(email.TemplateOptions{
    Title:   "Welcome to Our App",
    Content: content,
    AppName: "My Application",
    HeaderLinks: map[string]string{
        "Login": "https://example.com/login",
    },
})

// Use the template in SendOptions
sender.Send(email.SendOptions{
    From:     "sender@example.com",
    To:       []string{"recipient@example.com"},
    Subject:  "Welcome to Our App",
    HtmlBody: template,
})

Customization

The email template can be customized by providing different options to the DefaultTemplate function:

  • Title: The email title (appears in the browser tab)
  • Content: The HTML content of the email
  • AppName: The application name (appears in header and footer)
  • HeaderBackgroundColor: The background color of the header (default: #17A2B8)
  • Year: The copyright year (default: current year)
  • HeaderLinks: A map of link text to URLs for the header

Style Constants

The package provides style constants for consistent email styling:

  • StyleHeading: Style for headings
  • StyleParagraph: Style for paragraphs
  • StyleButton: Style for buttons

Documentation

Index

Constants

View Source
const (
	StyleHeading   = "margin:0px;padding:10px 0px;text-align:left;font-size:22px;"
	StyleParagraph = "margin:0px;padding:10px 0px;text-align:left;font-size:16px;"
	StyleButton    = "" /* 163-byte string literal not displayed */
)

Common style constants for email templates

Variables

This section is empty.

Functions

func DefaultTemplate

func DefaultTemplate(options TemplateOptions) string

DefaultTemplate generates a standard responsive email template

Types

type Config

type Config struct {
	// Host is the SMTP server host
	Host string

	// Port is the SMTP server port
	Port string

	// Username is the SMTP server username
	Username string

	// Password is the SMTP server password
	Password string

	// Logger for logging errors
	Logger *slog.Logger
}

Config holds the email configuration

type SMTPSender

type SMTPSender struct {
	// contains filtered or unexported fields
}

SMTPSender implements the Sender interface using SMTP

func (*SMTPSender) Send

func (s *SMTPSender) Send(options SendOptions) error

Send sends an email using SMTP

type SendOptions

type SendOptions struct {
	// From is the email sender
	From string

	// FromName is the name of the sender (unused for now)
	FromName string

	// To is the list of recipients
	To []string

	// Bcc is the list of BCC recipients
	Bcc []string

	// Cc is the list of CC recipients
	Cc []string

	// Subject is the email subject
	Subject string

	// HtmlBody is the HTML content of the email
	HtmlBody string

	// TextBody is the plain text content of the email
	TextBody string
}

SendOptions defines the options for sending an email

type Sender

type Sender interface {
	// Send sends an email with the given options
	Send(options SendOptions) error
}

Sender defines the interface for sending emails

func NewSMTPSender

func NewSMTPSender(config Config) Sender

NewSMTPSender creates a new SMTP email sender

type TemplateOptions

type TemplateOptions struct {
	// Title is the email title
	Title string

	// Content is the HTML content to include in the template
	Content string

	// AppName is the application name to display in the header and footer
	AppName string

	// HeaderBackgroundColor is the background color for the header
	HeaderBackgroundColor string

	// Year is the copyright year (defaults to current year if empty)
	Year string

	// HeaderLinks is a map of link text to URLs for the header
	HeaderLinks map[string]string
}

TemplateOptions defines the options for generating an email template

Jump to

Keyboard shortcuts

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