email

package module
v0.0.0-...-f61b042 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: MIT Imports: 10 Imported by: 0

README

email Travis-CI GoDoc Report card

An easy way to send emails with attachments in Go

Install

go get github.com/wilsonsouza/email

Usage

package email_test

import (
	"log"
	"net/mail"
	"net/smtp"

	"github.com/wilsonsouza/email"
)

func Example() {
	// compose the message
	message := email.CreateMessage("Hi", "this is the body", email.EmailHTMLType)
	message.From = mail.Address{Name: "From", Address: "from@example.com"}
	message.To = []string{"to@example.com"}

	// add attachments
	if failure := m.Attach("email.go"); failure != nil {
		log.Fatal(failure)
	}

	// add headers
	message.AddHeader("X-CUSTOMER-id", "xxxxx")

	// send it
	authentication := smtp.PlainAuth("", "from@example.com", "pwd", "smtp.zoho.com")
	if failure := email.Send("smtp.zoho.com:587", authentication, message); failure != nil {
		log.Fatal(failure)
	}
}

Html

	// use the html constructor
	message := email.CreateMessage("Hi", "this is the body", email.EmailHTMLType)

Inline

	// use Inline to display the attachment inline.
	if failure := message.Inline("main.go"); failure != nil {
		log.Fatal(failure)
	}

Documentation

Overview

Package email allows to send emails with attachments.

With base in scorredoira project

(c) 2020 2ww Engenharia de Sistemas

wilson.souza
Example
// compose the message
message := email.CreateMessage("Hi", "this is the body", email.EmailHtml)
message.From = mail.Address{Name: "From", Address: "from@example.com"}
message.AddTo(mail.Address{Name: "someToName", Address: "to@example.com"})
message.AddCc(mail.Address{Name: "someCcName", Address: "cc@example.com"})
message.AddBcc(mail.Address{Name: "someBccName", Address: "bcc@example.com"})
// add attachments
failure := message.Attach("email.go")
//
if failure != nil {
	log.Fatal(err)
}

// add headers
message.AddHeader("X-CUSTOMER-id", "HTML-Email-Formatted")

// send it
authentication := smtp.PlainAuth("", "wilson@example.com", "pwd", "smtp.zolla.com")
failure := email.Send("smtp.zolla.com:587", authentication, message)
//
if failure != nil {
	log.Fatal(err)
}
Output:

Index

Examples

Constants

View Source
const (
	EmailTextType = "text/plain"
	EmailHTMLType = "text/html"
)

define email body

Variables

This section is empty.

Functions

func Send

func Send(address string, authentication smtp.Auth, message *Message) error

Send sends the message.

Types

type Attachment

type Attachment struct {
	Filename string
	Data     []byte
	Inline   bool
}

Attachment represents an email attachment.

type Header struct {
	Key   string
	Value string
}

Header represents an additional email header.

type Message

type Message struct {
	From            mail.Address
	To              []string
	Cc              []string
	Bcc             []string
	ReplyTo         string
	Subject         string
	Body            string
	BodyContentType string
	Headers         []Header
	Attachments     map[string]*Attachment
}

Message represents a smtp message.

func CreateMessage

func CreateMessage(subject, body, contenttype string) *Message

func (*Message) AddBcc

func (m *Message) AddBcc(address mail.Address) []string

func (*Message) AddCc

func (m *Message) AddCc(address mail.Address) []string

func (*Message) AddHeader

func (m *Message) AddHeader(key string, value string) Header

Ads a Header to message

func (*Message) AddTo

func (m *Message) AddTo(address mail.Address) []string

func (*Message) Attach

func (m *Message) Attach(file string, inline bool) error

Attach attaches a file.

func (*Message) AttachBuffer

func (m *Message) AttachBuffer(filename string, buf []byte, inline bool) error

AttachBuffer attaches a binary attachment.

func (*Message) Inline

func (m *Message) Inline(file string) error

Inline includes a file as an inline attachment.

func (*Message) ToBytes

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

Bytes returns the mail data in bytes array

func (*Message) ToList

func (m *Message) ToList() []string

Tolist returns all the recipients of the email

Jump to

Keyboard shortcuts

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