Send emails with mailgun in your Graphcool project 🎁
npm -g install graphcool
graphcool init
graphcool modules add graphcool/modules/messaging/mailgun
In your base project, you need to configure the following environment variables.
MAILGUN_API_KEY
: mailgun API KeyMAILGUN_DOMAIN
: mailgun domain
You can receive them after signing up at mailgun.
An easy way to setup environment variables is using direnv.
To use direnv
, put the following into .envrc
in you project root:
export MAILGUN_API_KEY=xxx
export MAILGUN_DOMAIN=xxx
Use the sendMailgunEmail
mutation to send emails according to its parameters:
tag: String!
: custom tag for internal loggingfrom: String!
: sender emailto: [String!]!
: a list of recipient emailssubject: String!
: the email subject, can contain references torecipientVariables
text: String!
: the email body, can contain references torecipientVariables
recipientVariables: Json
: optional recipient variables for batched emails. Read the documentation for more information on the encoding.
Go to the Graphcool Playground:
graphcool playground
Hook into the function logs:
graphcool logs -f sendEmail --tail
Run this mutation to send a single email:
mutation {
# replace __SENDER_EMAIL__ and __RECIPIENT_EMAIL__ with *authorized* email addresses!
sendMailgunEmail(
tag: "2017-09-16-welcome-email"
from: "__SENDER_EMAIL__"
to: "__RECIPIENT_EMAIL__"
subject: "A new email from the Graphcool mailgun module!"
text: "This is your first email from the Graphcool mailgun module!"
) {
success
}
}
Run this mutation to send a batched email:
mutation {
# replace __SENDER_EMAIL__, __FIRST_EMAIL__ and __SECOND_EMAIL__ with *authorized* email addresses!
sendMailgunEmail(
tag: "2017-09-16-batched-welcome-email"
from: "__SENDER_EMAIL__"
to: ["__FIRST_EMAIL__", "__SECOND_EMAIL__"]
subject: "A new email from the Graphcool mailgun module!"
text: "Hey %recipient.name%, this is your first email from the Graphcool mailgun module!"
recipientVariables: "{\"__FIRST_EMAIL__\": {\"name\": \"First\"}, \"__SECOND_EMAIL__\": {\"name\": \"Second\"}}"
) {
success
}
}