Getting frustrated with triggered emails

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" or “error” or " Bad Request: please check the user inputs."
I have tried:

 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:

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?

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

thanks!

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

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

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


. 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
// Add your code for this event here:
}
/**

  • Adds an event handler that runs when the element is clicked.
    Read more
  • @param {$w.MouseEvent} event
    */

/**

  • Adds an event handler that runs when the element is clicked.
    Read more
  • @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
    // Add your code for this event here:
    }

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)