📧 Easy to use, yet comprehensive library for sending mails with Go https://go-mail.dev
Find a file
2022-10-11 19:27:54 +02:00
.github Update codecov.yml 2022-09-08 15:33:39 +02:00
.idea #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
.reuse #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
assets Switch logo to CC-BY-ND-4.0 license 2022-07-11 13:23:50 +02:00
auth #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
LICENSES Switch logo to CC-BY-ND-4.0 license 2022-07-11 13:23:50 +02:00
.cirrus.yml #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
.gitignore Update .gitignore 2022-08-27 11:07:17 +02:00
auth.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
b64linebreaker.go #42: Return error in b64linebreaker.go when no out is set 2022-09-08 15:43:14 +02:00
b64linebreaker_test.go #42: Better test coverage for b64linebreaker.go. Also we return errors now 2022-09-08 11:47:11 +02:00
client.go Closes #54 2022-09-26 10:40:57 +02:00
client_test.go Closes #54 2022-09-26 10:40:57 +02:00
CODE_OF_CONDUCT.md #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
codecov.yml #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
CONTRIBUTING.md #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
doc.go Bump version to 0.2.9 2022-10-08 13:41:46 +02:00
doc_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
encoding.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
encoding_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
file.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
file_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
go.mod #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
go.sum Forgot to update go.mod/.sum 2022-03-13 17:25:57 +01:00
header.go Added support for requesting MDNs as described in RFC 8098 2022-09-09 11:35:45 +02:00
header_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
LICENSE Initial commit 2022-03-05 12:03:35 +01:00
msg.go #60 Working on Get/Set methods to access the Msg part slice 2022-10-11 17:05:44 +02:00
msg_nowin_test.go #42: Add test for Msg.WriteToSendmail 2022-09-08 15:08:17 +02:00
msg_test.go Added test for Msg.GetGenHeader 2022-10-02 12:30:08 +02:00
msg_totmpfile.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
msg_totmpfile_116.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
msgwriter.go Fixes a bug with attachments. Hopefully addresses #37 as well 2022-08-14 12:06:26 +02:00
msgwriter_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
part.go Closes #60. Added final test coverage and an additional SetContent method 2022-10-11 19:27:54 +02:00
part_test.go Closes #60. Added final test coverage and an additional SetContent method 2022-10-11 19:27:54 +02:00
README.md Updated README.md to introduce middlewares 2022-10-02 13:03:51 +02:00
SECURITY.md #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
sonar-project.properties #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
tls.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00
tls_test.go #24: Add SPDX license IDs for REUSE compliance 2022-06-17 15:05:54 +02:00

go-mail - Simple and easy way to send mails in Go

GoDoc codecov Go Report Card Build Status Mentioned in Awesome Go #go-mail on Discord REUSE status buy ma a coffee

go-mail logo

The main idea of this library was to provide a simple interface to sending mails for my JS-Mailer project. It quickly evolved into a full-fledged mail library.

go-mail follows idiomatic Go style and best practice. It's only dependency is the Go Standard Library. It combines a lot of functionality from the standard library to give easy and convenient access to mail and SMTP related tasks.

Parts of this library (especially some parts of msgwriter.go) have been forked/ported from the go-mail/mail respectively go-gomail/gomail which both seems to not be maintained anymore.

Features

Some of the features of this library:

  • Only Standard Library dependant
  • Modern, idiomatic Go
  • Sane and secure defaults
  • Explicit SSL/TLS support
  • Implicit StartTLS support with different policies
  • Makes use of contexts for a better control flow and timeout/cancelation handling
  • SMTP Auth support (LOGIN, PLAIN, CRAM-MD)
  • RFC5322 compliant mail address validation
  • Support for common mail header field generation (Message-ID, Date, Bulk-Precedence, Priority, etc.)
  • Reusing the same SMTP connection to send multiple mails
  • Support for attachments and inline embeds (from file system, io.Reader or embed.FS)
  • Support for different encodings
  • Middleware support for 3rd-party libraries to alter mail messages
  • Support sending mails via a local sendmail command
  • Support for requestng MDNs (RFC 8098) and DSNs (RFC 1891)
  • Message object satisfies io.WriteTo and io.Reader interfaces
  • Support for Go's html/template and text/template (as message body, alternative part or attachment/emebed)
  • Output to file support which allows storing mail messages as e. g. .eml files to disk to open them in a MUA

