email

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

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 13 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/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)
	}

	// 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 (
	"github.com/yangfei4913438/email"
	"log"
	"net/mail"
	"net/smtp"
)

func main() {
	// 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)
	}

	// 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, use_tls bool) error

Send sends the message.

func SendMail

func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte, use_tls bool) error

modified from net/smtp/smtp.go, add TLS support

Types

type Attachment

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

Attachment represents an email attachment.

type Message

type Message struct {
	From            mail.Address
	To              []string
	Cc              []string
	Bcc             []string
	ReplyTo         string
	Subject         string
	Body            string
	BodyContentType string
	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) 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