How to use User Invites with Mails API
You can utilize the User Invites API in conjunction with the sendMail()
function from the Mails API. There are numerous scenarios where it is advantageous to use the Mails API along with User Invites. For instance, if you wish to send supplementary details via email subsequent to the invitee's acceptance, or if you need to forward information about the invitation to a different user's email address.
As an illustration, we aim to provide instructions on how to begin using our application once invitations are accepted. To send an email, we will utilize a code similar to this:
const sendMailStatus = await roqClient.asSuperAdmin().sendMail({
mail: {
key: 'onboarding-getstarted',
locale: 'en-US',
emails: ['user007@mewmail.com'],
data: [
{ key: 'recipients_name', value: 'any_name' },
{ key: "getting_started_url", value: "https://docs.roq.tech/getting-started" },
{ key: "your_name", value: "user_invitee_name" }]
},
});
Make sure to check if you have an email template with the key onboarding-getstarted
on the ROQ Console before sending any emails.
To create an email template on the ROQ Console. Please look into this tutorial.
To provide support for emails sent to numerous invitees, we can retrieve the list of invitees and then sort it according to their status
property. Emails will be sent only to those with an accepted
status value.
import 'dotenv/config';
import { Platform } from '@roq/nodejs';
/**
* Connect to the ROQ Platform
* Credentials in .env file
*/
const roqClient = new Platform({
apiKey: process.env.ROQ_API_KEY,
environmentId: process.env.ROQ_ENVIRONMENT_ID,
host: process.env.ROQ_PLATFORM_URL
})
async function sendMailToAcceptedInvites(emailAddress) {
const search = await roqClient.asSuperAdmin().userInvites({
filter: {
email: {
equalTo: emailAddress
}
}
});
// Iterate over all the invites
for (let invite of search.data.userInvites.data) {
if (invite.status === 'accepted') {
const email = invite.email;
const name = invite.firstName + ' ' + invite.lastName;
// Send the mail
const sendMailStatus = await roqClient.asSuperAdmin().sendMail({
mail: {
key: 'onboarding-getstarted',
locale: 'en-US',
emails: [email],
data: [
{
key: 'recipients_name',
value: name
},
{ key: "getting_started_url",
value: "https://docs.roq.tech/getting-started"
},
{ key: "your_name",
value: name
}
]
},
});
console.log(`Mail sent for accepted invite: ${invite.id}`);
}
}
}
To search for an invitee with the email address user007@mewmail.com
and send them an onboarding get started email if they accept the invitation:
await sendMailToAcceptedInvites("user007@mewmail.com").catch(console.error);