The SendGrid Templates package allows you to access SendGrid’s transactional email templates from within your Wix site. You can access your templates including any different versions, use them to send emails with dynamic content, and schedule your emails to be sent at a specific time. The package also enables you to cancel or pause your scheduled email sends, retrieve a list of scheduled email sends, and clear the status of a scheduled send.
Before using the package, set up the following:
velo-sendgrid-api-key
.Configure the following in your config.json file:
senderEmail
: The verified sender email address used when setting up Single Sender Verification in your SendGrid account. This is the email address your dynamic template emails will be sent from.emailDefaultContent
: A string of text formatted with HTML tags. It defines the default content of emails sent when no template is provided to SendGrid’s Mail Send API. You can customize the default content as needed.Example config.json file:
The following files are included in the package. Note that only exported functions that you can use in your site are listed here.
The code in this file contains the configurations needed for using this package. Follow the instructions in the Setup section to edit the file with your configurations.
The code in this file contains functions that get a list of SendGrid email templates, get a specific email template, get a specific version of an email template, and send emails based on templates.
To use the functions below in your code, import them with the following syntax:
`import { <functionName> } from '@velo/sendgrid-templates-backend'`
getEmailTemplates()
Gets a list of SendGrid email templates. You can choose to retrieve all available templates or only a specific page.
Syntax:
Parameters:
paging
: (optional) An object that contains the details needed to get a list of templates. The object contains the following properties to specify the page a user wants:
page_size
: (optional) The number of templates to be returned per page of results. Defaults to the maximum size of 200.page_url
: (optional) A URL that corresponds to a specific page of email templates, provided by the result’s metadata. See the example return value below. The page_url
is the value of the ‘self’ in the _metadata
object.page_token
: (optional) A string corresponding to a specific page of email templates, provided by the result's metadata. See the example return value below. The page_token
is a query parameter that is part of the page URL.Returns: A promise that resolves to an object containing an array of template objects, and a metadata object.
Example return value:
getEmailTemplateById()
Gets a specific email template, including all versions, by ID.
Syntax:
Parameters:
template_id
: A string representing a specific email template.Returns:
A promise that resolves to a template object containing all template details including all versions of the template.
Example return value:
getEmailTemplateVersionById()
Gets the details of a specific version of an email template by ID.
Syntax:
Parameters:
template_id
: A string representing a specific email template.version_id
: A string representing a specific version of the email template.Returns: A promise that resolves to an object containing a specific version of a template with its details.
Example return value:
sendEmailTemplate()
Sends a transactional email using a template and dynamic content.
Syntax:
Parameters:
details
: An object that contains the following properties:
template_id
: A string representing a specific email template.personalizations
: An array of objects representing a contact list and dynamic data required by the email template such as images, texts, and links. Each object must contain the following 2 properties:
to
: An array of objects containing the emails and names of the intended recipients.
dynamic_template_data
: An object representing the dynamic data required by an email template:
You can find other optional properties for personalizations
in the SendGrid Mail Send API Documentation.
send_at
: (optional) A number, written as a unix timestamp, representing the time you want your email to be sent. This value can be overridden by using the optional send_at
property in the personalizations
object. Delivery of an email cannot be scheduled more than 72 hours in advance.batch_id
: (optional) A string representing a batch of emails that are sent simultaneously. Including a batch_id
in your request adds the new email to that batch. It also enables you to cancel or pause the send of a scheduled email.details
object, such as from
and **reply_to
** in the SendGrid Mail Send API Documentation.Returns: A promise that resolves to a response object with a 202 response code and status text.
Example return value:
Note: It’s better to schedule emails to be sent at off-peak times. Most emails are scheduled and sent at the top of the hour or half hour. Scheduling sends at an off-peak time, such as 10:53, can result in lower deferral rates due to reduced traffic.
The code in this file contains functions that cancel or pause your scheduled email sends, and resume sending canceled or paused sends, as long as their send_at
time hasn’t arrived. There are also functions for updating the status of your scheduled sends, and getting a list of the sends that are paused or canceled.
To use the functions below in your code, import them with the following syntax:
`import { <functionName> } from '@velo/sendgrid-templates-backend'`
getBatchId()
Generates a batch ID string that is used to represent a group of emails that are sent simultaneously.
Note: You can have up to 100 unique batch IDs at a time. Syntax:
Parameters: None
Returns: A promise that resolves to a batch ID string.
cancelPauseScheduledEmails()
Cancels or pauses the send of one or more scheduled emails using a batch ID.
Syntax:
Parameters:
batch_id
: A string representing a specific batch of emails that are sent simultaneously.status
: A string representing the batch send status. The status can be one of the following:
‘cancel’
‘pause’
Returns: A promise that resolves to a response object containing the batch ID and the new batch send status.
Example return value:
Notes:
cancelPauseScheduledEmails()
to pause or cancel a send, you must use either updateScheduleSendStatus()
or clearScheduleSendStatus()
to change or remove the send status.send_at
value has passed.send_at
value has passed. If the pause status is removed within those 72 hours, the batch is delivered. The batch is discarded 72 hours after the send_at
value has passed.clearScheduledSendStatus()
Removes the canceled or paused status of a batch’s scheduled send.
Syntax:
Parameters:
batch_id
: A string representing a specific batch of emails that are sent simultaneously.Returns: A promise that resolves to a response object with a 204 response code and status text.
Example return value:
updateScheduledSendStatus()
Updates the status of a batch’s scheduled send.
Syntax:
Parameters:
batch_id
: A string representing a specific batch of emails that are sent simultaneously.status
: A string representing the batch status. The status can be one of the following:
’cancel’
’pause’
Returns: A promise that resolves to a response object with a 204 response code and status text.
getScheduledSendsStatus()
Gets the scheduled send status for a specific batch ID or all batch IDs.
Syntax:
Parameters:
batch_id
:(optional) A string representing a specific batch of emails that are sent simultaneously.Returns: A promise that resolves to an array of objects containing batch IDs and their scheduled send statuses.
Example return value:
Note: This function only returns the scheduled sends that have a batch ID. It is possible to schedule a send for an email without assigning it a batch ID. However, we recommend assigning a batch ID to any scheduled send that may need to be paused or canceled in the future.
None
1.0 Initial Version.
SendGrid, templates, emails, dynamiccontent, scheduledsends, batch