# Getting frustrated with triggered emails

**URL:** <https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141>\
**Category:** Ask the community\
**Tags:** question, code, wix-members-area\
**Created:** [November 26, 2021, 6:59pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141 "2021-11-26T18:59:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wynngeisler](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@wynngeisler](https://forum.wixstudio.com/u/wynngeisler)\
**Post date:** [November 26, 2021, 6:59pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/1 "2021-11-26T18:59:42Z")

</div>

Hello, I am trying to make a script to create a contact, then send a triggered email to that contact. Whatever I do, I always get " Wix code SDK error: variable “contactTo” value must be uuid. For more information visit [https://www.wix.com/velo/reference/wix-crm/triggeredemails-obj/emailcontact"](https://www.wix.com/velo/reference/wix-crm/triggeredemails-obj/emailcontact%22) or “error” or " Bad Request: please check the user inputs."  
I have tried:

```javascript
 wixCrm.createContact( {
6 "firstName": $w("#firstName").value,
7 "lastName": $w("#lastName").value,
8 "emails": [$w("#email").value],
9 "phones": [$w("#phone").value]
10 } )
11 .then( (contactId) => {
12 wixCrm.emailContact("thankyou", contactId, {
13 "variables": {
14 "firstName": $w("#firstName").value,
15 "lastName": $w("#lastName").value
16 }
17 } );

```

and:

```javascript
import { contacts, triggeredEmails } from 'wix-crm';
2
3$w.onReady(function () {
4 $w('#createContact').onClick(async () => {
5 const firstName = $w('#firstName').value;
6 const lastName = $w('#lastName').value;
7 const email = $w('#email').value;
8 const phone = $w('#phone').value;
9
10 const contactInfo = {
11 name: { first: firstName, last: lastName },
12 emails: [{ email: email }],
13 phones: [{ phone: phone }],
14 };
15
16 let resolvedContact;
17
18 try {
19 resolvedContact = await contacts.appendOrCreateContact(contactInfo);
20 console.log('Resolved to contact', resolvedContact);
21
22 if (resolvedContact.identityType !== 'CONTACT') {
23 console.log('Current contact is already a site member. Not sending a welcome email.');
24
25 // emailContact() cannot be used to email site members.
26 // If you want to email a member, use triggeredEmails.emailMember()
27
28 return;
29 } else {
30
31 const emailId = 'Spp8KfP';
32 const contactId = resolvedContact.contactId;
33 const options = {
34 variables: { firstName: firstName, lastName: lastName },
35 };
36
37 await triggeredEmails.emailContact(emailId, contactId, options);
38 console.log('Emailed contact');
39 }
40 } catch (error) {
41 console.error(error);
42 }
43 });
44});

```

I have played around with the code so much and still can’t get any solution.  
Can I have some help with this?

---

<div class="post-metadata">

**Author:** ![jacobg](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jacobg/32/29611_2.png) [@jacobg](https://forum.wixstudio.com/u/jacobg)\
**Post date:** [November 26, 2021, 7:45pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/2 "2021-11-26T19:45:54Z")

</div>

@wynngeisler I forwarded this to relevant devs and will get back you with an answer when it’s available.

---

<div class="post-metadata">

**Author:** ![wynngeisler](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@wynngeisler](https://forum.wixstudio.com/u/wynngeisler)\
**Post date:** [November 26, 2021, 7:46pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/3 "2021-11-26T19:46:56Z")

</div>

thanks!

---

<div class="post-metadata">

**Author:** ![jacobg](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jacobg/32/29611_2.png) [@jacobg](https://forum.wixstudio.com/u/jacobg)\
**Post date:** [November 29, 2021, 7:18am UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/4 "2021-11-29T07:18:13Z")

</div>

@wynngeisler Please follow [https://support.wix.com/en/article/velo-creating-a-triggered-email](https://support.wix.com/en/article/velo-creating-a-triggered-email) .  
From what I see, triggered emails work.

---

<div class="post-metadata">

**Author:** ![wynngeisler](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@wynngeisler](https://forum.wixstudio.com/u/wynngeisler)\
**Post date:** [November 29, 2021, 9:22pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/5 "2021-11-29T21:22:27Z")

</div>

I just saw that the code only works on the published site! Thank you! I will finish the code then tell you if I need more help

---

<div class="post-metadata">

**Author:** ![wynngeisler](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@wynngeisler](https://forum.wixstudio.com/u/wynngeisler)\
**Post date:** [November 29, 2021, 10:00pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/6 "2021-11-29T22:00:30Z")

</div>

Ok, I got it working, except it is trying to send emails too fast and it keeps failing. What do I do? I have a list in content manager with all the email addresses i want to send the triggered email to

 ![](https://us1.discourse-cdn.com/wix/original/3X/1/9/197f21f30cf03a57fa3c28f5e7393149457cbb26.png)  
. I made a code that gets the value of each email, then creates a contact and sends an email. here is my code:

export function sendEmail ( tcode , email ) {  
wixCRM . createContact ({  
“firstName” : email ,  
“emails” : [email]  
})  
. then (( contactId ) =\> {

```
  console . log ( contactId ) 

```

wixCRM . triggeredEmails . emailContact ( tcode , contactId , {  
“variables” : {  
idCode : email  
}  
})  
. then (() =\> {  
console . log ( “Sent!” )  
// do something after the email was sent  
})  
. catch (( err ) =\> {  
console . log ( “err” )  
// handle the error if the email wasn’t sent  
});  
});  
// This function was added from the Properties & Events panel. To learn more, visit [Velo: Working with the Properties & Events Panel | Help Center | Wix.com](http://wix.to/UcBnC-4)  
// Add your code for this event here:  
}  
/\*\*

- Adds an event handler that runs when the element is clicked.  
[Read more](https://www.wix.com/corvid/reference/$w.ClickableMixin.html#onClick)
- @param {$w.MouseEvent} event  
\*/

/\*\*

- Adds an event handler that runs when the element is clicked.  
[Read more](https://www.wix.com/corvid/reference/$w.ClickableMixin.html#onClick)
- @param {$w.MouseEvent} event  
\*/  
**export async function** yesButton\_click ( event ) {  
**await** wixData . query ( “Codes” )  
. find ()  
. then ( ( results ) =\> {  
if ( results . items . length \> 0 ) {  
**for** ( let i = 0 ; i \< results . items . length ; i ++) {  
console . log ( "i = " + i )  
let curItem = results . items [i]  
console . log ( "email = " + curItem . value )  
sendEmail ( “Spp8KfP” , curItem . value )  
}  
//see item below  
} **else** {  
// handle case where no matching items found  
}  
} )  
. catch ( ( err ) =\> {  
let errorMsg = err ;  
} );  
// This function was added from the Properties & Events panel. To learn more, visit [Velo: Working with the Properties & Events Panel | Help Center | Wix.com](http://wix.to/UcBnC-4)  
// Add your code for this event here:  
}

---

<div class="post-metadata">

**Author:** ![wynngeisler](https://avatars.discourse-cdn.com/v4/letter/w/ba9def/32.png) [@wynngeisler](https://forum.wixstudio.com/u/wynngeisler)\
**Post date:** [November 29, 2021, 10:37pm UTC](https://forum.wixstudio.com/t/getting-frustrated-with-triggered-emails/3141/7 "2021-11-29T22:37:00Z")

</div>

Ok, so I think the problem is with using async and await commands. I also heard somewhere that you have to move the code to the back end. I keep on getting this error:  
contactId does not match current session contact (401)
