The Official Twilio SendGrid Led, Community Driven Golang API Library

Twilio SendGrid Logo

BuildStatus Email Notifications Badge GoDoc MIT licensed Twitter Follow GitHub contributors Open Source Helpers

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/27/2020.

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Go.

Version 3.X.X of this library provides full support for all Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

This library represents the beginning of a new path for Twilio SendGrid. We want this library to be community driven and Twilio SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.

Please browse the rest of this README for further detail.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • Go version 1.7
  • The Twilio SendGrid service, starting at the free level, to send up to 40,000 emails for the first 30 days, then send 100 emails/day free forever or check out our pricing.

Setup Environment Variables

Update the development environment with your SENDGRID_API_KEY, for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

Install Package

go get github.com/sendgrid/sendgrid-go

Dependencies

Setup Environment Variables

Initial Setup

cp .env_sample .env

Environment Variable

Update the development environment with your SENDGRID_API_KEY, for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):

With Mail Helper Class

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
)

func main() {
	from := mail.NewEmail("Example User", "[email protected]")
	subject := "Sending with Twilio SendGrid is Fun"
	to := mail.NewEmail("Example User", "[email protected]")
	plainTextContent := "and easy to do anywhere, even with Go"
	htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
	client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
	response, err := client.Send(message)
	if err != nil {
		log.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

The NewEmail constructor creates a personalization object for you. Here is an example of how to add to it.

Without Mail Helper Class

The following is the minimum needed code to send an email without the /mail/send Helper (here is a full example):

package main

import (
	"fmt"
	"github.com/sendgrid/sendgrid-go"
	"log"
	"os"
)

func main() {
	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
	request.Method = "POST"
	request.Body = []byte(` {
	"personalizations": [
		{
			"to": [
				{
					"email": "[email protected]"
				}
			],
			"subject": "Sending with Twilio SendGrid is Fun"
		}
	],
	"from": {
		"email": "[email protected]"
	},
	"content": [
		{
			"type": "text/plain",
			"value": "and easy to do anywhere, even with Go"
		}
	]
}`)
	response, err := sendgrid.API(request)
	if err != nil {
		log.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

General v3 Web API Usage

package main

import (
	"fmt"
	"github.com/sendgrid/sendgrid-go"
	"log"
	"os"
)

func main() {
	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/api_keys", "https://api.sendgrid.com")
	request.Method = "GET"

	response, err := sendgrid.API(request)
	if err != nil {
		log.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

Processing Inbound Email

Please see our helper for utilizing our Inbound Parse webhook.

Usage

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Announcements

Please see our announcement regarding breaking changes. Your support is appreciated!

All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email release notifications for releases and breaking changes.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-go is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-go are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

License

The MIT License (MIT)

Owner
Twilio SendGrid
Official open source libraries - send emails and integrate with our APIs fast.
Twilio SendGrid
Comments
  • impersonate subuser

    impersonate subuser

    Fixes #239

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • HTTP header "On-Behalf-Of" for API request
    • updated tests
    • updated docs
  • Send a Single Email to a Single Recipient

    Send a Single Email to a Single Recipient

    Fixes #87 Add new client helper that encapsulates sending an mail Add a send func for the client that takes an email and triggers the email request

    Sample usage

    package main
    
    import (
        "fmt"
        "github.com/sendgrid/sendgrid-go"
        "github.com/sendgrid/sendgrid-go/helpers/mail"
        "os"
    )
    
    func main() {
        client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
        from := mail.NewEmail("Example User", "[email protected]")
        subject := "Hello World from the SendGrid Go Library"
        to := mail.NewEmail("Example User", "[email protected]")
        plainTextContent := "some text here"
        htmlContent := "<strong>some HTML content</strong>"
        email := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
    
        response, err := client.Send(email)
        if err != nil {
            log.Println(err)
        } else {
            fmt.Println(response.StatusCode)
            fmt.Println(response.Body)
            fmt.Println(response.Headers)
        }
    }
    
  • feat: Auto-Generate Plain Text Content from HTML

    feat: Auto-Generate Plain Text Content from HTML

    Fixes #125

    Has the following features:

    • uses github.com/microcosm-cc/bluemonday to first sanitize input
    • does not require valid HTML (e.g. improper nesting of tags)
    • parses HTML entities e.g. &lt; or &#35;
    • respects formatting in <pre> and <textarea>
    • shows links (except #anchor or relative)
    • simple output of <hr> as text separator
    • has tests

    Many additional formatting enhancements can be added (e.g. head levels, numbered or bulleted lists).

    In ./plaintext/main, go build plaintext.go will make a command-line test utility: e.g curl -s 'https://sendgrid.com/docs' |. ./plaintext

  • Dynamic template support

    Dynamic template support

    Fixes #260

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Add dynamic template support for Go library
    • Add helper method for dynamic template data
    • Add dynamic template example in examples/helpers/mail/example.go (this had to be done in a separate function because dynamic template data and substitutions may not be used in the same API call
    • Add documentation and examples in USE_CASES.md
  • Allow Multiple API Keys To Be Set

    Allow Multiple API Keys To Be Set

    Fixes #245

    Checklist

    • [x] I have made GetRequests and NewSendMultiClient functions that allow multiple key (functionality).
    • [x] I have added tests (Test_test_send_multi_client and TestGetRequests) that prove my fix is effective or that my feature works

    Short description of what this PR does:

    • GetRequests is a function that allows multiple key to do request. This function was tested on TestGetRequests testcase
    • NewSendMultiClient is a function that allows multiple key to send message to clients. This function was tested on Test_test_send_multi_client testcase. Because this test function has multiple key, probably you need to set timeout time higher. e.g. go test -run Test_test_send_multi_client -timeout 10000s
  • Create helper methods for the /contactdb endpoints

    Create helper methods for the /contactdb endpoints

    Fixes #275

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Creates helper methods for the /contactdb endpoitns

    Testing my helper functions is confusing me, do I use the docker container to do it, or how. Can anyone specify this? Thank you :)

    Also, I am confused as to what documentation I should add, the issue specifies this: updated USE_CASES.md or /use-cases documentation, but does that mean that I just add use cases to the file?

    And lastly, do I add examples in the examples folder?

  • added custom timeout abilities

    added custom timeout abilities

    I felt the need for that ability while sending some "big" attachments in a slow connection...

    I'm a rookie in Go, so maybe I did something wrong... please let me know if that's the case.

    Cheers

  • docs: Correct *.md files using Grammarly

    docs: Correct *.md files using Grammarly

    Resolves #286

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
  • feat: Adding permission scopes and methods to manage permissions

    feat: Adding permission scopes and methods to manage permissions

    Fixes #154 .

    @thinkingserious @mbernier I am pretty sure I will get some review comments for this. I thought this PR would be a great starting point for discussion.

  • fix broken link

    fix broken link

    Fixes

    Fixes #291

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Fix broken link.

    If you have questions, please send an email to Sendgrid, or file a Github Issue in this repository.

  • Add DigitalOcean deployment tutorial

    Add DigitalOcean deployment tutorial

    Hi all. This is nearly identical to #145, except that I added the tutorial to the table of contents of the article, and I...well...didn't totally screw up the rebase this time (you can't screw it up if you don't do it). :)

    (additionally, it seems this changes indentation on lines 1355–1426, but on my local copy of the repo I don't see those changes. I'm fairly new to Git, so if I'm missing anything please let me know)

    #133

  • feat: Adding go modules for v3

    feat: Adding go modules for v3

    This PR adds go modules capabilities as per standard go applications. This also adheres to the go standard for versions greater than v1 by adding a v3 folder with a copy of all of the go files inside of it (see https://go.dev/blog/v2-go-modules).

    To avoid duplicative work, the tests to check for non-critical files (such as the license file) has been removed from v3 so that the readme and license files do not need to be updated in multiple places moving forward.

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the Contribution Guidelines and my PR follows them
    • [x] I have titled the PR appropriately
    • [x] I have updated my branch with the main branch
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added the necessary documentation about the functionality in the appropriate .md file
    • [x] I have added inline documentation to the code I modified

    If you have questions, please file a support ticket.

  • fix: Add missing err checks to inbound.Parse

    fix: Add missing err checks to inbound.Parse

    Fixes

    Added missing err checks that cause panic for some emails.

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [ ] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [ ] I have read the Contribution Guidelines and my PR follows them
    • [ ] I have titled the PR appropriately
    • [ ] I have updated my branch with the main branch
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added the necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added inline documentation to the code I modified
  • fix: inbound.Parse panics when Content-Type is not multipart

    fix: inbound.Parse panics when Content-Type is not multipart

    Resolves https://github.com/sendgrid/opensource/issues/28

    Fixes

    Fixes inbound.Parse that currently panics when the Content-Type of the email is not multipart/....

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
  • Support send_each_at mail header

    Support send_each_at mail header

    Fixes

    Support send_each_at mail header send_each_at is scheduling-parameters Reference https://docs.sendgrid.com/for-developers/sending-email/scheduling-parameters

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the Contribution Guidelines and my PR follows them
    • [x] I have titled the PR appropriately
    • [x] I have updated my branch with the main branch
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added the necessary documentation about the functionality in the appropriate .md file
    • [x] I have added inline documentation to the code I modified

    If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

  • feat: Add Go Modules

    feat: Add Go Modules

    Feature

    Add a go.mod file to manage dependencies of a given Go project. This should allow users to specify a version when installing the GoLib vs always pulling from master.

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the Contribution Guidelines and my PR follows them
    • [x] I have titled the PR appropriately
    • [x] I have updated my branch with the main branch
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added the necessary documentation about the functionality in the appropriate .md file
    • [x] I have added inline documentation to the code I modified

    If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

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
go-pst is a library for reading PST files (written in Go/Golang).
go-pst is a library for reading PST files (written in Go/Golang).

go-pst A library for reading PST files (written in Go/Golang). Introduction go-pst is a library for reading PST files (written in Go/Golang). The PFF

Dec 29, 2022
Golang library for sending email using gmail credentials

library for sending email using gmail credentials

Jan 22, 2022
Web and API based SMTP testing
Web and API based SMTP testing

MailHog Inspired by MailCatcher, easier to install. Download and run MailHog Configure your outgoing SMTP server View your outgoing email in a web UI

Jan 4, 2023
a simple api that sent spam via sms and email

a simple api that sent spam via sms and email routes: /sms /email example request with python

Oct 19, 2021
Robust and flexible email library for Go

email Robust and flexible email library for Go Email for humans The email package is designed to be simple to use, but flexible enough so as not to be

Dec 30, 2022
:white_check_mark: A Go library for email verification without sending any emails.

email-verifier ✉️ A Go library for email verification without sending any emails. Features Email Address Validation: validates if a string contains a

Dec 30, 2022
:inbox_tray: An IMAP library for clients and servers

go-imap An IMAP4rev1 library written in Go. It can be used to build a client and/or a server. Usage Client package main import ( "log" "github.com

Jan 6, 2023
: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
✉️ A Go library for email verification without sending any emails.

email-verifier ✉️ A Go library for email verification without sending any emails. Features Email Address Validation: validates if a string contains a

Jun 24, 2021
A simple Go POP3 client library for connecting and reading mails from POP3 servers.

go-pop3 A simple Go POP3 client library for connecting and reading mails from POP3 servers. This is a full rewrite of TheCreeper/go-pop3 with bug fixe

Dec 17, 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
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
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 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
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
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
Go Mail - A cross platform mail driver for GoLang.
Go Mail  - A cross platform mail driver for GoLang.

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.

Dec 16, 2022