emailContact not working "contactId does not match current session contact (401)"

Hi all:

I am having issues with the emailContact function. For some emails, it’s not working with the 401 error " contactId does not match current session contact (401) ".

My idea is to create a form where the visitor enters his name and email to get a coupon. I don’t want visitors to signup for the site for this.

I am creating the contact before sending the email, but still no luck:

wixCRM.createContact({
“firstName” : $w( ‘#txtNombre’ ).value,
“lastName” : $w( ‘#txtAppellido’ ).value,
“emails” : [$w( “#txtEmail” ).value]
})
.then((contactId) => {
console.log( "contactId : " + contactId);

//bunch of other validations i make before deciding to send the email

                        wixCRM.emailContact( 'S8Q6ivl' , contactId, { 
                            variables: { 
                                fecha_desde: fechaD, 
                                fecha_hasta: fechaH, 
                                descripcion_cupon: desc, 
                                nro_cupon: results.items[ 0 ].codCupon 
                            } 
                        }). **catch** ((err) => { 
                            console.log( "Error al Enviar mail: "  + err); 
                        }); 
                     
    }); 

I’ve checked and the contact was created, I am getting a contactId, so I don’t get why I cannot send the email.

Thanks and regards,

1 Like

Forget about this, I’ve found the anwer: I have to use the backend to send the email, because when it’s done from the frontend, every time I call the createContact function, that contact session is logged and that’s why it will give me an error if I try again with a different contactId.

More details are in: https://www.wix.com/corvid/forum/community-discussion/triggered-emails-to-contacts-2020