Documentation ¶
Index ¶
- Constants
- type EmailBuilder
- func (b *EmailBuilder) BoundaryString() (string, error)
- func (b *EmailBuilder) EncodeBase64HTML(s []byte)
- func (b *EmailBuilder) EncodeBase64Plain(s []byte)
- func (b *EmailBuilder) EncodeQuotedHTML(s []byte) error
- func (b *EmailBuilder) EncodeQuotedPlain(s []byte) error
- func (b *EmailBuilder) SetFrom(from string)
- func (b *EmailBuilder) SetHTMLCharset(s string)
- func (b *EmailBuilder) SetPlainCharset(s string)
- func (b *EmailBuilder) SetSubject(s string)
- func (b *EmailBuilder) SetTo(to []string)
- func (b *EmailBuilder) Write(w io.Writer) error
Examples ¶
Constants ¶
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.