Easily create and send MIME emails in Go
Mimemailer provides a way to easily create send multi-part MIME email messages as specified by RFC 2045 and RFC 2046. It is inspired by nodemailer but it much more limited and minimal.
Mimemailer takes a HTML email message, parses into an html - text multi-part MIME message, and sends it over SMTP
This package takes care of:
- Connecting to a SMTP server through TLS
- Creating a text version of an HTML email
- Converting the HTML and Text versions to Quoted Printable format
- Put together whole email according to RFC 2045 and RFC 2046
Given an email like this:
Email{
ToAddress: "[email protected]",
ToName: "Test Recipient",
Subject: "Test Email",
Date: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
HTML: `<!doctype html>
<html xmlns=http:www.w3.org/1999/xhtml style=background:#f3f3f3>
<body>
<p>Hello ๐. This email wรญll be formatted as a MIME message as per RFC 2045 and RFC 2046 ๐ง</p>
</body>
</html>`,
}
It will generate and send a multi-part MIME message over SMTP like this:
Subject: Test Email
From: "Sender Name" <[email protected]>
To: "Test Recipient" <[email protected]>
Date: Tue, 10 Nov 2009 23:00:00 +0000
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=boundary42
--boundary42
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hello =F0=9F=8C=8D. This email w=C3=ADll be formatted as a MIME message as =
per RFC 2045 and RFC 2046 =F0=9F=93=A7
--boundary42
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<!doctype html>
<html xmlns=3Dhttp:www.w3.org/1999/xhtml style=3Dbackground:#f3f3f3>
<body>
<p>Hello =F0=9F=8C=8D. This email w=C3=ADll be formatted as a MIME message =
as per RFC 2045 and RFC 2046 =F0=9F=93=A7</p>
</body>
</html>
--boundary42--
The text version is generated automatically, and the message is converted into quoted printable format with CRLF line endings.
After connecting to a SMTP server, you can send multiple messages and re-use the same connection.
Example Usage:
package main
import (
"log"
"time"
"github.com/adueck/mimemailer"
)
func main() {
// Step 1 - Create a new mailer instance with config for SMTP
m, err := mimemailer.NewMailer(mimemailer.Config{
Host: "smtp.example.com",
Port: "576",
Username: "myusername",
Password: "mysecretpassword",
SenderName: "My Name",
SenderAddress: "[email protected]",
})
if err != nil {
log.Fatal(err)
}
// Step 2 - Connect to SMTP Server
err = m.Connect()
if err != nil {
log.Fatal(err)
}
// Step 3 - Send message(s)
err = m.SendEmail(mimemailer.Email{
ToAddress: "[email protected]",
ToName: "Bob Smith",
Subject: "Example Mail",
HTML: "<html><p>Hello Bob</p></html>",
Date: time.Now(),
})
if err != nil {
log.Print(err)
}
// ...
// You can send more messages on the persistent connection
// When you are done, disconnect from the SMTP server
// Step 4 - Disconnect from SMTP server
err = m.Disconnect()
if err != nil {
log.Fatal(err)
}
}