Go Mail - A cross platform mail driver for GoLang.

Gopher

Go Mail

A cross platform mail driver for GoLang.

Travis Software License Coverage Status Go Report Card GoDoc

Introduction

Go Mail aims to unify multiple popular mail API's (SparkPost, MailGun & SendGrid) into a singular easy to use interface. Email sending is seriously simple and great for allowing the developer to choose what platform they use.

cfg := mail.Config{
    URL:         "https://api.eu.sparkpost.com",
    APIKey:      "my-key",
    FromAddress: "[email protected]",
    FromName:    "Gopher",
}

driver, err := mail.NewClient(mail.SparkPost, cfg)
if err != nil {
    fmt.Println(err)
    return
}

tx := &mail.Transmission{
    Recipients:  []string{"[email protected]"},
    Subject:     "My email",
    HTML:        "

Hello from go mail!

"
, } result, err := driver.Send(tx) if err != nil { fmt.Println(err) return } fmt.Println(result)

Installation

go get -u github.com/ainsleyclark/go-mail

Supported API's

Currently, Sparkpost, MailGun and SendGrid is supported, if you want to see more, just submit a feature request or create a new Driver and submit a pull request.

API Dependency Examples
SparkPost github.com/SparkPost/gosparkpost Here
MailGun github.com/mailgun/mailgun-go/v4 Here
SendGrid github.com/sendgrid/sendgrid-go Here

Docs

Documentation can be found at the Go Docs, but we have included a kick start guide below to get you started.

Creating a new client:

The driver type is the first argument to be passed to the NewClient function, being one of the following:

  • mail.SparkPost
  • mail.MailGun
  • mail.SendGrid

A new configuration type is needed to create a new mailer as the second argument, each platform requires its own data, for example, MailGun requires a domain, but SparkPost doesn't. This is based of the requirements for the API. For more details see the examples above.

cfg := mail.Config{
    URL:         "https://api.eu.sparkpost.com",
    APIKey:      "my-key",
    FromAddress: "[email protected]",
    FromName:    "Gopher",
}

driver, err := mail.NewClient(mail.SparkPost, cfg)
if err != nil {
    fmt.Println(err)
    return
}

Sending Data

A transmission is required to transmit to a mailer as shown below. Once send is called, a mail.Result and error will be returned indicating if the transmission was successful.

tx := &mail.Transmission{
    Recipients: []string{"[email protected]"},
    Subject:    "My email",
    HTML:       "

Hello from go mail!

"
, PlainText: "plain text", } result, err := driver.Send(tx) if err != nil { fmt.Println(err) return } fmt.Println(result)

Adding attachments:

Adding attachments to the transmission is as simple as passing a byte slice and filename, Go Mail takes care of the rest for you.

image, err := ioutil.ReadFile("gopher.jpg")
if err != nil {
    fmt.Println(err)
    return
}

tx := &mail.Transmission{
    Recipients: []string{"[email protected]"},
    Subject:    "My email",
    HTML:       "

Hello from go mail!

"
, PlainText: "plain text", Attachments: mail.Attachments{ mail.Attachment{ Filename: "gopher.jpg", Bytes: image, }, }, }

Todo

  • Add CC & BCC

Contributing

We welcome contributors, but please read the contributing document before making a pull request.

Licence

Code Copyright 2021 go mail. Code released under the MIT Licence.

Owner
Ainsley Clark
Front-End Developer HTML5 | CSS3 | JavaScript | Java | SQL | jQuery | SASS
Ainsley Clark
Comments
  • Changes in v1.1.x break any project that imports v1.0.x

    Changes in v1.1.x break any project that imports v1.0.x

    Hi:

    I don't suppose that you could move v1.1.x to a v2 branch; any project that uses this package and wrote code as required in v1.0.x now breaks when go get -u is executed, since you've moved to the (much better) approach that you have in version 1.1.x.

    Obviously, this is not a huge deal, but it would make life easier for those of us who are using your package, and it also is a more correct approach of versioning, since v1.1.x introduces breaking changes.

    This is just a suggestion, of course.

    Thanks again for this useful package.

  • Mailgun

    Mailgun

    Great package!

    The only problem I am running into, and I'm sure it's me, is that when I try to send through mailgun, my HTML message gets rendered in the mail client with the wrong content type (text/plain).

    Below is the code used to send, and below is the sample email (raw) received.

    That is, the message displayed in the browser appears to have the correct payload, but the user gets to read raw html rather than a nicely formatted message.

    Any idea what I'm doing wrong?

    cfg := mail.Config{
        URL:         "https://api.mailgun.net/",
        APIKey:      "someapikey",
        FromAddress: mailMessage.FromAddress,
        FromName:    mailMessage.FromName,
        Domain:      "mg.mydomain.com",
    }
    
    driver, err := mail.NewClient(mail.MailGun, cfg)
        if err != nil {
        fmt.Println(err)
    }
    
    var toSlice []string
    toSlice = append(toSlice, mailMessage.ToAddress)
    
    formattedMessage = "<h1>Hello, world</h1>"
    plainText := "Hello, world"
    
    tx := &mail.Transmission{
        Recipients: toSlice,
        Subject:    mailMessage.Subject,
        HTML:       formattedMessage,
        PlainText: plainText,
    }
    
    rs, err := driver.Send(tx)
        if err != nil {
        fmt.Println(err)
    }
    
    fmt.Println(rs)
    

    And here is the resulting message:

    Delivered-To: [email protected] Received: by 2002:a05:6602:2816:0:0:0:0 with SMTP id d22csp709301ioe; Sun, 11 Apr 2021 06:46:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYhhmrpu7UvmoOFLE1Vl0fb2bUd4fYYpfMjrIacwmORyXF9WkHqNdSgejUFlyJNHcr/vT2 X-Received: by 2002:a05:6820:451:: with SMTP id p17mr19136528oou.93.1618148815224; Sun, 11 Apr 2021 06:46:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618148815; cv=none; d=google.com; s=arc-20160816; b=g2Sc5tbiMNl9Lk6GDXfByhcD4pjTiA1zIArM+FhOttmsqvMirqnoE3ZO1K/Q5nHWb4 q5Dx/WNm3a8OiR8d/bg9/XKrK3oFGc5lT6TtEDWi1/RqfYiTdywD5pK2Ug+EoGBQfQBb S1kAqKeka9iYqdQ665link5rBySB602ZuEekGvFDqvzdwPchlOLIkye5Ggr/RIbKcuYf 7JWehr8bSuX1RsTFTCfv6ojJozPDmEdtF6tbAxOca2bA0W6oCqesFC0e0BfdBHu9vhYK IAapYpx7YVLaFwPAgLP6O+POU374Av+19Is9W+dgP0nhA5JvZFCiwnSmhEl9L/l9YSd4 INlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:subject:from:to:message-id:sender:date :content-transfer-encoding:dkim-signature:dkim-signature; bh=CBs4VIi+Hgt2a2XJuW2wr7q/kVgtt7M5U3mvhqs5674=; b=aV7Kc/3pE6pwJZPHAZwqkSVeUCmsDGs777s/ko+Zo53JiMy8nk8IDU92Zef8lDXHS8 9sfejXMiG3Sw4MgiekT8vaul8QgiXgPPnD6m3KhCif+h1AJ7k/y/scvxFeXi8l5bhVMo 8oo1ioE5Xy6UOfK7JQxECnhgQz25VaytycvtEhxB6Rtf6wkRVrrOONB9B4VS8JwU3EzG zQ3y/Qzvisivlb50s1FBDnScBKE6eGQx15rsPxyb/hVWPDcR8VkNsmRVfNkX4n22Y3oZ E8hVsySOo8/ht9/ImMMfPqVC9dwkHg3vZqq15t8X/+WNrWD/gq/DpElPX3urORIFmMZU 7WcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass [email protected] header.s=pic header.b=VlTzhdjS; dkim=pass [email protected] header.s=mg header.b=uysNa81o; spf=pass (google.com: domain of [email protected] designates 209.61.151.241 as permitted sender) smtp.mailfrom="[email protected]" Return-Path: [email protected] Received: from rs241.mailgun.us (rs241.mailgun.us. [209.61.151.241]) by mx.google.com with UTF8SMTPS id c189si9473199oif.183.2021.04.11.06.46.51 for [email protected] (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 11 Apr 2021 06:46:55 -0700 (PDT) Received-SPF: pass (google.com: domain of [email protected] designates 209.61.151.241 as permitted sender) client-ip=209.61.151.241; Authentication-Results: mx.google.com; dkim=pass [email protected] header.s=pic header.b=VlTzhdjS; dkim=pass [email protected] header.s=mg header.b=uysNa81o; spf=pass (google.com: domain of [email protected] designates 209.61.151.241 as permitted sender) smtp.mailfrom="[email protected]" DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.somedomain.com; q=dns/txt; s=pic; t=1618148815; h=Mime-Version: Content-Type: Subject: From: To: Message-Id: Sender: Date: Content-Transfer-Encoding: X-Feedback-Id; bh=CBs4VIi+Hgt2a2XJuW2wr7q/kVgtt7M5U3mvhqs5674=; b=VlTzhdjShoQVADb+knojm1mrleJz3KUO4l4Hc20/8rjfG52nYM4JD9Y2Jcc/hCqGbFUunmgd WfhyQtPSwqkw8JWBH2vqRZHyjVWvI58C4C8Wpwuhp84+NTlgO7w9ZtmnoBo8uWyr6kqfzTsS tkxC773nMW0Jb6vv2scwPKgiJm8= DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mailgun.org; q=dns/txt; s=mg; t=1618148815; h=Mime-Version: Content-Type: Subject: From: To: Message-Id: Sender: Date: Content-Transfer-Encoding: X-Feedback-Id; bh=CBs4VIi+Hgt2a2XJuW2wr7q/kVgtt7M5U3mvhqs5674=; b=uysNa81oWS2WPuiiJHGtDTSclAKGgshDcOlfgr3WM5F1LKdaiKE+d8M7bo/MKVehvZR2y1/3 D2w2D604OHt4bXiBe2OIhIxecAeTiIzdfMtLSiDArreCLSCTDlTg5PPZJoK6Ew6quk44283s nEn1iJ6Fs9d/QLnZmVBEM6EJG0c= X-Feedback-Id: 589e0b0bcd1682225498c986:mailgun X-Mailgun-Sending-Ip: 209.61.151.241 X-Mailgun-Sid: WyI2MDliMSIsICJ0cmV2b3Iuc2F3bGVyQGdtYWlsLmNvbSIsICI1ZDdiZTAiXQ== Content-Transfer-Encoding: 7bit Received: by luna.mailgun.net with HTTP; Sun, 11 Apr 2021 13:46:31 +0000 Date: Sun, 11 Apr 2021 13:46:31 +0000 Sender: [email protected] Message-Id: [email protected] To: [email protected] From: [email protected] Subject: Password Reset Request Content-Type: text/plain; charset="ascii" Mime-Version: 1.0

    Hello, world

  • Postal Support

    Postal Support

  • Support SMTP

    Support SMTP

    It would be great if plain old SMTP was supported.

    For development, and for apps which only sends a few mails, I often use Gmail's SMTP or other SMTP servers.

  • Deps

    Deps

    Description

    • New Postmark driver.
    • Removed all external dependencies from Mailgun, Sparkpost & Sendgrid.
    • Refactored codebase.
    • Moved drivers to seperate package.

    Fixes # (issue)

    Type of change

    Please delete options that are not relevant.

    • Bug fix: Fixed SMTP Plain Text issue.
    • New feature
    • Breaking change: Changed driver string to constructors.

    How Has This Been Tested?

    • New driver testing.
  • Postmark

    Postmark

    Description

    • Added Postal Sypport
    • Refactored Codebase
    • Seperated Drivers
    • Added custom client for API calls.

    Type of change

    • New feature (non-breaking change which adds functionality)

    How Has This Been Tested?

    • Unit tests - postmark_test.go
  • Postal

    Postal

    Description

    • Added Postal Sypport
    • Added CC & BCC to all providers (untested).
    • Cleaned up examples.

    Type of change

    • New feature (non-breaking change which adds functionality)

    How Has This Been Tested?

    • Unit tests - postal_test.go
  • Remove External Deps

    Remove External Deps

    Remove external go.mod dependencies for the following. Ensure only native http.Client is used for the sending of mail.

    • github.com/SparkPost/gosparkpost
    • github.com/sendgrid/sendgrid-go
    • github.com/sendgrid/rest
    • github.com/mailgun/mailgun-go/v4
  • MailChimp Support

    MailChimp Support

    MailChimp provider for Go Mail, see below for implentation.

    • https://mailchimp.com/developer/
    • https://mailchimp.com/developer/transactional/api/messages/send-new-message/
  • multipart email is not properly formatted when using smtp driver

    multipart email is not properly formatted when using smtp driver

    Describe the bug When send smtp email with both html and plain text, the Content-Type is not properly set.

    To Reproduce

    cfg := mail.Config{
    	URL:         "smtp.gmail.com",
    	FromAddress: "[email protected]",
    	FromName:    "Gopher",
    	Password:    "my-password",
    	Port:        587,
    }
    
    mailer, err := drivers.NewSMTP(cfg)
    if err != nil {
    	log.Fatalln(err)
    }
    
    tx := &mail.Transmission{
    	Recipients: []string{"[email protected]"},
    	Subject:    "Multipart Email Example",
    	HTML:       "<h1>This is the HTML Section!</h1>",
    	PlainText:  "Plain text email goes here!",
    }
    
    result, err := mailer.Send(tx)
    

    Expected behavior

    From: [email protected]
    To: [email protected]
    Subject: Multipart Email Example
    Content-Type: multipart/alternative; boundary="boundary-string"
    
    --your-boundary
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline
    
    Plain text email goes here!
    
    --boundary-string
    Content-Type: text/html; charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline
    
    <h1>This is the HTML Section!</h1>
    
    --boundary-string--
    

    There is a article for Multipart MIME Email Guide. And go-simple-email seems work well with it.

  • x509: certificate signed by unknown authority when using self-signed certificate

    x509: certificate signed by unknown authority when using self-signed certificate

    Is your feature request related to a problem? Please describe. I am using mailpit for mail testing, but i got x509: certificate signed by unknown authority error You may take a look at the thread

    Describe the solution you'd like Maybe add InsecureSkipVerify: true config when testing

Inline styling for html mail in golang

go-premailer Inline styling for HTML mail in golang Document install go get github.com/vanng822/go-premailer/premailer Example import ( "fmt" "gith

Nov 30, 2022
Golang package that generates clean, responsive HTML e-mails for sending transactional mail
Golang package that generates clean, responsive HTML e-mails for sending transactional mail

Hermes Hermes is the Go port of the great mailgen engine for Node.js. Check their work, it's awesome! It's a package that generates clean, responsive

Dec 28, 2022
πŸ“§ Example of sending mail via SendGrid in Golang.

?? go-sendgrid-example Example of sending mail via SendGrid in Golang. Get it started $ make setup # Edit environment variables $ vim ./env/local.env

Jan 11, 2022
:envelope: A streaming Go library for the Internet Message Format and mail messages

go-message A Go library for the Internet Message Format. It implements: RFC 5322: Internet Message Format RFC 2045, RFC 2046 and RFC 2047: Multipurpos

Dec 26, 2022
Go library for sending mail with the Mailgun API.

Mailgun with Go Go library for interacting with the Mailgun API. Usage package main import ( "context" "fmt" "log" "time" "githu

Dec 25, 2022
βœ‰οΈ Composable all-in-one mail server.

Maddy Mail Server Composable all-in-one mail server. Maddy Mail Server implements all functionality required to run a e-mail server. It can send messa

Dec 27, 2022
MIME mail encoding and decoding package for Go

enmime enmime is a MIME encoding and decoding library for Go, focused on generating and parsing MIME encoded emails. It is being developed in tandem w

Nov 30, 2022
an MDA that sends a webhook on recieval of mail

an MDA that sends a webhook on recieval of mail

Aug 13, 2022
Filtering spam in mail server, protecting both client privacy and server algorithm

HE Spamfilter SNUCSE 2021 "Intelligent Computing System Design Project" Hyesun Kwak Myeonghwan Ahn Dongwon Lee abstract NaΓ―ve Bayesian spam filtering

Mar 23, 2022
Mail_sender - This library is for sending emails from your mail

Mail Sender This library is for sending emails from your mail Installation mail_

Dec 30, 2021
Mcopa - A library allows for parsing an email message into a more convenient form than the net/mail provides

Mail content parsing This library allows for parsing an email message into a mor

Jan 1, 2022
Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade

smtptest Simple tool to test SMTP mail send with various settings including TLS1.1 downgrade All settings are configurable in the config.yaml file ser

Sep 19, 2022
Go-mail - Email service using Kafka consumer

?? The Project This project consists in a Kafka consumer that reads messages of

Feb 5, 2022
Send markdown files as MIME-encoded electronic mail.

Send markdown files as MIME-encoded electronic mail.

Aug 9, 2022
DKIM package for golang

go-dkim DKIM package for Golang Getting started Install go get github.com/toorop/go-dkim Warning: you need to use Go 1.4.2-master or 1.4.3 (when it

Dec 10, 2022
Golang package for send email. Support keep alive connection, TLS and SSL. Easy for bulk SMTP.

Go Simple Mail The best way to send emails in Go with SMTP Keep Alive and Timeout for Connect and Send. IMPORTANT Examples in this README are for v2.2

Jan 8, 2023
The Official Twilio SendGrid Led, Community Driven Golang API Library
The Official Twilio SendGrid Led, Community Driven Golang API Library

NEW: Subscribe to email notifications for releases and breaking changes. The default branch name for this repository has been changed to main as of 07

Dec 15, 2022
An email MIME artist for golang

Marcel is a tool to generate IETF compliant emails in raw MIME format. I mainly use this for generating emails with attachments and sending them via amazon SES. If that's what you're doing too, you may want notifications

Nov 7, 2022
EMail Searcher By Golang
EMail Searcher By Golang

GiveMeMail β–„β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–“ β–ˆβ–ˆβ–’ β–ˆβ–ˆβ–“ β–“β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–„ β–„β–ˆβ–ˆβ–ˆβ–“β–“β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–„ β–„β–ˆβ–ˆβ–ˆβ–“ β–ˆβ–ˆ β–ˆβ–ˆβ–“ β–ˆβ–ˆβ–“ β–ˆβ–ˆβ–’ β–€β–ˆβ–’ β–“β–ˆβ–ˆβ–’ β–“β–ˆβ–ˆβ–‘ β–ˆβ–’ β–“β–ˆ β–€ β–“β–ˆβ–ˆβ–’β–€β–ˆβ–€ β–ˆβ–ˆβ–’β–“β–ˆ

Dec 4, 2021