Code Monkey home page Code Monkey logo

mailer's Introduction

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/qor/mailer"
	"github.com/qor/mailer/gomailer"
	"github.com/go-gomail/gomail"
)

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: "[email protected]", Name: "jinzhu"}},
		From:        &mail.Address{Address: "[email protected]"},
		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/qor/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,
	})
}

mailer's People

Contributors

jinzhu avatar cryptix avatar

Watchers

James Cloos avatar fredbi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.