mail

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewQueuedMail

func NewQueuedMail(msg *Message, driver string) *queue.GenericJob[QueuedMailPayload]

NewQueuedMail creates a new background job for sending an email.

Types

type Attachment

type Attachment struct {
	Name    string
	Content []byte
	MIME    string
}

Attachment represents a file attached to an email.

type LogMailer

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

LogMailer implements the Mailer interface by writing emails to a local file/log.

func NewLogMailer

func NewLogMailer(logDir string) *LogMailer

NewLogMailer creates a new LogMailer.

func (*LogMailer) Send

func (m *LogMailer) Send(ctx context.Context, msg *Message) error

Send writes the email to a file in the log directory.

type Mailable

type Mailable interface {
	// Subject returns the email subject line.
	Subject() string
	// From returns the sender address (overrides mailer default if non-empty).
	From() string
	// To returns the list of recipient addresses.
	To() []string
	// Template returns the template name (without extension) relative to the
	// email template directory (e.g. "emails/welcome").
	Template() string
	// Data returns the template data map.
	Data() map[string]any
}

Mailable is the interface for structured, layout-aware HTML emails. Implement this to get automatic layout wrapping, data interpolation, and seamless integration with the notification package.

Example:

type WelcomeMail struct {
    User schema.User
}

func (m *WelcomeMail) Subject() string         { return "Welcome to Astra!" }
func (m *WelcomeMail) From() string            { return "noreply@example.com" }
func (m *WelcomeMail) To() []string            { return []string{m.User.Email} }
func (m *WelcomeMail) Template() string        { return "emails/welcome" }
func (m *WelcomeMail) Data() map[string]any    { return map[string]any{"User": m.User} }

type MailableLayout

type MailableLayout interface {
	Mailable
	// Layout returns the layout template name relative to the email template
	// directory (e.g. "layouts/email"). Return "" to disable layout.
	Layout() string
}

MailableLayout optionally implemented by a Mailable to override the layout. If not implemented, the mailer's default layout is used.

type MailableSender

type MailableSender interface {
	SendMailable(m Mailable) error
}

MailableSender can send Mailable instances. Combine with TemplateMailer to get HTML rendering + delivery.

type Mailer

type Mailer interface {
	Send(ctx context.Context, msg *Message) error
}

Mailer defines the interface for sending emails.

type Message

type Message struct {
	From        string
	To          []string
	Subject     string
	Body        string
	HTML        string
	Attachments []Attachment
}

Message represents an email message.

type QueuedMailPayload

type QueuedMailPayload struct {
	Message *Message
	Driver  string
}

QueuedMailPayload holds the data for a background email task.

type ResendMailer

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

ResendMailer implements the Mailer interface using Resend.com.

func NewResendMailer

func NewResendMailer(cfg config.MailConfig, emitter *event.Emitter) *ResendMailer

NewResendMailer creates a new ResendMailer.

func (*ResendMailer) Send

func (m *ResendMailer) Send(ctx context.Context, msg *Message) error

Send sends an email via Resend HTTP API.

type SMTPMailer

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

SMTPMailer implements the Mailer interface using SMTP.

func NewSMTPMailer

func NewSMTPMailer(cfg config.MailConfig, emitter *event.Emitter) *SMTPMailer

NewSMTPMailer creates a new SMTPMailer.

func (*SMTPMailer) Send

func (m *SMTPMailer) Send(ctx context.Context, msg *Message) error

Send sends an email using SMTP.

type TemplateMailer

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

TemplateMailer wraps a base Mailer and adds HTML template rendering for Mailable.

func NewTemplateMailer

func NewTemplateMailer(base Mailer, opts ...TemplateMailerOption) *TemplateMailer

NewTemplateMailer creates a TemplateMailer that renders Mailable into HTML before handing off to the underlying Mailer.

func (*TemplateMailer) QueueMailable

func (tm *TemplateMailer) QueueMailable(m Mailable) (*queue.GenericJob[QueuedMailPayload], error)

QueueMailable renders the mailable and returns a background job to send it.

func (*TemplateMailer) Send

func (tm *TemplateMailer) Send(ctx context.Context, msg *Message) error

Send implements mail.Mailer so TemplateMailer can be used anywhere Mailer is expected. It delegates raw (pre-built) messages directly to the underlying mailer.

func (*TemplateMailer) SendMailable

func (tm *TemplateMailer) SendMailable(m Mailable) (*Message, error)

SendMailable renders the Mailable's template (with optional layout wrapping) and then returns the resulting message.

type TemplateMailerOption

type TemplateMailerOption func(*TemplateMailer)

TemplateMailerOption configures a TemplateMailer.

func WithDefaultFrom

func WithDefaultFrom(from string) TemplateMailerOption

WithDefaultFrom sets the default sender address.

func WithDefaultLayout

func WithDefaultLayout(layout string) TemplateMailerOption

WithDefaultLayout sets the default layout template for all emails. Set to "" to disable layout wrapping by default.

func WithMailExtension

func WithMailExtension(ext string) TemplateMailerOption

WithMailExtension sets the template file extension (default: ".html").

func WithMailFS

func WithMailFS(filesystem fs.FS) TemplateMailerOption

WithMailFS sets the filesystem for email templates.

type TemplateRenderer

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

TemplateRenderer handles HTML email templates.

func NewTemplateRenderer

func NewTemplateRenderer(fileSystem fs.FS) *TemplateRenderer

NewTemplateRenderer creates a new TemplateRenderer.

func (*TemplateRenderer) Render

func (r *TemplateRenderer) Render(name string, data any) (string, error)

Render renders a template with the given data.

Jump to

Keyboard shortcuts

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