@amplifiers/amplify-graphql-send-email-transformer
v1.3.5
Published
Amplify GraphQL @sendEmail transformer.
Downloads
52
Maintainers
Readme
amplify-graphql-send-email-transformer
Description
This AWS Amplify directive allows you to send emails from your GraphQL API. It uses AWS SES service to send emails. Once a database INSERT, MODIFY, REMOVE operation is performed, the template is populated with data from the mutation and email is sent out to the recipient.
@sendEmail
Definition
directive @sendEmail(template:Template, trigger:String="INSERT") on OBJECT
input Template {
subject: String!
bodyText: String!
bodyHtml: String!
sender: String!
recipient: String!
}
Installation
npm install --save @amplifiers/amplify-graphql-send-email-transformer
Import
/amplify/backend/api/<API_NAME>/transform.conf.json
{
"transformers": [
"@amplifiers/amplify-graphql-send-email-transformer"
]
}
Usage
Append the @sendEmail
directive to a model in your GraphQL schema. The @sendEmail
directive takes a trigger
string and template
input object as arguments.
trigger
field specifies the BD event type and can be one of the following values: INSERT
, MODIFY
, REMOVE
subject
contains the subject of the email.
bodyHtml
contains the HTML body of the email.
bodyText
contains the text body of the email.
The sender
is the email you have configured with SES to send out the emails.
The recipient
is the email address of the recipient.
You can use the {{}}
syntax to populate your fields with data from the database event.
Example:
type User
@model
@sendEmail(
trigger: "INSERT"
template: {
subject: "Welcome to Amplify"
bodyHtml: "<body><h1 style='color:#ff00ff'>Hello, {{name}}! Welcome to Amplify!</h1></body>"
bodyText: "Hello, {{name}}! Welcome to Amplify!\nMessage here"
sender: "[email protected]",
recipient: "{{email}}"
}
) {
id: ID!
name: String!
email: String!
}
Setup AWS SES
If it is your first time using SES you will be in a sandbox where you have multiple restrictions and only allowed to send from and to whitelisted email addresses. You can whitelist your own email address by following the instructions:
- Go to SES Service in AWS Console and Click “Email Addresses” under Identity Management. Remember the AWS Region where you are registering the email.
- Click on “Verify a new email address” and add the email address to which emails will be sent. Remember email addresses are case sensitive.
- The above action will send a verification mail from AWS to the added email address to confirm Id and accept emails. Once the user of that email address verifies, you can check “Verification Status” turns to “verified”.
- You can “Send a Test Email” to verify the same and check the stats of sent emails in the SES console.
Development and Contributions
Contributions are more than welcome! Please feel free to open an issue or a pull request. Developer docs are here