> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: emailContact(emailId: string, contactId: string, options: TriggeredEmailOptions) # Method package: wixCrmBackend # Method menu location: wixCrmBackend --> TriggeredEmails --> emailContact # Method Link: https://dev.wix.com/docs/velo/apis/wix-crm-backend/triggered-emails/email-contact.md # Method Description: Sends a triggered email to a contact, unless that contact is marked as unsubscribed.. The `emailContact()` function returns a Promise that resolves when the triggered email is sent to the contact. > **Note:** > This function replaces the deprecated > `wix-crm-backend.emailContact()`. > The deprecated function will continue to work, but it will not receive updates. > To keep any existing code compatible with future changes, see the > [migration instructions](https://dev.wix.com/docs/velo/api-reference/wix-crm-backend/email-contact.md). To learn more about triggered emails, see: - [About Triggered Emails](https://support.wix.com/en/article/about-triggered-emails) - [Creating a Triggered Email](https://support.wix.com/en/article/creating-a-triggered-email) - [How to Send a Triggered Email to Contacts with Code](https://support.wix.com/en/article/velo-tutorial-sending-a-triggered-email-to-contacts) Before using the `emailContact()` function, you must set up at least 1 triggered email. Specify which email to send by passing the email's ID in the `emailId` parameter. Specify which contact the email is sent to by passing a contact's ID in the `contactId` parameter. To receive the email, the contact must be subscribed or marked as 'subscription not set'. If the contact is marked as unsubscribed, the email will not be sent. Read more about how to check the contact's [subscription status](https://www.wix.com/velo/reference/wix-crm-backend/contacts/querycontacts#wix-crm-backend_contacts_querycontacts_supported-query-filters-for-extended-fields). If the specified triggered email contains variables, you can pass values for those variables using the optional `options` parameter. You pass a `variables` object which contains the values to be inserted into your email in place of the variables defined in that email. The values passed must be strings. If `variables` does not contain a `key:value` pair for a variable in your triggered email, the fallback value defined when creating your triggered email is inserted in place of the variable. > __Notes:__ > + The statistics for Triggered Emails are based on every time an email is sent, > including test emails that you send to yourself. > This is different from the > [statistics for Email Marketing](https://support.wix.com/en/article/viewing-your-email-marketing-statistics) > that are based on emails sent to unique contacts. Therefore, the statistics for Triggered > Emails may seem inflated compared to Email Marketing statistics. > + Triggered Emails generates a code snippet for each of your email > templates. The generated code includes the email's ID and keys for all the > email's variable names. You can copy and paste the snippet into your code. > Then, you need to define values for the `memberId` property and for each > variable key. To learn how to use the generated snippet in your code, see > [How to Send a Triggered Email with Code](https://support.wix.com/en/article/how-to-send-a-triggered-email-with-code). # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Send a triggered email to a contact ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { triggeredEmails } from 'wix-crm-backend'; export const myEmailContactFunction = webMethod(Permissions.Anyone, (emailId, contactId) => { return triggeredEmails.emailContact(emailId, contactId) .then(() => { console.log('Email was sent to contact'); }) .catch((error) => { console.error(error); }); }); ``` ## Send a triggered email with variable values ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { triggeredEmails } from 'wix-crm-backend'; export const myEmailContactFunction = webMethod(Permissions.Anyone, (emailId, contactId, options) => { return triggeredEmails.emailContact(emailId, contactId, options) .then(() => { console.log('Email was sent to contact'); }) .catch((error) => { console.error(error); }); }); ``` ## Query for a contact and then email the contact ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { contacts, triggeredEmails } from 'wix-crm-backend'; export const sendEmailToContact = webMethod(Permissions.Anyone, async (emailToFind) => { let contactId; const queryResults = await contacts.queryContacts() .eq('info.emails.email', emailToFind) .find(); const contactsWithEmail = queryResults.items; if (contactsWithEmail.length === 1) { console.log('Found 1 contact'); contactId = contactsWithEmail[0]._id; } else if (contactsWithEmail.length > 1) { console.log('Found more than 1 contact'); // Handle when more than one contact is found } else { console.log('No contacts found'); // Handle when no contacts are found } const triggeredEmailTemplate = 'welcome_email'; try { await triggeredEmails.emailContact(triggeredEmailTemplate, contactId); console.log('Email sent to contact'); } catch (error) { console.error(error); // Handle the error } }); ``` ---