Send emails with mailgun in your Graphcool project 🎁
graphcool add-template messaging/mailgun
The add-template
command is performing three major steps:
- Download the source files from the
src
directory and put them into your service'ssrc
directory (into a subdirectory calledmailgun
). - Download the contents from
graphcool.yml
and append them as comments to your service'sgraphcool.yml
. - Download the contents from
types.graphql
and append them as comments to your service'stypes.graphql
.
In order for the changes to take effect, you need to manually uncomment all the lines that have been added by the add-template
command.
You need to configure these credentials as environment variables:
MAILGUN_API_KEY
MAILGUN_DOMAIN
You can receive them after signing up at mailgun.
To manage your environment variables, you can use a tool like direnv.
Finally, you need to install the node dependencies and apply all the changes you just made by deploying the service:
npm install
graphcool deploy
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
}
}