email

package module
v0.0.0-...-5f4e933 Latest Latest
Warning

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

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

README

email GoDoc Report card

An easy way to send emails with attachments in Go.

Forked from github.com/scorredoira/email, with these modifications:

  • RFC-2047-compliant splitting of attachment filenames and subject
  • Can send mail via /usr/sbin/sendmail

Install

go get github.com/casipw/email

Usage

package main

import (
    "github.com/casipw/email"
    "net/mail"
    "net/smtp"
)

func main() {
    m := email.NewMessage("Subject", "Body")
    m.From = mail.Address{Name: "Sender", Address: "from@example.com"}
    m.To = []string{"to@example.com"}
    if err := m.Attach("file.pdf"); err != nil {
        panic(err)
    }

    // pass it to sendmail
    email.Sendmail("sender@example.com", m)

    // or send it via SMTP
    auth := smtp.PlainAuth("", "from@example.com", "mypassword", "smtp.example.com")
    if err := email.Send("smtp.example.com:587", auth, m); err != nil {
        panic(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. Forked from https://github.com/scorredoira/email

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.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) error

Send sends the message via SMTP. Thus CRLF is used as line separator.

func Sendmail

func Sendmail(from string, m *Message) error

Sendmail sends the message via the /usr/sbin/sendmail interface. It uses LF as line separator, which is suitable for most MTAs on Linux/Unix.

qmail: "Unlike Sendmail, qmail requires locally-injected messages to use Unix newlines (LF only)."

postfix: "The SMTP record delimiter is CRLF. Postfix removes it (as well as invalid CR characters at the end of a record) while receiving mail via SMTP, and adds it when sending mail via SMTP."

sendmail: The system's line separator seems to be default, as the -bs flag changes that behavior.

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

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

func (*Message) BytesLF

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

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