gopostal

package module
v0.0.0-...-9574e5e Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2015 License: MIT Imports: 9 Imported by: 0

README

gopostal

Simple email library for creating and sending smtp mail - plain text, html, cc/bcc recipients and attachments are supported.

Default configurations for Gmail and Sendgrid are supported.

Installation

Install via the go get tool:

go get github.com/pcrawfor/gopostal

Usage

Quick use:

Sending basic mail via Gmail:

import (
  "github.com/pcrawfor/gopostal"
  "log"
)

// Send mail via Gmail with both and text and html content    
m := gopostal.NewGmailMailer("some_user", "some_pass")

subject := "Hi there!"
content := "Hi Paul, This is email sent from go code how are you today?"
html_content := "<h2>Hi Paul,</h2> <p>This is email sent from go code how are you today?</p>"

err := m.SendMail("some_to@test.com", "some_from@othertest.com", subject, content, html_content)
if err != nil {
  log.Fatal(err)
}

Sending basic email via any service:

import (
  "github.com/pcrawfor/gopostal"
  "log"
)

// Similarly you can configure the mail service info:
// using: NewMailer(identity, username, password, host, port string)
m := gopostal.NewMailer("", "some_user", "some_password", "smtp.sendgrid.net", "25")

subject := "Hi there!"
content := "Hi Paul, This is email sent from go code how are you today?"
html_content := "<h2>Hi Paul,</h2> <p>This is email sent from go code how are you today?</p>"

err := m.SendMail("some_to@test.com", "some_from@othertest.com", subject, content, html_content)    

if err != nil {
  log.Fatal(err)
}
Building messages and sending them

Building a more complicated message and sending it:

// Create a mailer and a message with more recipients/info
m := gopostal.NewMailer("", "some_user", "some_password", "smtp.sendgrid.net", "25")

subject := "Hi there!"
content := "Hi Paul, This is email sent from go code how are you today?"
html_content := "<h2>Hi Paul,</h2> <p>This is email sent from go code how are you today?</p>"

// build the message
msg := m.NewMessage("some_to@test.com", "some_from@othertest.com", subject, content, html_content)
msg.AddTo("other_recipient@test.com")
msg.AddCc("somecc@test.com")
msg.AddBcc("somebcc@test.com")

// send the message
err := m.Send(msg)
if err != nil {
  log.Fatal(err)
}
Adding attachments

In Progress.

Credits

Based in part on work done by:

License

See repo LICENSE file.

Documentation

Overview

Package gopostal - Supports Sending Text and HTML based emails. Send email as plain text or html.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mailer

type Mailer struct {
	Identity string
	Username string
	Password string
	Host     string
	Port     string
}

Mailer represents a mail sender object

func NewGmailMailer

func NewGmailMailer(username, password string) *Mailer

NewGmailMailer is a shortcut constructor for a Mailer with a gmail connection

func NewMailer

func NewMailer(identity, username, password, host, port string) *Mailer

NewMailer returns an instance of Mailer with the passed in connnection configuration options

func NewSendGridMailer

func NewSendGridMailer(username, password string) *Mailer

NewSendGridMailer is a shortcut constructor for a Mailer with a sendgrid connection

func (*Mailer) NewMessage

func (m *Mailer) NewMessage(to, from, subject, body, htmlBody string) (*Message, error)

NewMessage returns a new Message object build with the to, from, subject, body and html body provided

func (*Mailer) Send

func (m *Mailer) Send(msg Message) error

Send will send the given Message

func (*Mailer) SendMail

func (m *Mailer) SendMail(to, from, subject, body, htmlBody string) error

SendMail sends email to the to address populating the from, body and htmlbody provided

type Message

type Message struct {
	From mail.Address
	To   []mail.Address

	Subject  string
	TextBody string
	IsText   bool
	HtmlBody string
	IsHtml   bool
	Headers  map[string]string
	// contains filtered or unexported fields
}

Message represents an email message containing the sender to, the from, cc and bcc recipients and all content for the message including text and html body content

func (*Message) AddBcc

func (m *Message) AddBcc(bcc string)

AddBcc appends a bcc address to the message

func (*Message) AddCc

func (m *Message) AddCc(cc string)

AddCc appends a cc address to the message

func (*Message) AddHeader

func (m *Message) AddHeader(name, value string)

AddHeader appends a bcc address to the message

func (*Message) AddTo

func (m *Message) AddTo(to string)

AddTo appends a to address to the message

func (*Message) Bytes

func (m *Message) Bytes() []byte

Bytes returns a byte array for the email content

Jump to

Keyboard shortcuts

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