GithubHelp home page GithubHelp logo

nixigaj / mimemailer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adueck/mimemailer

0.0 0.0 0.0 25 KB

Easily create and send MIME emails in Go ๐Ÿ“ฌ (with some of my fixes)

License: MIT License

Go 100.00%

mimemailer's Introduction

mimemailer

Build Status Go Report Card GoDoc

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)
	}
}

Documentation

https://godoc.org/github.com/nixigaj/mimemailer

mimemailer's People

Contributors

adueck avatar nixigaj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.