mailer

package module
v0.0.0-...-42b7f01 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 9 Imported by: 2

README

Mailer

Mail solution

Usage

Initailize Mailer

Mailer will support multiple sender adaptors, it works similar, you need to initialize a Mailer first, then use it to send emails.

Here is how to use gomail to send emails

import (
	"github.com/ecletus/mailer"
	"github.com/ecletus/mailer/gomailer"
	gomail "gopkg.in/gomail.v2"
)

func main() {
	// Config gomail
	dailer := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
	sender, err := dailer.Dial()

	// Initialize Mailer
	Mailer := mailer.New(&mailer.Config{
		Sender: gomailer.New(&gomailer.Config{Sender: sender}),
	})
}
Sending Emails
import "net/mail"

func main() {
	Mailer.Send(mailer.Email{
		TO:          []mail.Address{{Address: "jinzhu@example.org", Name: "jinzhu"}},
		From:        &mail.Address{Address: "jinzhu@example.org"},
		Subject:     "subject",
		Text:        "text email",
		HTML:        "html email <img src='cid:logo.png'/>",
		Attachments: []mailer.Attachment{{FileName: "gomail.go"}, {FileName: "../test/logo.png", Inline: true}},
	})
}
Sending Emails with templates

Mailer is using Render to render email templates and layouts, please refer it for How-To.

Emails could have HTML and text version, when sending emails,

It will look up template hello.html.tmpl and layout application.html.tmpl from view paths, and render it as HTML version's content, and use template hello.text.tmpl and layout application.text.tmpl as text version's content.

If we haven't find the layout file, we will only render template as the content, and if we haven't find template, we will just skip that version, for example, if hello.text.tmpl doesn't exist, we will only send the HTML version.

Mailer.Send(
	mailer.Email{
		TO:      []mail.Address{{Address: Config.DefaultTo}},
		From:    &mail.Address{Address: Config.DefaultFrom},
		Subject: "hello",
	},
	mailer.Template{Name: "hello", Layout: "application", Data: currentUser},
)
Mailer View Paths

All templates and layouts should be located in app/views/mailers, but you could change or register more paths by customizing Mailer's AssetFS.

import "github.com/ecletus/assetfs"

func main() {
	assetFS := assetfs.AssetFS().NameSpace("mailer")
	assetFS.RegisterPath("mailers/views")

	Mailer := mailer.New(&mailer.Config{
		Sender: gomailer.New(&gomailer.Config{Sender: sender}),
		AssetFS: assetFS,
	})
}

Documentation

Index

Constants

View Source
const Key keyType = iota

Variables

View Source
var (
	Fallback     = mailer.Fallback
	WithReporter = mailer.WithReporter
)

Functions

func Set

func Set(to *maps.Map, value *Mailer)

Types

type Config

type Config struct {
	DefaultEmailTemplate *Email
	AssetFS              assetfs.Interface
	Sender               SenderInterface
	From                 *mail.Address
	*render.Render
}

Config mailer config

type Email

type Email = mailer.Email

type GetSiteMailer

type GetSiteMailer func(site *core.Site) *SiteMailer

type Mailer

type Mailer struct {
	*Config
}

Mailer mailer struct

func Get

func Get(m maps.Map) (value *Mailer, ok bool)

func MustGet

func MustGet(m maps.Map) *Mailer

func New

func New(config *Config) *Mailer

New initialize mailer

func (Mailer) Render

func (mailer Mailer) Render(t Template, langs ...string) (email Email, err error)

Render render template

func (Mailer) Send

func (mailer Mailer) Send(site *core.Site, email *Email, templates ...Template) (err error)

Send send email

func (Mailer) WithSender

func (mailer Mailer) WithSender(Sender SenderInterface) Mailer

WithSender set sender now

type SendHandler

type SendHandler = mailer.SendHandler

type SenderFunc

type SenderFunc = mailer.SenderFunc

type SenderInterface

type SenderInterface = mailer.Sender

type SendhandlerFunc

type SendhandlerFunc = mailer.SendHandlerFunc

type SiteMailer

type SiteMailer struct {
	Site *core.Site
}

func FromSite

func FromSite(site *core.Site) *SiteMailer

func (SiteMailer) Mailer

func (this SiteMailer) Mailer() *Mailer

func (SiteMailer) Send

func (this SiteMailer) Send(email *Email, templates ...Template) (err error)

type Template

type Template struct {
	Name    string
	Layout  string
	Data    interface{}
	Context *core.Context
	// contains filtered or unexported fields
}

Template email template

func (Template) Funcs

func (tmpl Template) Funcs(funcMap ...template.FuncMap) Template

Funcs set template's funcs

func (Template) FuncsValues

func (tmpl Template) FuncsValues(funcValues ...template.FuncValues) Template

FuncsValues set template's funcs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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