Go bindings for Apostle.io.
import "github.com/apostle/apostle.go/apostle"
You will need to provide your apostle domain key to send emails. You can either place this value into your environment as APOSTLE_DOMAIN_KEY
, or specify it in your code.
apostle.SetDomainKey("Your Domain Key")
Sending an email is easy, a minimal example may look like this.
mail := apostle.NewMail("welcome_email", "[email protected]")
mail.Deliver()
You can pass any information that your Apostle.io template might need to the Data
attribute. Since these structs are converted using the stdlib encoding/json
package, only exportable attributes will be encoded. You can change key names by adding `json:"key_name"`
in the type definition.
type Order struct {
Id int `json:"id"`
Total float64 `json:"total"`
Items int `json:"items"`
email string // Will not be sent to Apostle
}
func MailOrder() {
o := Order{1234, 12.56, 3, "[email protected]"}
m := apostle.Mail("order_email", o.email)
m.Data["order"] = o
m.Deliver()
}
You can send multiple emails at once by using a queue.
q := apostle.NewQueue()
q.Add(apostle.NewMail("welcome_email", "[email protected]"))
q.Add(apostle.NewMail("user_signed_up", "[email protected]"))
q.Deliver()
Both NewMail
and Queue.Add
return an error to be checked. It will be one of the following:
NoEmailError
: You supplied an invalid email (an empty string)NoTemplateError
: You suppled no template id
Mail.Deliver
and Queue.Deliver
return an error to be checked. It will be one of the following:
NoDomainKeyError
: You haven't set a domain key. Either pop your domain key in theAPOSTLE_DOMAIN_KEY
environment variable, or callapostle.SetDomainKey
with it.InvalidDomainKeyError
: The server rejected the supplied domain key (HTTP 403)UnprocessableEntityError
: The supplied payload was invalid. An invalid payload was supplied, usually a missing email or template id, or no recipients key.apostle.go
should validate before sending, so it is unlikely you will see this response.ServerError
: (HTTP >= 500) – Server error. Something went wrong at the Apostle API, you should try again with exponential backoff.DeliveryError
– Any response code that is not covered by the above exceptions.
All delivery errors, with the exception of NoDomainKeyError
, have Request
and Response
attributes.
You can check the type of error like so:
err := NewMail(someTemplateVar, someEmailVar)
if err != nil{
if _, ok = err.(apostle.NoEmailError); ok {
// Email error
}else if _, ok = err.(apostle.NoTemplateError); ok {
// Template error
}
}