...
Tawesoft Logo

Package email

import "tawesoft.co.uk/go/legacy/email"
Overview
Index

Overview ▾

Package email implements the formatting of multipart RFC 2045 e-mail messages, including headers, attachments, HTML email, and plain text.

File attachments are lazy, and read from disk only at the time the e-mail is sent.

Package Stability

It is likely that this package will change at some point as follows:

* A Message-ID header will no longer be implicitly generated for a Message.

* The Envelope struct will no longer have a message field - instead, use an (Envelope, Message) 2-tuple where you need both of these items.

This is a breaking change. As such, when this happens, the old behaviour will be made available at tawesoft.co.uk/go/legacy/email.

Example

This example demonstrates formatting an email message and printing it to a Writer (here, `os.Stdout`).

package main

import (
    "net/mail"
    "os"

    "tawesoft.co.uk/go/email"
)

func main() {
    var eml = email.Message{
        From:  mail.Address{"Alan Turing", "turing.alan@example.org"},
        To:  []mail.Address{{"Grace Hopper", "amazing.grace@example.net"}},
        Bcc: []mail.Address{{"BCC1", "bcc1@example.net"}, {"BCC2", "bbc2@example.net"}},
        Subject: "Computer Science is Cool! ❤",
        Text: `This is a test email!`,
        Html: `<!DOCTYPE html><html lang="en"><body><p>This is a test email!</p></body></html>`,
        Attachments: []*email.Attachment{
            email.FileAttachment("Entscheidungsproblem.pdf"),
            email.FileAttachment("funny-cat-meme.png"),
        },
        Headers: mail.Header{
            "X-Category": []string{"newsletter", "marketing"},
        },
    }

    var err = eml.Print(os.Stdout)
    if err != nil { panic(err) }
}

Package Information

License: MIT (see LICENSE.txt)

Stable: no

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/legacy/email

type Attachment

Attachment defines an e-mail attachment. They are read lazily.

type Attachment struct {
    // Filename is the name to give the attachment in the email
    Filename string

    // Mimetype e.g. "application/pdf".
    // If an empty string, then attempts to automatically detect based on filename extension.
    Mimetype string

    // Reader is a lazy reader. e.g. return the result of os.Open.
    Reader func() (io.Reader, error)
}

func FileAttachment

func FileAttachment(src string) *Attachment

FileAttachment returns an Attachment from a file path. The file at that path is lazily opened at the time the attachment is sent.

type Envelope

Envelope wraps an Email with some SMTP protocol information for extra control.

type Envelope struct {
    // From is the sender. Usually this should match the Email From address. In the cause of autoreplies (like "Out of
    // Office" or bounces or delivery status notifications) this should be an empty string to stop an infinite loop
    // of bounces)
    From string

    // Data is just a pointer to an Email struct
    Data *Message

    // ReceiptTo is normally automatically generated from the Email To/CC/BCC addresses
    ReceiptTo []string
}

type Message

Messages defines a multipart e-mail (including headers, HTML body, plain text body, and attachments).

Use the `headers` parameter to specify additional headers. Note that `mail.Header` maps keys to a *list* of strings, because some headers may appear multiple times.

type Message struct {
    From        mail.Address
    To          []mail.Address
    Cc          []mail.Address
    Bcc         []mail.Address
    Subject     string
    Headers     mail.Header
    Html        string
    Text        string
    Attachments []*Attachment
}

func (*Message) Print

func (e *Message) Print(dest io.Writer) error

Print writes a multipart Email to dest.

NOTE: the maximum length of a email message line is 998 characters. If sending emails to multiple addresses the caller should keep this limit in mind and divide the addresses over multiple calls to this function.