mail

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2018 License: MIT Imports: 12 Imported by: 4

README

Mail __/ email sender written in Go

This simple package supports rich email messages, unix sendmail command and more.

The mail package provides an amazing API to work with.

Build Status License Releases Godocs Build Status Built with GoLang Platforms

Installation

The only requirement is the Go Programming Language, at least version 1.9.

$ go get -u -v github.com/kataras/mail

Stable release installation by go get gopkg.in/kataras/mail.v0

Getting Started

  • New returns a new, e-mail sender service
  • Mail#Send sends a (rich) email message
  • Mail#Subject("...") returns a Builder for the mail message which ends up with Send() error, SendUNIX() error
  • SendUNIX make use of the sendmail program of *nix OS, no Mail sender is needed
// New returns a new *Mail, which contains the `Send(...) error`
// and `Subject(...) *Builder` functions.
New(c Credentials) *Mail
Example
$ cat send-mail.go
package main

import "github.com/kataras/mail"

func main() {
    c := mail.Credentials{
        Addr:     "smtp.sendgrid.net:587",
        Username: "apikey",
        Password: "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",
    }

    message, err := mail.New(c)
    if err != nil {
        panic(err)
    }

    message.
        Subject("Subject").
        BodyString("Body").
        To("receipt@example.com", "receipt2@example.com").
        From("FromName", "from@example.com").
        Send()

    // Tip #1
    //
    // Alternative ways to set Body inside a Builder:
    // Body([]byte)
    // BodyReader(io.Reader)
    // BodyReadCloser(io.ReadCloser)

    // Tip #2
    //
    // If you want to re-use a Builder(= `message.Subject(...)`'s result) after the `Send`
    // then you have to call the Builder's `MarkSingleton()` before its `Send` execution.

    // Small:
    //
    // [ init time, once after the `mail.New(...)` ]
    // message.DefaultFrom = &mail.Address{"FromName", "from@example.com"}
    // [[ run time, many ]]
    // message.Subject("Subject").BodyString("Body").To("receipt@example.com").Send()
}

For the stable release use import gopkg.in/kataras/mail.v0 instead

$ go run send-mail.go
More examples

FAQ

Explore these questions or navigate to the community chat.

Versioning

Current: v0.0.1

Read more about Semantic Versioning 2.0.0

People

The author of the mail is @kataras.

Contributing

If you are interested in contributing to the mail project, please make a PR.

TODO
  • Add a simple CLI tool for sending emails
  • Read the specification for the email attachment and implement that.

License

This project is licensed under the MIT License. License file can be found here.

Documentation

Overview

Package mail is a simple email sender written in Go. Please refer to the https://github.com/kataras/mail/tree/master/_examples folder for more.

Index

Constants

View Source
const (
	// Version current semantic version of the `mail` package.
	Version = "0.0.1"
)

Variables

View Source
var ParseAddress = mail.ParseAddress

ParseAddress parses a string to an `Address`.

Usage: addr, err := mail.ParseAddress("Gerasimos <gerasimos@example.com>") addr.Name is "Gerasimos" and addr.Address is "gerasimos@example.com".

ParseAddress is just a shortcut of `net/mail#ParseAddress`.

Functions

func SendUNIX

func SendUNIX(from *Address, subject string, body []byte, to ...string) error

SendUNIX will call the "sendmail" unix command and send the e-mail based on the given subject, body and recipients (to). If the "sendmail" command is not part of the host's(current machine) programs then it will return an error. It resets the current Builder on finish.

Note that this function can be used from a *nux operating system host only, windows operating system doesn't support this. Don't forget to make sure to configure the machine's mail client before make use of this feature.

Types

type Address

type Address = mail.Address

Address is an alias of `net/mail#Address`.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is the builder of the e-mail headers and message body, it resets the current Builder instance each time `Send` is called to avoid memory allocations.

See `Mail#Subject`.

func (*Builder) AppendBody

func (b *Builder) AppendBody(bodyData []byte) *Builder

AppendBody adds more body to the body of the mail.

func (*Builder) Body

func (b *Builder) Body(body []byte) *Builder

Body sets the body of the mail.

func (*Builder) BodyReadCloser

func (b *Builder) BodyReadCloser(r io.ReadCloser) *Builder

BodyReadCloser same as `BodyReader` but it closes the reader at the end.

func (*Builder) BodyReader

func (b *Builder) BodyReader(r io.Reader) *Builder

BodyReader same as `Body` but it accepts an io.Reader to read the actual body from and set.

func (*Builder) BodyString

func (b *Builder) BodyString(body string) *Builder

BodyString sets a body like `Body` but it accepts a string instead of []byte.

func (*Builder) From

func (b *Builder) From(name, address string) *Builder

From is from address header, it's not required. If not setted then the `Mail.DefaultFrom` will be used instead.

Accepts two input arguments: name is the proper name; may be empty. address is the full address; user@domain.

func (*Builder) MarkSingleton

func (b *Builder) MarkSingleton() *Builder

MarkSingleton will make this Builder re-usable, even after the `Send` or `SendUNIX` functions.

func (*Builder) Send

func (b *Builder) Send() error

Send sends the e-mail based on the subject, body and recipients. It resets the current Builder on finish.

func (*Builder) SendUNIX

func (b *Builder) SendUNIX() error

SendUNIX will call the "sendmail" unix command and send the e-mail based on the subject, body and recipients. If the "sendmail" command is not part of the host's(current machine) programs then it will return an error. It resets the current Builder on finish.

Note that this function can be used from a *nux operating system host only, windows operating system doesn't support this. Don't forget to make sure to configure the machine's mail client before make use of this feature.

func (*Builder) Subject

func (b *Builder) Subject(subject string) *Builder

Subject ses the subject of the mail header.

func (*Builder) To

func (b *Builder) To(recipients ...string) *Builder

To adds a recipient to the mail header, it can be called multiple times.

type Credentials

type Credentials struct {
	// Addr is the server mail server's full host, IP or host:port,
	//
	// if port is missing then `:smtp` will be used.
	//
	// Required.
	Addr string
	// Username is the auth username@domain.com for the sender.
	//
	/// Required.
	Username string
	// Password is the auth password for the sender.
	//
	// Required.
	Password string
}

Credentials are the SMTP credentials.

type Mail

type Mail struct {
	Addr string
	// DefaultFrom is being used if from Address is missing from the
	// Send functions, it's the "Username <Username@host>".
	DefaultFrom *Address
	// Auth is exported so caller can change it to another.
	//
	// Defaults to smtp.PlainAuth based on the Credential's Addr, Username and Password.
	Auth smtp.Auth
}

Mail is the main structure of this package, it's the mail sender.

func New

func New(c Credentials) (*Mail, error)

New returns a new Mail instance based on the given `Credentials`.

func (*Mail) Send

func (m *Mail) Send(from *Address, subject string, body []byte, to ...string) error

Send sends an email to the recipient(s) the body can be in HTML format as well.

func (*Mail) Subject

func (m *Mail) Subject(subject string) *Builder

Subject returns thae `Builder` with a filled mail subject, from there the caller can build the e-mail and Send the e-mail from the `Builder` instance.

Directories

Path Synopsis
_examples
cmd

Jump to

Keyboard shortcuts

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