email

package module
v0.0.0-...-8e3db6f Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2014 License: MIT Imports: 17 Imported by: 0

README

email

Build Status

Robust and flexible email library for Go

Email for humans

The email package is designed to be simple to use, but flexible enough so as not to be restrictive. The goal is to provide an email interface for humans.

The email package currently supports the following:

  • From, To, Bcc, and Cc fields
  • Email addresses in both "test@example.com" and "First Last <test@example.com>" format
  • Text and HTML Message Body
  • Attachments
  • Read Receipts
  • Custom headers
  • More to come!
Installation

go get github.com/phelian/email

Note: Requires go version 1.1 and above

Examples
Sending email using Gmail using predefined configuration
SetConfig(Config{From: "test@gmail.com", Server: "smtp.gmail.com", Port: 587, SMTPPassword: "password123", SMTPUsername: "smtp.gmail.com"})
e := NewEmail()
e.To = []string{"test@example.com"}
e.Bcc = []string{"test_bcc@example.com"}
e.Cc = []string{"test_cc@example.com"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
Send(e)
Another way is to store config on file
ReadConfig("./config.json")
Sending email using Gmail
e := email.NewEmail()
e.From = "Jordan Wright <test@gmail.com>"
e.To = []string{"test@example.com"}
e.Bcc = []string{"test_bcc@example.com"}
e.Cc = []string{"test_cc@example.com"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!")
e.HTML = []byte("<h1>Fancy HTML is supported, too!</h1>")
SendSmtpAddr(e, "smtp.gmail.com:587", smtp.PlainAuth("", "test@gmail.com", "password123", "smtp.gmail.com"))
Another Method for Creating an Email

You can also create an email directly by creating a struct as follows:

e := &email.Email {
	To: []string{"test@example.com"},
	From: "Jordan Wright <test@gmail.com>",
	Subject: "Awesome Subject",
	Text: []byte("Text Body is, of course, supported!"),
	HTML: []byte("<h1>Fancy HTML is supported, too!</h1>"),
	Headers: textproto.MIMEHeader{},
}
Attaching a File
e := NewEmail()
e.AttachFile("test.txt")
Documentation

http://godoc.org/github.com/phelian/email

Other Sources

Forked from https://github.com/jordan-wright/email Sections inspired by the handy gophermail project.

Contributors

I'd like to thank all the contributors and maintainers of this package.

A special thanks goes out to Jed Denlea jeddenlea for his numerous contributions and optimizations.

Documentation

Overview

Package email is designed to provide an "email interface for humans." Designed to be robust and flexible, the email package aims to make sending email easy without getting in the way.

Index

Constants

View Source
const (
	// MaxLineLength is the maximum line length per RFC 2045
	MaxLineLength = 76
)

Variables

This section is empty.

Functions

func ReadConfig

func ReadConfig(path string) error

ReadConfig reads json file path and try to unmashal it to config

func Send

func Send(e *Email) error

Send sends email using the values specified in config

func SendSMTP

func SendSMTP(e *Email, addr string, auth smtp.Auth) error

SendSMTP sends email to specified smtp address using given auth

func SendSMTPWithoutAuth

func SendSMTPWithoutAuth(e *Email, smtpServer string) error

SendSMTPWithoutAuth sends email to specified smtpServer without trying to authenticate

func SetConfig

func SetConfig(newConfig Config)

SetConfig store a global config setting

Types

type Attachment

type Attachment struct {
	Filename string
	Header   textproto.MIMEHeader
	Content  []byte
}

Attachment is a struct representing an email attachment. Based on the mime/multipart.FileHeader struct, Attachment contains the name, MIMEHeader, and content of the attachment in question

type Config

type Config struct {
	From         string `json:"from"`
	SMTPUsername string `json:"smtp_username"`
	SMTPPassword string `json:"smtp_password"`
	Server       string `json:"server"`
	Port         int64  `json:port`
}

Config Stores default values

func GetConfig

func GetConfig() *Config

GetConfig returns current global config settings

type Email

type Email struct {
	From        string
	To          []string
	Bcc         []string
	Cc          []string
	Subject     string
	Text        []byte // Plaintext message (optional)
	HTML        []byte // Html message (optional)
	Headers     textproto.MIMEHeader
	Attachments []*Attachment
	ReadReceipt []string
}

Email is the type used for email messages

func NewEmail

func NewEmail() *Email

NewEmail creates an Email, and returns the pointer to it. Default from to config's from is set.

func (*Email) Attach

func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error)

Attach is used to attach content from an io.Reader to the email. Required parameters include an io.Reader, the desired filename for the attachment, and the Content-Type The function will return the created Attachment for reference, as well as nil for the error, if successful.

func (*Email) AttachFile

func (e *Email) AttachFile(filename string) (a *Attachment, err error)

AttachFile is used to attach content to the email. It attempts to open the file referenced by filename and, if successful, creates an Attachment. This Attachment is then appended to the slice of Email.Attachments. The function will then return the Attachment for reference, as well as nil for the error, if successful.

func (*Email) Bytes

func (e *Email) Bytes() ([]byte, error)

Bytes converts the Email object to a []byte representation, including all needed MIMEHeaders, boundaries, etc.

func (*Email) SetHTML

func (e *Email) SetHTML(text string)

SetHTML adds string as email html message

func (*Email) SetMessage

func (e *Email) SetMessage(text string)

SetMessage adds string as email message

type MailBox

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

MailBox type

func NewMailBox

func NewMailBox() *MailBox

NewMailBox returns a mailbox with own queue

func (*MailBox) Close

func (m *MailBox) Close()

Close mailbox, use closer to avoid possible race condition. Throws away all messages left in pipe

func (*MailBox) Put

func (m *MailBox) Put(e Email) error

Put new email in mail queue

Jump to

Keyboard shortcuts

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