go-mail works like a programatic email client and provides lots of methods and functionalities you would consider standard in a MUA.

Support

We have a support and general discussion channel on the Gophers Discord server. Find us at: #go-mail

Middleware

The goal of go-mail is to keep it free from 3rd party dependencies and only focus on things a mail library should fulfill. Yet, since version v0.2.8 we've added support for middleware on the Msg object, allowing 3rd parties to alter a given mail message to their needs without relying on go-mail to support their specific need.

To get our users started with message middleware, we've created a collection of useful middlewares. It can be found in a seperate repository: go-mail-middlware.

Examples

The package is shipped with GoDoc example code for difference scenarios. Check them out on its GoDoc page

For ease of use, here is a full usage example:

package main

import (
	"fmt"
	"github.com/wneessen/go-mail"
	"os"
)

func main() {
	// Create a new mail message
	m := mail.NewMsg()

	// To set address header fields like "From", "To", "Cc" or "Bcc" you have different methods
	// at your hands. Some perform input validation, some ignore invalid addresses. Some perform
	// the formating for you.
	// 
	if err := m.FromFormat("Toni Tester", "sender@example.com"); err != nil {
		fmt.Printf("failed to set FROM address: %s\n", err)
		os.Exit(1)
	}
	if err := m.To(`"Max Mastermind <rcpt@example.com>"`); err != nil {
		fmt.Printf("failed to set TO address: %s\n", err)
		os.Exit(1)
	}
	m.CcIgnoreInvalid("cc@example.com", "invalidaddress+example.com")

	// Set a subject line
	m.Subject("This is a great email")

	// And some other common headers...
	//
	// Sets a valid "Date" header field with the current time
	m.SetDate()
	//
	// Generates a valid and unique "Message-ID"
	m.SetMessageID()
	//
	// Sets the "Precedence"-Header to "bulk" to indicate a "bulk mail"
	m.SetBulk()
	//
	// Set a "high" importance to the mail (this sets several Header fields to 
	// satisfy the different common mail clients like Mail.app and Outlook)
	m.SetImportance(mail.ImportanceHigh)

	// Add your mail message to body
	m.SetBodyString(mail.TypeTextPlain, "This is a great message body text.")

	// Attach a file from your local FS
	// We override the attachment name using the WithFileName() Option
	m.AttachFile("/home/ttester/test.txt", mail.WithFileName("attachment.txt"))

	// Next let's create a Client
	// We have lots of With* options at our disposal to stear the Client. It will set sane
	// options by default, though
	//
	// Let's assume we need to perform SMTP AUTH with the sending server, though. Since we
	// use SMTP PLAIN AUTH, let's also make sure to enforce strong TLS
	host := "relay.example.com"
	c, err := mail.NewClient(host,
		mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername("ttester"),
		mail.WithPassword("V3rySecUr3!Pw."), mail.WithTLSPolicy(mail.TLSMandatory))
	if err != nil {
		fmt.Printf("failed to create new mail client: %s\n", err)
		os.Exit(1)
	}

	// Now that we have our client, we can connect to the server and send our mail message
	// via the convenient DialAndSend() method. You have the option to Dial() and Send()
	// seperately as well
	if err := c.DialAndSend(m); err != nil {
		fmt.Printf("failed to send mail: %s\n", err)
		os.Exit(1)
	}

	fmt.Println("Mail successfully sent.")
}

Contributors

Thanks to the following people for contributing to the go-mail project: