Created
April 19, 2016 20:19
-
-
Save johnsogg/c7ca4652925d9fe90450be01017861f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
example.go | |
This is a golang example of how to use SendGrid for transactional | |
email that uses substitutions (e.g. replace "firstName" with | |
"Grover"). It assumes you have a SendGrid API key. | |
I made this because I had to move away from Mandrill by April 27, and | |
SendGrid is basically across the street from me so they were the first | |
alternative that came to mind. | |
*/ | |
package main | |
import ( | |
"fmt" | |
"github.com/sendgrid/sendgrid-go" | |
"net/mail" | |
"os" | |
) | |
func main() { | |
// Make a SendGrid Transactional template and have it look like this | |
// (in Design mode): | |
/* | |
<%body%> | |
Welcome *|firstName|* *|lastName|*! | |
I hope you like this email. It is **configurable**. | |
*/ | |
// Create a client with an API key | |
sendgridKey := "<insert your sendgrid api key here>" | |
sg := sendgrid.NewSendGridClientWithApiKey(sendgridKey) | |
// Create message and configure with empty text to force template | |
// body to be used instead. You have to set up a transactional | |
// template on SendGrid's web site and reference its ID below where | |
// it says <template_id>. | |
message := sendgrid.NewMail() | |
message.SetText("\n") | |
message.SetHTML(" ") | |
message.SetSubject("This is a **configurable** subject line") | |
message.AddFilter("templates", "enable", "1") | |
message.AddFilter("templates", "template_id", "<template_id>") | |
// Add substitutions that will replace the left value with the right | |
// value whenever it is seen in the subject or body. The subject | |
// line, for example, will be transformed to "This is a fantastic | |
// subject line". | |
// | |
// Notice that I'm using MailChimp-flavored escape sequences for the | |
// first and last names. I used double asterisks for the last one | |
// because I felt like it. You don't in fact need to escape the | |
// substitution phrase at all. | |
message.AddSubstitution("*|firstName|*", "Grover") | |
message.AddSubstitution("*|lastName|*", "Cleveland") | |
message.AddSubstitution("**configurable**", "fantastic") | |
// Configure where the message will come from. | |
address, err := mail.ParseAddress("[email protected]") | |
if err != nil { | |
fmt.Println("Malformed 'from' email address.") | |
os.Exit(-1) | |
} | |
message.SetFromEmail(address) | |
message.SetFromName("YourDomain Support") | |
// Add categories for our own tracking purposes | |
var categories = []string{ | |
"Welcome Email", | |
} | |
message.AddCategories(categories) | |
// Specify who the message will be to | |
recipients := []string{ | |
"[email protected]", | |
} | |
message.AddTos(recipients) | |
// Send the mail. | |
err = sg.Send(message) | |
if err != nil { | |
fmt.Printf("%v\n", err) | |
os.Exit(-1) | |
} | |
fmt.Println("Got to the end without exploding. Mail sent!") | |
// The resulting email should be: | |
// Subject: This is a fantastic subject line | |
// Body: Welcome Grover Cleveland! I hope you like this email. It is fantastic. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment