msgr

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 13 Imported by: 0

README

Fyro Mailer

Configuration

type NewMailerOpts struct {
	// Path to email layout, locales, and templates
	TemplatesDir  string
	DefaultFrom   string
	Provider      Provider
	DefaultLocale string
	// Dynamic data to be used in the layout
	LayoutData MailData
}

Letters

Each letter has its own templates and locales.

type RegisterLetterOpts struct {
	Name           string // must be unique and match folder name
	ExtraTemplates []string // index.html.tmpl is included, define extras here
}

Register added letters using:

mailer.RegisterLetter(RegisterLetterOpts{})

Templates

File structure should be as follows:

templates
  layout.html.tmpl // Root layout
  locale.en.yml
  locale.zh-cn.yml
  [entryName]/
    locale.en.yml
    locale.zh-cn.yml
    index.html.tmpl
    additional.html.tmpl

Locale files should be named locale.[lang].yml

Subject

Letter locale files must include a mandatory subject message entry, this is templated using data passed in.

# YAML
subject: Hello {{ .Name }}

Sending

type SendOpts struct {
	LetterName string
	Data       MailData `json:"data"`
	To         string
	From       string
	ReplyTo    string
	Subject    string // Leave blank to use subject from template
	Locale     string // Leave blank for default
}

Example

Check test/preview.go for a working example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoChannels    = errors.New("no channels found")
	ErrInvalidKind   = errors.New("invalid entry")
	ErrNoProviders   = errors.New("no providers found")
	ErrInvalidFormat = errors.New(`invalid format, needs to be "html" or "text"`)
	ErrNoLocaleFiles = errors.New("no locale files found")
)

Functions

func RenderHTML

func RenderHTML(opts RenderOpts) (string, error)

func RenderText

func RenderText(opts RenderOpts) (string, error)

Types

type AddMessageOpts

type AddMessageOpts struct {
	Name            string
	MailChannelOpts *MailChannelOpts
}

type Channel

type Channel int
const (
	MailChannel Channel = iota
	SMSChannel
)

func (Channel) String

func (c Channel) String() string

type ClientOpts

type ClientOpts struct {
	// Path to email layout, locales, and templates
	TemplatesRoot string
	DefaultFrom   string
	MailProvider  provider.MailProvider
	SMSProvider   provider.SMSProvider
	DefaultLocale string
	// Dynamic data to be used in the layout
	LayoutData MessageData
}

type ComposeOpts

type ComposeOpts struct {
	Message Message
	Channel Channel
	Format  RenderFormat
	Locale  string
	Data    MessageData
}

type MailChannelOpts

type MailChannelOpts struct {
	From    string
	ReplyTo string
}

type Message

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

func NewMessage

func NewMessage(opts NewMessageOpts) (*Message, error)

func (*Message) Localizer

func (msg *Message) Localizer(locale string) *i18n.Localizer

func (*Message) MailSubject

func (msg *Message) MailSubject(
	locale string, data MessageData,
) (string, error)

func (*Message) TemplateFiles

func (msg *Message) TemplateFiles(channel Channel, format RenderFormat) []string

type MessageData

type MessageData map[string]any

type Messenger

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

func NewClient

func NewClient(opts ClientOpts) (*Messenger, error)

func (*Messenger) AddMessage

func (msgr *Messenger) AddMessage(opts AddMessageOpts) error

func (*Messenger) Compose

func (msgr *Messenger) Compose(opts ComposeOpts) (string, error)

func (*Messenger) LayoutFile

func (msgr *Messenger) LayoutFile(channel Channel, format RenderFormat) string

func (*Messenger) Preview

func (msgr *Messenger) Preview(opts PreviewOpts) error

func (*Messenger) Send

func (msgr *Messenger) Send(opts SendOpts) error

type NewMessageOpts

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

type PreviewOpts

type PreviewOpts struct {
	MessageName string
	Data        MessageData
	Locale      string
}

type RenderFormat

type RenderFormat string
var (
	RenderKindText RenderFormat = "text"
	RenderKindHTML RenderFormat = "html"
)

type RenderOpts

type RenderOpts struct {
	Templates     []string
	Data          MessageData
	Locale        string
	LayoutBundle  *i18n.Bundle
	MessageBundle *i18n.Bundle
}

type SendOpts

type SendOpts struct {
	MessageName string
	MailTo      string
	SMSTo       string
	Data        MessageData
	Locale      string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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