gomail

package module
v0.0.0-...-92be0a3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2013 License: MIT Imports: 15 Imported by: 0

README

gomail

Render plain text or html email body from Go template and send it. It supports TLS and written in Go.

Here is an example:

package main

import (
  "fmt"

  "github.com/arkxu/gomail"
)

func main() {
  // init the mailer and tell the smtp server information
  mailer := &gomail.Mailer{Server: "smtp.gmail.com", Port: 587, UserName: "your gmail", Password: "your pwd"}

  // set the default sender. this is optional(you can set it in the message itself)
  mailer.Sender = &gomail.Sender{From: "foo@bar.com"}

  // configure the template folder, the file with extension .txt will be rendered as plain text email body
  // and the file with extension .html will be rendered as html email body
  err := gomail.TemplateFolder("templates")
  if err != nil {
    panic(err)
  }

  // arguments used for template rendering
  var args = make(map[string]interface{})
  args["world"] = "世界"
  args["user"] = struct {
    Name string
    Link string
  }{
    "Go",
    "http://golang.org",
  }

  // init the message, set the CC or BCC, Subject or body
  message := &gomail.Message{To: []string{"anyone@foo.com"}, Subject: "from template 6", Cc: []string{"bar@foo.com"}}

  // set the template file for this message
  err = message.RenderTemplate("helloWorld", args)
  if err != nil {
    fmt.Println(err)
  }

  // send the message out
  err = mailer.SendMessage(message)
  if err != nil {
    fmt.Println(err)
  }
}

In order to make it work, you need to create a folder templates and put the template file in there.

TemplateFolder() is not mandatory if the email body is passed in, instead of rendered. Please check out the test cases for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MailTemplate *template.Template
View Source
var (
	NewLine string = "\r\n"
)

Functions

func Send

func Send(c *smtp.Client, message *Message) (err error)

Send send message through the client

func TemplateFolder

func TemplateFolder(templateFolder string) error

func Transport

func Transport(address string, port int, host string, a smtp.Auth) (*smtp.Client, error)

Transport initialize the smtp client

Types

type MailSender

type MailSender interface {
	SendMessage(messages ...*Message) (err error)
}

type Mailer

type Mailer struct {
	Server   string
	Port     int
	UserName string
	Password string
	Host     string    // This is optional, only used if you want to tell smtp server your hostname
	Auth     smtp.Auth // This is optional, only used if Authentication is not plain
	Sender   *Sender   // This is optional, only used if the From/ReplyTo does not specified in the message
}

func (*Mailer) SendMessage

func (m *Mailer) SendMessage(messages ...*Message) (err error)

Send the given email messages using this Mailer.

type Message

type Message struct {
	From      string
	ReplyTo   string
	To        []string
	Cc        []string
	Bcc       []string
	Subject   string
	PlainBody *bytes.Buffer
	HtmlBody  *bytes.Buffer
}

func NewHtmlMessage

func NewHtmlMessage(to []string, subject string, body string) *Message

NewHtmlMessage create a html message

func NewTextAndHtmlMessage

func NewTextAndHtmlMessage(to []string, subject string, plainBody string, htmlBody string) *Message

NewTextAndHtmlMessage create a message contains both plain text and html message

func NewTextMessage

func NewTextMessage(to []string, subject string, body string) *Message

NewTextMessage create a plain text message

func (*Message) RenderData

func (m *Message) RenderData() (data []byte, err error)

RenderData render the whole email body

func (*Message) RenderTemplate

func (m *Message) RenderTemplate(templatePath string, args map[string]interface{}) error

RenderTemplate renders the message body from the template and input parameters, the change is inline the message

type Sender

type Sender struct {
	From    string
	ReplyTo string
}

Jump to

Keyboard shortcuts

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