/sendgrid-go

SendGrid Library to Interface through GO

Primary LanguageGo

SendGrid-Go

SendGrid Helper Library to send emails very easily using Go.

SMTP Deprecation

SMTP API is going to be deprecated from most of our libraries in favor of just using the Web API. If you still wish to use SMTP as your transport have a look at this example.

Installation

go get github.com/sendgrid/sendgrid-go

Example

package main

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

func main() {
	sg := sendgrid.NewSendGridClient("sendgrid_user", "sendgrid_key")
	message := sendgrid.NewMail()
	message.AddTo("yamil@sendgrid.com")
	message.AddToName("Yamil Asusta")
	message.AddSubject("SendGrid Testing")
	message.AddText("WIN")
	message.AddFrom("yamil@sendgrid.com")
    if r := sg.Send(message); r == nil {
		fmt.Println("Email sent!")
	} else {
		fmt.Println(r)
	}
}

Adding Recipients

message := sendgrid.NewMail()
message.AddTo("example@sendgrid.com") // Returns error if email string is not valid RFC 5322

// or

address, _ := mail.ParseAddress("Example <example@sendgrid.com>") // make sure to import "net/mail" if you wish to use mail.ParseAddress
message.AddRecipient(address) // Receives a vaild mail.Address

Adding BCC Recipients

Same concept as regular recipient excepts the methods are:

  • AddBCC
  • AddRecipientBCC

Setting the Subject

message := sendgrid.NewMail()

message.AddSubject("New email")

Set Text or HTML

message := sendgrid.NewMail()

message.AddText("Body")

//or

message.AddHTML("<html><body>Stuff, you know?</body></html>")

Set From

message := sendgrid.NewMail()

message.AddFrom("example@lol.com")

Set File Attachments

message := sendgrid.NewMail()

message.AddAttachment("./stuff.txt")

//or

message.AddAttachmentLink("filename", "http://example.com/file.zip")

//or

message.AddAttachmentStream("filename", []byte("some file content"))

SendGrid's X-SMTPAPI

If you wish to use the X-SMTPAPI on your own app, you can use the SMTPAPI Go library.

message := sendgrid.NewMail()

message.AddSubstitution("key", "value")
message := sendgrid.NewMail()

message.AddSection("section", "value")
message := sendgrid.NewMail()

message.AddCategory("category")
message := sendgrid.NewMail()

message.AddUniqueArg("key", "value")
message := sendgrid.NewMail()

message.AddFilter("filter", "setting", "value")

AppEngine Example

package main

import (
	"fmt"
	"appengine/urlfetch"
	"github.com/elbuo8/sendgrid-go/sendgrid"
)

func handler(w http.ResponseWriter, r *http.Request) {
	sg := sendgrid.NewSendGridClient("sendgrid_user", "sendgrid_key")
	c := appengine.NewContext(r)
	// set http.Client to use the appengine client
	sg.Client = urlfetch.Client(c) //Just perform this swap, and you are good to go.
	message := sendgrid.NewMail()
	message.AddTo("yamil@sendgrid.com")
	message.AddSubject("SendGrid is Baller")
	message.AddHTML("Simple Text")
	message.AddFrom("kunal@sendgrid.com")
	if r := sg.Send(message); r == nil {
		fmt.Println("Email sent!")
	} else {
		c.Errorf("Unable to send mail %v",r)
	}
}

Kudos to Matthew Zimmerman for this example.

###Tests

Please run the test suite in before sending a pull request.

go test

##MIT License

Enjoy. Feel free to make pull requests :)