paganotoni / sendgrid-sender

Buffalo's sender implementation for Sendgrid in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sendgrid Buffalo Sender

This is a buffalo sender for the Sendgrid email service.

How to use

In your mailers.go

import (
    ... 
    ssender "github.com/paganotoni/sendgrid-sender"
)

var sender mail.Sender

func init() {
	APIKey := envy.Get("SENDGRID_API_KEY", "")
	sender = ssender.NewSendgridSender(APIKey)
}

And then in your mailers you would do the same sender.Send(m) as this sender matches buffalos mail.Sender interface.

Add Custom Args

To add custom args, you must add values using CustomArgs type and add it into Message.Data ussing CustomArgsKey key

CustomArgsKey = "sendgrid_custom_args_key"
...
type CustomArgs map[string]string

How to use Sendgrid customargs

One thing you could need is to add customArgs to the message you're sending through sendgrid, to do this you would be using SetCustomArgs function, passing your mail.Messagewith the CustomArgs you want to add.

import (
	...
    ssender "github.com/paganotoni/sendgrid-sender"
)

func main() {
	APIKey := envy.Get("SENDGRID_API_KEY", "")
    sender = ssender.NewSendgridSender(APIKey)

	m := mail.NewMessage()
    ...
    ssender.SetCustomArgs(m, ssender.CustomArgs{
        "custom_arg_0": "custom_value_0",
        "custom_arg_1": "custom_value_1",
        ...
    })

    if err := sender.Send(m); err != nil{
        ...
    }
}

Test mode

Whenever the GO_ENV variable is set to be test this sender will use mocksmtp sender to send messages, you can read values in your tests within the property TestSender of the SendgridSender.

About

Buffalo's sender implementation for Sendgrid in Go


Languages

Language:Go 100.0%