email

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

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 8 Imported by: 0

README

MIME email builder for Golang

Text only email:

b := email.NewEmailBuilder()
b.SetFrom("hello@example.com")
b.SetTo([]string{
	"alice@example.com",
	"Bob <bob@example.com>",
})
b.SetSubject("See you tomorrow")

// Add custom headers
b.Headers.Set("Reply-To", "hello@example.com")
b.Headers.Set("Return-Path", "bounces@example.com")

b.SetPlainCharset("utf-8")
b.EncodeBase64Plain([]byte("See you tomorrow"))

w := &bytes.Buffer{}
err := b.Write(w)
if err != nil {
	// handle error
}
msg := w.String()
fmt.Println(msg)

The result is:

From: hello@example.com
Message-Id: myid
Reply-To: hello@example.com
Return-Path: bounces@example.com
Subject: See you tomorrow
To: alice@example.com, Bob <bob@example.com>
Date: Mon, 02 May 2022 19:51:17 +0200
Mime-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset=utf-8

U2VlIHlvdSB0b21vcnJvdw==

HTML only email:

b := email.NewEmailBuilder()
b.SetFrom("hello@example.com")
b.SetTo([]string{
	"alice@example.com",
	"Bob <bob@example.com>",
})
b.SetSubject("See you tomorrow")

// Add custom headers
b.Headers.Set("Reply-To", "hello@example.com")
b.Headers.Set("Return-Path", "bounces@example.com")

b.SetHTMLCharset("utf-8")
b.EncodeQuotedHTML([]byte("<p>See you tomorrow</p>"))

w := &bytes.Buffer{}
err := b.Write(w)
if err != nil {
	// handle error
}
msg := w.String()
fmt.Println(msg)

The result is:

From: hello@example.com
Message-Id: myid
Reply-To: hello@example.com
Return-Path: bounces@example.com
Subject: See you tomorrow
To: alice@example.com, Bob <bob@example.com>
Date: Mon, 02 May 2022 19:51:17 +0200
Mime-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=utf-8

<p>See you tomorrow</p>

Text and HTML email:

b := email.NewEmailBuilder()
b.SetFrom("hello@example.com")
b.SetTo([]string{
	"alice@example.com",
	"Bob <bob@example.com>",
})
b.SetSubject("See you tomorrow")

// Add custom headers
b.Headers.Set("Reply-To", "hello@example.com")
b.Headers.Set("Return-Path", "bounces@example.com")

b.SetPlainCharset("utf-8")
b.EncodeBase64Plain([]byte("See you tomorrow"))
b.SetHTMLCharset("utf-8")
b.EncodeQuotedHTML([]byte("<p>See you tomorrow</p>"))

w := &bytes.Buffer{}
err := b.Write(w)
if err != nil {
	// handle error
}
msg := w.String()
fmt.Println(msg)

The result is:

From: hello@example.com
Message-Id: myid
Reply-To: hello@example.com
Return-Path: bounces@example.com
Subject: See you tomorrow
To: alice@example.com, Bob <bob@example.com>
Content-Type: multipart/alternative; boundary="110000000000863a1705ddeb4f86"
Date: Mon, 02 May 2022 16:38:28 +0200
Mime-Version: 1.0

--110000000000863a1705ddeb4f86
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset=utf-8

U2VlIHlvdSB0b21vcnJvdw==
--110000000000863a1705ddeb4f86
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=utf-8

<p>See you tomorrow</p>
--110000000000863a1705ddeb4f86--

Godoc

Available at https://godoc.org/github.com/szxp/email

GoDoc

License

Copyright 2022 Szakszon Péter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Examples

Constants

View Source
const DefaultBoundary = "110000000000863a1705ddeb4f86"

Variables

This section is empty.

Functions

This section is empty.

Types

type EmailBuilder

type EmailBuilder struct {
	// Headers stores the custom key-value pairs of a MIME message.
	Headers http.Header

	// Boundary is the custom boundary.
	// Used only if the Headers does not contain a Content-Type header.
	// If the Headers contains a Content-Type header, the boundary
	// will be parsed from the header value.
	Boundary string

	// PlainHeaders stores the custom key-value pairs for the plain body part.
	PlainHeaders http.Header

	// Plain is the encoded plain text body part in wire format without the trailing \r\n.
	Plain bytes.Buffer

	// HTMLHeaders stores the custom key-value pairs for the HTML body part.
	HTMLHeaders http.Header

	// HTML is the encoded HTML text body part in wire format without the trailing \r\n.
	HTML bytes.Buffer
}

EmailBuilder helps build a multipart email message in MIME (Multipurpose Internet Mail Extensions) encoding.

Example (HtmlOnly)
package main

import (
	"github.com/szxp/email"

	"bytes"
	"fmt"
)

func main() {
	b := email.NewEmailBuilder()
	b.SetFrom("hello@example.com")
	b.SetTo([]string{
		"alice@example.com",
		"Bob <bob@example.com>",
	})
	b.SetSubject("See you tomorrow")

	// Add custom headers
	b.Headers.Set("Reply-To", "hello@example.com")
	b.Headers.Set("Return-Path", "bounces@example.com")
	b.Headers.Set("Message-ID", "myid")

	b.SetHTMLCharset("utf-8")
	b.EncodeQuotedHTML([]byte("<p>See you tomorrow</p>"))

	w := &bytes.Buffer{}
	err := b.Write(w)
	if err != nil {
		// handle error
	}
	msg := w.String()
	fmt.Println(msg)

	// This is the result:

	// From: hello@example.com
	// Message-Id: myid
	// Reply-To: hello@example.com
	// Return-Path: bounces@example.com
	// Subject: See you tomorrow
	// To: alice@example.com, Bob <bob@example.com>
	// Date: Mon, 02 May 2022 19:51:17 +0200
	// Mime-Version: 1.0
	// Content-Transfer-Encoding: quoted-printable
	// Content-Type: text/html; charset=utf-8
	//
	// <p>See you tomorrow</p>
}
Output:

Example (TextAndHTML)
package main

import (
	"github.com/szxp/email"

	"bytes"
	"fmt"
)

func main() {
	b := email.NewEmailBuilder()
	b.SetFrom("hello@example.com")
	b.SetTo([]string{
		"alice@example.com",
		"Bob <bob@example.com>",
	})
	b.SetSubject("See you tomorrow")

	// Add custom headers
	b.Headers.Set("Reply-To", "hello@example.com")
	b.Headers.Set("Return-Path", "bounces@example.com")
	b.Headers.Set("Message-ID", "myid")

	b.SetPlainCharset("utf-8")
	b.EncodeBase64Plain([]byte("See you tomorrow"))
	b.SetHTMLCharset("utf-8")
	b.EncodeQuotedHTML([]byte("<p>See you tomorrow</p>"))

	w := &bytes.Buffer{}
	err := b.Write(w)
	if err != nil {
		// handle error
	}
	msg := w.String()
	fmt.Println(msg)

	// This is the result:

	// From: hello@example.com
	// Message-Id: myid
	// Reply-To: hello@example.com
	// Return-Path: bounces@example.com
	// Subject: See you tomorrow
	// To: alice@example.com, Bob <bob@example.com>
	// Content-Type: multipart/alternative; boundary="110000000000863a1705ddeb4f86"
	// Date: Mon, 02 May 2022 16:38:28 +0200
	// Mime-Version: 1.0
	//
	// --110000000000863a1705ddeb4f86
	// Content-Transfer-Encoding: base64
	// Content-Type: text/plain; charset=utf-8
	//
	// U2VlIHlvdSB0b21vcnJvdw==
	// --110000000000863a1705ddeb4f86
	// Content-Transfer-Encoding: quoted-printable
	// Content-Type: text/html; charset=utf-8
	//
	// <p>See you tomorrow</p>
	// --110000000000863a1705ddeb4f86--
}
Output:

