email

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

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

Go to latest
Published: Nov 7, 2019 License: MIT Imports: 10 Imported by: 0

README

PROJECT DISCONTINUED

This repository only exists for archival purposes.


email Travis-CI GoDoc Report card

An easy way to send emails with attachments in Go

Install

go get github.com/scorredoira/email

Usage

package email_test

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

	"github.com/scorredoira/email"
)

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

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

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

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

Html

	// use the html constructor
	m := email.NewHTMLMessage("Hi", "this is the body")

Inline

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

Documentation

Overview

Package email allows to send emails with attachments.

Example
package main

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

	"github.com/scorredoira/email"
)

func main() {
	// compose the message
	m := email.NewMessage("Hi", "this is the body")
	m.From = mail.Address{Name: "From", Address: "from@example.com"}
	m.AddTo(mail.Address{Name: "someToName", Address: "to@example.com"})
	m.AddCc(mail.Address{Name: "someCcName", Address: "cc@example.com"})
	m.AddBcc(mail.Address{Name: "someBccName", Address: "bcc@example.com"})

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

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

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(addr string, auth smtp.Auth, m *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 NewHTMLMessage

func NewHTMLMessage(subject string, body string) *Message

NewHTMLMessage returns a new Message that can compose an HTML email with attachments

func NewMessage

func NewMessage(subject string, body string) *Message

NewMessage returns a new Message that can compose an email with attachments

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) 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) Bytes

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

Bytes returns the mail data

func (*Message) Inline

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

Inline includes a file as an inline attachment.

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