mail

package module
v2.2.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 20 Imported by: 0

README

Go Simple Mail

The best way to send emails in Go with SMTP Keep Alive and Timeout for Connect and Send.

Go Doc Go Report

IMPORTANT This example is for version 2.2.0 and above.

Version 2.1.3 and below use "text/html" and "text/plain" in SetBody and AddAlternative Also 2.0.0 and below go to this doc https://gist.github.com/xhit/54516917473420a8db1b6fff68a21c99

Introduction

Go Simple Mail is a simple and efficient package to send emails. It is well tested and documented.

Go Simple Mail can only send emails using an SMTP server. But the API is flexible and it is easy to implement other methods for sending emails using a local Postfix, an API, etc.

Features

Go Simple Mail supports:

  • Multiple Attachments with path
  • Multiple Attachments in base64
  • Multiple Recipients
  • Priority
  • Reply to
  • Set other sender
  • Set other from
  • Embedded images
  • HTML and text templates
  • Automatic encoding of special characters
  • SSL and TLS
  • Unencrypted connection (not recommended)
  • Sending multiple emails with the same SMTP connection (Keep Alive or Persistent Connection)
  • Timeout for connect to a SMTP Server
  • Timeout for send an email
  • Return Path
  • Alternaive Email Body
  • CC and BCC
  • Add Custom Headers in Message
  • Send NOOP, RESET, QUIT and CLOSE to SMTP client

Documentation

https://godoc.org/github.com/xhit/go-simple-mail

Download

go get -u github.com/xhit/go-simple-mail

Usage

package main

import (
	"github.com/xhit/go-simple-mail"
	"log"
)

func main() {

	htmlBody :=
`<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Hello Gophers!</title>
	</head>
	<body>
		<p>This is the <b>Go gopher</b>.</p>
		<p><img src="cid:Gopher.png" alt="Go gopher" /></p>
		<p>Image created by Renee French</p>
	</body>
</html>`

	server := mail.NewSMTPClient()
	
	//SMTP Server
	server.Host = "smtp.example.com"
	server.Port = 587
	server.Username = "test@example.com"
	server.Password = "examplepass"
	server.Encryption = mail.EncryptionTLS
	
	//Variable to keep alive connection
	server.KeepAlive = false
	
	//Timeout for connect to SMTP Server
	server.ConnectTimeout = 10 * time.Second
	
	//Timeout for send the data and wait respond
	server.SendTimeout = 10 * time.Second
	
	//SMTP client
	smtpClient,err :=server.Connect()
	
	if err != nil{
		log.Fatal(err)
	}

	//New email simple html with inline and CC
	email := mail.NewMSG()

	email.SetFrom("From Example <nube@example.com>").
		AddTo("xhit@example.com").
		AddCc("otherto@example.com").
		SetSubject("New Go Email")

	email.SetBody(mail.TextHTML, htmlBody)

	email.AddInline("/path/to/image.png", "Gopher.png")

	//Call Send and pass the client
	err = email.Send(smtpClient)

	if err != nil {
		log.Println(err)
	} else {
		log.Println("Email Sent")
	}
}

Send multiple emails in same connection

	//Set your smtpClient struct to keep alive connection
	server.KeepAlive = true

	toList := [3]string{"to1@example1.com", "to3@example2.com", "to4@example3.com"}

	for _, to := range toList {
		//New email simple html with inline and CC
		email := mail.NewMSG()

		email.SetFrom("From Example <nube@example.com>").
			AddTo(to).
			SetSubject("New Go Email")

		email.SetBody(mail.TextHTML, htmlBody)

		email.AddInline("/path/to/image.png", "Gopher.png")

		//Call Send and pass the client
		err = email.Send(smtpClient)

		if err != nil {
			log.Println(err)
		} else {
			log.Println("Email Sent")
		}
	}

Documentation

Index

Constants