Example (TextOnly)
package main

import (
	"github.com/szxp/email"

	"bytes"
	"fmt"
)

func main() {
	b := email.NewEmailBuilder()
	b.SetFrom("hello@example.com")
	b.SetTo([]string{
		"alice@example.com",
		"Bob <bob@example.com>",
	})
	b.SetSubject("See you tomorrow")

	// Add custom headers
	b.Headers.Set("Reply-To", "hello@example.com")
	b.Headers.Set("Return-Path", "bounces@example.com")
	b.Headers.Set("Message-ID", "myid")

	b.SetPlainCharset("utf-8")
	b.EncodeBase64Plain([]byte("See you tomorrow"))

	w := &bytes.Buffer{}
	err := b.Write(w)
	if err != nil {
		// handle error
	}
	msg := w.String()
	fmt.Println(msg)

	// This is the result:

	// From: hello@example.com
	// Message-Id: myid
	// Reply-To: hello@example.com
	// Return-Path: bounces@example.com
	// Subject: See you tomorrow
	// To: alice@example.com, Bob <bob@example.com>
	// Date: Mon, 02 May 2022 19:51:17 +0200
	// Mime-Version: 1.0
	// Content-Transfer-Encoding: base64
	// Content-Type: text/plain; charset=utf-8
	//
	// U2VlIHlvdSB0b21vcnJvdw==
}
Output:

func NewEmailBuilder

func NewEmailBuilder() *EmailBuilder

func (*EmailBuilder) BoundaryString

func (b *EmailBuilder) BoundaryString() (string, error)

BoundaryString returns the boundary string. If a custom Content-Type header is specified in the Headers the boundary will be parsed from that header value. If a custom Boundary field is specified it will return that value. Otherwise it will return the DefaultBoundary.

func (*EmailBuilder) EncodeBase64HTML

func (b *EmailBuilder) EncodeBase64HTML(s []byte)

EncodeBase64HTML encodes s using base64 encoding and writes it to HTML buffer. HTML buffer will be reset to be empty before encoding, but the underlying storage will be retained.

func (*EmailBuilder) EncodeBase64Plain

func (b *EmailBuilder) EncodeBase64Plain(s []byte)

EncodeBase64Plain encodes s using base64 encoding and writes it to Plain buffer. Plain buffer will be reset to be empty before encoding, but the underlying storage will be retained.

func (*EmailBuilder) EncodeQuotedHTML

func (b *EmailBuilder) EncodeQuotedHTML(s []byte) error

EncodeQuotedHTML encodes s using quoted-printable encoding and writes it to HTML buffer. It limits line length to 76 characters. HTML buffer will be reset to be empty before encoding, but the underlying storage will be retained.

func (*EmailBuilder) EncodeQuotedPlain

func (b *EmailBuilder) EncodeQuotedPlain(s []byte) error

EncodeQuotedPlain encodes s using quoted-printable encoding and writes it to Plain buffer. It limits line length to 76 characters. Plain buffer will be reset to be empty before encoding, but the underlying storage will be retained.

func (*EmailBuilder) SetFrom

func (b *EmailBuilder) SetFrom(from string)

SetFrom creates the From header.

func (*EmailBuilder) SetHTMLCharset

func (b *EmailBuilder) SetHTMLCharset(s string)

SetHTMLCharset creates the HTML text Content-Type header with the specified s charset.

func (*EmailBuilder) SetPlainCharset

func (b *EmailBuilder) SetPlainCharset(s string)

SetPlainCharset creates the plain text Content-Type header with the specified s charset.

func (*EmailBuilder) SetSubject

func (b *EmailBuilder) SetSubject(s string)

SetSubject creates the Subject header with the specified s value.

func (*EmailBuilder) SetTo

func (b *EmailBuilder) SetTo(to []string)

SetTo creates the To header.

func (*EmailBuilder) Write

func (b *EmailBuilder) Write(w io.Writer) error

Write writes a MIME email in wire format.

Jump to

Keyboard shortcuts

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