mai
github.com/mohamedattahri/mail
go get github.com/mohamedattahri/mail
mai

github.com/mohamedattahri/mail

Package mail implements composing and parsing of mail messages

by Mohamed Attahri

v0.0.0-20150907213728-52bc9c59063f (see all)License:BSD-2-Clause
go get github.com/mohamedattahri/mail
Readme

Package mail

Build Status GoDoc

Package mail implements composing and parsing of mail messages.

Creating a message with multiple parts and attachments ends up being a surprisingly painful task in Go. The net/mail package in the standard library only offers tools to parse mail messages and addresses.

This package can replace the net/mail in your code without breaking it.

Feedback and contributions are more than welcome.

Features

  • multipart support
  • attachments (base64)
  • quoted-printable encoding of body text
  • quoted-printable decoding of headers
  • getters and setters common headers

Known issues

Installation

Alex Cesaro's quotedprintable package is the only external dependency. It's likely to be included in Go 1.5 in a new mime/quotedprintable package.

go get godoc.org/gopkg.in/alexcesaro/quotedprintable.v1
go get github.com/mohamedattahri/mail

Examples

Plain text message

msg := NewMessage()
msg.SetFrom(sender)
msg.To().Add(recipient)
msg.SetSubject("Plain text message")
msg.SetContentType("text/plain")
fmt.Fprintf(msg.Body, "Hello, World!")
fmt.Println(msg)

HTML and alternative text

msg := NewMessage()
msg.SetFrom(sender)
msg.To().Add(recipient)
msg.SetSubject("HTML and alternative text")
alternative := NewMultipart("multipart/alternative", msg)
alternative.AddText("text/plain", text)
alternative.AddText("text/html", html)
alternative.Close()
fmt.Println(msg)

Simple message with an attachment

msg := NewMessage()
msg.SetFrom(sender)
msg.To().Add(recipient)
msg.SetSubject("Simple message with an attachment")
mixed := NewMultipart("multipart/mixed", msg)
mixed.AddText("text/plain", text)
mixed.AddAttachment(Attachment, "Gopher.png", "", attachment)
mixed.Close()
fmt.Println(msg)

HTML message, alternative text and an attachment

msg := NewMessage()
msg.SetFrom(sender)
msg.To().Add(recipient)
msg.SetSubject("HTML message, alternative text and an attachment")

mixed := NewMultipart("multipart/mixed", msg)
alternative, _ := mixed.AddPart("multipart/alternative", nil)
alternative.AddText("text/plain", text)
alternative.AddText("text/html", html)
alternative.Close()
mixed.AddAttachment(Inline, "Photo", "image/jpeg", attachment)
mixed.Close()

fmt.Println(msg)

Attached image and cid URI Scheme

This example shows how to use the cid URI Scheme to use an attachment as a data source for an HTML img tag.

msg := NewMessage()
msg.SetFrom(&Address{"Al Bumin", "a.bumin@example.name"})
msg.To().Add(&Address{"Polly Ester", "p.ester@example.com"})
msg.SetSubject("Message with HTML, alternative text, and an attachment")
mixed := NewMultipart("multipart/mixed", msg)
// filename is the name that will be suggested to a user who would like to
// download the attachment, but also the ID with which you can refer to the
// attachment in a cid URI scheme.
filename := "gopher.jpg"
// The src of the image in this HTML is set to use the attachment with the
// Content-ID filename.
html := fmt.Sprintf("<html><body><img src=\"cid:%s\"/></body></html>", filename)
mixed.AddText("text/html", bytes.NewReader([]byte(html)))
// Load the photo and add the attachment with filename.
attachment, _ := ioutil.ReadFile("path/of/image.jpg")
mixed.AddAttachment(Attachment, filename, "image/jpeg", bytes.NewReader(attachment))
// Closing mixed, the parent part.
mixed.Close()

fmt.Println(msg)

GitHub Stars

12

LAST COMMIT

7yrs ago

MAINTAINERS

0

CONTRIBUTORS

2

OPEN ISSUES

4

OPEN PRs

1
VersionTagPublished
v0.0.0-20150907213728-52bc9c59063f
3yrs ago
No alternatives found
No tutorials found
Add a tutorial