View Source
const (
	// EncryptionTLS sets encryption type to TLS when sending email
	EncryptionTLS encryption = iota
	// EncryptionSSL sets encryption type to SSL when sending email
	EncryptionSSL
	// EncryptionNone uses no encryption when sending email
	EncryptionNone
)
View Source
const (
	// EncodingQuotedPrintable sets the message body encoding to quoted-printable
	EncodingQuotedPrintable encoding = iota
	// EncodingBase64 sets the message body encoding to base64
	EncodingBase64
	// EncodingNone turns off encoding on the message body
	EncodingNone
)
View Source
const (
	// TextHTML sets body type to text/html in message body
	TextHTML contentType = iota
	// TextPlain sets body type to text/plain in message body
	TextPlain
)
View Source
const (
	// PriorityHigh sets the email priority to High
	PriorityHigh priority = iota
	// PriorityLow sets the email priority to Low
	PriorityLow
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Email

type Email struct {
	Charset    string
	Encoding   encoding
	Error      error
	SMTPServer *smtpClient
	// contains filtered or unexported fields
}

Email represents an email message.

func NewMSG

func NewMSG() *Email

NewMSG creates a new email. It uses UTF-8 by default.

func (*Email) AddAddresses

func (email *Email) AddAddresses(header string, addresses ...string) *Email

AddAddresses allows you to add addresses to the specified address header.

func (*Email) AddAlternative

func (email *Email) AddAlternative(contentType contentType, body string) *Email

AddAlternative allows you to add alternative parts to the body of the email message. This is most commonly used to add an html version in addition to a plain text version that was already added with SetBody.

func (*Email) AddAttachment

func (email *Email) AddAttachment(file string, name ...string) *Email

AddAttachment allows you to add an attachment to the email message. You can optionally provide a different name for the file.

func (*Email) AddAttachmentBase64

func (email *Email) AddAttachmentBase64(b64File string, name string) *Email

AddAttachmentBase64 allows you to add an attachment in base64 to the email message. You need provide a name for the file.

func (*Email) AddBcc

func (email *Email) AddBcc(addresses ...string) *Email

AddBcc adds a Bcc address. You can provide multiple addresses at the same time.

func (*Email) AddCc

func (email *Email) AddCc(addresses ...string) *Email

AddCc adds a Cc address. You can provide multiple addresses at the same time.

func (*Email) AddHeader

func (email *Email) AddHeader(header string, values ...string) *Email

AddHeader adds the given "header" with the passed "value".

func (*Email) AddHeaders

func (email *Email) AddHeaders(headers textproto.MIMEHeader) *Email

AddHeaders is used to add multiple headers at once

func (*Email) AddInline

func (email *Email) AddInline(file string, name ...string) *Email

AddInline allows you to add an inline attachment to the email message. You can optionally provide a different name for the file.

func (*Email) AddTo

func (email *Email) AddTo(addresses ...string) *Email

AddTo adds a To address. You can provide multiple addresses at the same time.

func (*Email) GetError

func (email *Email) GetError() error

GetError returns the first email error encountered

func (*Email) GetMessage

func (email *Email) GetMessage() string

GetMessage builds and returns the email message

func (*Email) Send

func (email *Email) Send(client *SMTPClient) error

Send sends the composed email

func (*Email) SetBody

func (email *Email) SetBody(contentType contentType, body string) *Email

SetBody sets the body of the email message.

func (*Email) SetDate

func (email *Email) SetDate(dateTime string) *Email

SetDate sets the date header to the provided date/time. The format of the string should be YYYY-MM-DD HH:MM:SS Time Zone.

Example: SetDate("2015-04-28 10:32:00 CDT")

func (*Email) SetFrom

func (email *Email) SetFrom(address string) *Email

SetFrom sets the From address.

func (*Email) SetPriority

func (email *Email) SetPriority(priority priority) *Email

SetPriority sets the email message priority. Use with either "High" or "Low".

func (*Email) SetReplyTo

func (email *Email) SetReplyTo(address string) *Email

SetReplyTo sets the Reply-To address.

func (*Email) SetReturnPath

func (email *Email) SetReturnPath(address string) *Email

SetReturnPath sets the Return-Path address. This is most often used to send bounced emails to a different email address.

func (*Email) SetSender

func (email *Email) SetSender(address string) *Email

SetSender sets the Sender address.

func (*Email) SetSubject

func (email *Email) SetSubject(subject string) *Email

SetSubject sets the subject of the email message.

type SMTPClient

type SMTPClient struct {
	Client      *smtpClient
	KeepAlive   bool
	SendTimeout time.Duration
}

SMTPClient represents a SMTP Client for send email

func (*SMTPClient) Close

func (smtpClient *SMTPClient) Close() error

Close closes the connection

func (*SMTPClient) Noop

func (smtpClient *SMTPClient) Noop() error

Noop send NOOP command to smtp client

func (*SMTPClient) Quit

func (smtpClient *SMTPClient) Quit() error

Quit send QUIT command to smtp client

func (*SMTPClient) Reset

func (smtpClient *SMTPClient) Reset() error

Reset send RSET command to smtp client

type SMTPServer

type SMTPServer struct {
	// From           string
	Encryption     encryption
	Username       string
	Password       string
	ConnectTimeout time.Duration
	SendTimeout    time.Duration
	Host           string
	Port           int
	KeepAlive      bool
}

SMTPServer represents a SMTP Server

func NewSMTPClient

func NewSMTPClient() *SMTPServer

NewSMTPClient returns the client for send email

func (*SMTPServer) Connect

func (server *SMTPServer) Connect() (*SMTPClient, error)

Connect returns the smtp client

Jump to

Keyboard shortcuts

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