mailer

package module
v0.0.0-...-152324c Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 5 Imported by: 0

README

Mailer Module

The mailer module provides a convenient way to send emails using Go. It includes functionality for validating email data and sending emails with SMTP authentication.

Features

  • Simple and intuitive API for sending emails.
  • Support for email authentication configuration.
  • Templating support for email content customization.

Installation

To use the mailer module in your Go project, you can simply import it using:

import "github.com/vidarlx/mailer"

Usage

Creating a Mailer

To create a new mailer instance, use the NewMailer function:

authConfig := mailer.EmailAuthConfig{
    Host:     "smtp.example.com",
    Port:     587,
    Username: "user@example.com",
    Password: "password",
    From:     "user@example.com",
}
templateDir := "templates"

mailService := mailer.NewMailer(authConfig, templateDir)
Sending an Email

To send an email, create an Email struct with the necessary data and pass it to the SendEmail method of the mailer instance:

email := &mailer.Email{
    Recipients:       []string{"recipient@example.com"},
    Subject:          "Test Subject",
    Body:             "Test Body",
    TemplateFileName: "test_template.tmpl",
    Data:             nil, // Assuming no data for simplicity
}

err := mailService.SendEmail(email)
if err != nil {
    // Handle error
}
Templating Support

You can use templates to customize the content of your emails. Simply provide the template file name and data to be parsed with the template:

email := &mailer.Email{
    Recipients:       []string{"recipient@example.com"},
    Subject:          "Test Subject",
    TemplateFileName: "template.tmpl",
    Data: map[string]interface{}{
        "Name": "John Doe",
    },
}
Validation

The mailer module provides validation for email data to ensure required fields are present:

email := &mailer.Email{
    Recipients:       []string{},
    Subject:          "Test Subject",
    Body:             "Test Body",
    TemplateFileName: "test_template.tmpl",
    Data:             nil,
}

err := mailer.ValidateEmail(email)
if err != nil {
    // Handle validation error
}

License

This module is released under the MIT License. Feel free to use and modify it according to your needs. I don't care.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppError

type AppError string

AppError represents application-level errors.

const (
	ErrInvalidConfig AppError = "invalid app config"
)

Application-level errors

func (AppError) Error

func (e AppError) Error() string

type AuthError

type AuthError string

AuthError represents authentication-related errors.

func (AuthError) Error

func (e AuthError) Error() string

type Email

type Email struct {
	Recipients       []string               // Email recipients
	Subject          string                 // Email subject
	Body             string                 // Email body
	TemplateFileName string                 // Template file name
	Data             map[string]interface{} // Data for template parsing
}

Email contains email data.

If data is provided, the template will be parsed with it. Otherwise, it will be parsed with the struct below.

struct { Title string; Body string }

type EmailAuthConfig

type EmailAuthConfig struct {
	Host     string // Email provider's host
	Port     int    // Email provider's port
	Username string // Email provider's username
	Password string // Email provider's password
	From     string // Sender's email address
}

EmailAuthConfig contains email authentication data.

Example:

EmailAuthConfig{Host: "smtp.gmail.com", Port: 587, Username: "user", Password: "password", From: "me@gmail.com"}

type EmailError

type EmailError string

EmailError represents email-related errors.

const (
	ErrEmptyTo EmailError = "email recipient(s) is empty"
)

Email errors

func (EmailError) Error

func (e EmailError) Error() string

type Mailer

type Mailer interface {
	Validate(kind ValidateKind) error
	SendEmail(mail *Email) error
}

func NewMailer

func NewMailer(auth EmailAuthConfig, templateDir string) Mailer

type MailerConfig

type MailerConfig struct {
	Config      EmailAuthConfig
	TemplateDir string
	// contains filtered or unexported fields
}

MailerConfig holds email sending configuration.

func (*MailerConfig) SendEmail

func (g *MailerConfig) SendEmail(mail *Email) error

SendEmail sends an email to the specified recipients with the given subject, template, and data.

func (*MailerConfig) Validate

func (g *MailerConfig) Validate(kind ValidateKind) error

Validate validates the email data.

type ValidateKind

type ValidateKind string

Jump to

Keyboard shortcuts

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