Send Email with afterInsert Hook from Database

My Database receives an entry and This afterInsert Hook from the Database should be sending an email via the sendgrid Backend Module

//data.js

import {sendEmail, sendEmailWithRecipient} from 'backend/newOrder';

export function Payments_afterInsert(item, context) {
	
	item.clientEmail;
	item.amount;
	item.clientName;
	
	const subject = `Order Confirmation`;
	const body = `Dear clientName, Your order for item.amount`;
	const recipient = item.clientEmail;

	sendEmailWithRecipient(subject, body, recipient)
		.then(response => console.log(response));

	sendEmail(subject, body)
		.then(response => console.log(response));
}

What am i doing wrong ?

Hi Shan,

I see no problem with your code.

Note, you won’t see the logs because the function sends emails asynchronously but returns without waiting until the emails are sent.

If you want to see the logs, the function should return a promise that resolves when the both emails are sent. You can use async syntax for simplicity:

import {sendEmail, sendEmailWithRecipient} from 'backend/newOrder';

export async function Payments_afterInsert(payment, context) {
	const subject = `Order Confirmation`;
	const body = `Dear ${payment.clientName}, Your order for ${payment.amount}`;
	const recipient = payment.clientEmail;

  try {
    console.log('sendEmailWithRecipient');
    const response = await sendEmailWithRecipient(subject, body, recipient)
    console.log('sendEmailWithRecipient resolved with', response);
  } catch (error) {
    console.log('sendEmailWithRecipient rejected with', error);
  }

  try {
    console.log('sendEmail');
    const response = await sendEmail(subject, body)
    console.log('sendEmail resolved with', response);
  } catch (error) {
    console.log('sendEmail rejected with', error);
  }
}

Try it out and tell me if you start seeing the logs.

Regards,
Yevhen

Works like a Charm!!

Yevhen, Thanks you so much man! I wish you a very nice weekend ahead.

You too!

Glad to be of help.

I keep getting this error: "Failed to load wix data hooks: Cannot find module ‘backend/Submissions’. Is there another module I should install?
Also, is there a way to attach a file attachment on a form submission to an email?

@c3291393

Have you got a file in your backend called Submissions?
https://support.wix.com/en/article/corvid-about-data-hooks
https://support.wix.com/en/article/corvid-using-data-hooks

@givemeawhisky
Wow I feel real dumb… I do not.
That’s the name of my database/submission form.

What do I need to have in the file for the import to work (sorry pretty new to this)

@c3291393

If you are after the same tutorial that Shan used before he altered his code, then see this one here.
https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission

Although, that is an old tutorial and has been passed on with these two.

Through SendGrid REST API.
https://www.wix.com/corvid/forum/wix-tips-and-updates/example-send-email-with-the-sendgrid-rest-interface

Through SendGrid NodeJS.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-npm-interface

Although, depending on what you actually want to do there are few other ways.

Simplest with Wix Automations.
https://support.wix.com/en/ascend-by-wix/wix-automations

Triggered emails.
https://support.wix.com/en/marketing-tools-analytics/triggered-emails
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-contacts
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members

@givemeawhisky
Wow thank you so much!

Is there any way to attach a file attached to my form submission to the email?

@c3291393 Yes you can.

Check out the Send Mail Docs from SendGrid https://sendgrid.api-docs.io/v3.0/mail-send/v3-mail-send

You will need to upload the file, get the URL of the file and then use nodejs-base64-encode (NPM) to encode the URL of the file inside a request.get() function using the request-promise NPM.

Use the SendGrid NPM example (link provided by @givemeawhisky in the above comments)