# Help modifying Backend triggered email code

**URL:** <https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334>\
**Category:** Ask the community\
**Tags:** code\
**Created:** [March 19, 2024, 7:26am UTC](https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334 "2024-03-19T07:26:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CCorres](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@CCorres](https://forum.wixstudio.com/u/CCorres)\
**Post date:** [March 19, 2024, 7:26am UTC](https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334/1 "2024-03-19T07:26:09Z")

</div>

I am trying to create a backend function that will send a triggered email to a specified contact (not a member). The frontend function is fairly straightforward, but I’m having trouble modifying (or even understanding) the more obtuse Wix backend code example.

What I’d like to do is call the backend function with three predefined parameters:  
“ **contactData** ” is a data array to fill the variables in the triggered email  
“ **contactCode** ” is the ID of the contact, eg. “a6c1170c-fa3a-41da-923f-7e0eb09d1781”  
“ **emailCode** ” is the triggered email ID, eg. “U3NJnZb”  
Backend call should return a **success** flag to confirm that email was sent

* * *

On the front End: (my code)

```js
import { sendEmailToContact } from 'backend/sendEmailToContact.jsw';
// [...]
sendEmailToContact( **contactCode** , **contactData** , **emailCode** )
  .then(success => {
         console.log(success);
     })
     .catch(error => {
         console.log(error);
     });

```

On the backend: (Wix sample code, verbatim for now)

```js
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);
    });
});

```

How to modify the backend code to work with the frontend so that the email is sent according to the three parameters and a success flag is returned?

---

<div class="post-metadata">

**Author:** ![anthony](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/anthony/32/79536_2.png) [@anthony](https://forum.wixstudio.com/u/anthony)\
**Post date:** [March 21, 2024, 2:31pm UTC](https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334/2 "2024-03-21T14:31:47Z")

</div>

We don’t write custom code for users on this forum but if you need that support then maybe try the marketplace: [https://www.wix.com/marketplace](https://www.wix.com/marketplace)

---

<div class="post-metadata">

**Author:** ![CCorres](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@CCorres](https://forum.wixstudio.com/u/CCorres)\
**Post date:** [April 1, 2024, 1:15pm UTC](https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334/4 "2024-04-01T13:15:10Z")

</div>

I gave up trying to modify the Wix sample code, but found that this backend code works for me. Hopefully it can help others.

import {contacts, triggeredEmails} from ‘wix-crm-backend’;

export async function EmailVoucher(emailToFind,data) {  
let contactID;  
let success;  
let emailID = ‘U3NJnZb’ // can replace with the code for whatever triggered email you want to send. Could also make it one of the function parameters.

let options = {  
variables: {  
“Firstname”: data.firstName,  
“Lastname”: data.lastName,  
// add your options here in this format  
}  
};  
const queryResults = await contacts.queryContacts()  
.eq(“info.emails.email”, emailToFind)  
.find ({suppressAuth: true});  
const contactsWithEmail = queryResults.items;

if (contactsWithEmail.length \>= 1) {  
contactID = contactsWithEmail[0].\_id;  
console.log("Found contact info for " + emailToFind);  
} else {  
console.log("No contacts found for " + emailToFind);  
// Can add code to handle when no contacts found  
}

try {  
await triggeredEmails.emailContact(emailID, contactID, options);  
console.log("Email successfully sent to contact " + contactID);  
success = “yes”  
} catch (error) {  
console.error(error);  
// Can add code to handle the error  
}  
return success  
}

---

<div class="post-metadata">

**Author:** ![codequeen](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/codequeen/32/88960_2.png) [@codequeen](https://forum.wixstudio.com/u/codequeen)\
**Post date:** [April 1, 2024, 3:39pm UTC](https://forum.wixstudio.com/t/help-modifying-backend-triggered-email-code/57334/5 "2024-04-01T15:39:02Z")

</div>

Just a small note …… if a Contact is marked as Unsubscribed no email will be sent out.

So at times it may seem like the code doesn’t work, when in reality it probably is just fine.

> **[emailContact - Velo API Reference - Wix.com](https://www.wix.com/velo/reference/wix-crm-frontend/triggeredemails/emailcontact)**
>
> Sends a triggered email to the current contact, unless that contact is marked as unsubscribed. To learn more about Triggered Emails, see: - About Triggered ...

 ![IMG_7723](https://us1.discourse-cdn.com/wix/original/3X/d/c/dc2f4009ccc04235377e29c37f4d2d251fb1a465.png)
