Sending 2 Different Emails on Form Submission

Hi,

I would like to send 2 different emails on a form submission. Currently my Page Code looks like this:

//automatedEmails

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

$w.onReady(function () {
  $w("#dataset1").onAfterSave(sendFormData);
});

function sendFormData() {
  const subject = `Booking Confirmation`;
  const body = `Hi ${$w("#input1").value},
\nWe have received your booking request for Product X.
\nOur Hotel Representative will be visiting you shortly to confirm your booking. If you wish to book another service simply go to https://www.concorde-excursions.com In the meantime we hope you are enjoying your vacation in Mauritius with us.
\n
\nPlease let us know if we can be of any assistance,
\n
\nX Company Team`;
    const recipient = $w("#input4").value;
 
 sendEmailWithRecipient(subject, body, recipient)
 .then(response => console.log(response));
 
  sendEmail(subject, body)
    .then(response => console.log(response)); 
}

I would like my office email to receive another email which contains information from the user inputs.

Is it possible ?

Sure. Just call the sendEmailWithRecipient a second time with your office email address as the recipient parameter.

Thank you Sam

Hi @shantanukumar847
I’m also trying to send 2 different email on form submission. Can you pls provide the backend javascript files for the above code. It would be much helpful.
Thanks!

Hi,

This is already available at Velo Tutorial: Sending an Email on Form Submission | Help Center | Wix.com
But I will type it for you anyway

1st Backend File (newBooking.jsw)

//newBooking.jsw

import {sendWithService} from 'backend/sendGrid';

export function sendEmail(subject, body) {
  const key = "your api key here";
  const sender = "no-reply@company.com";
  const recipient = "shantanu@company.com";
  return sendWithService(key, sender, recipient, subject, body);
}

export function sendEmailWithRecipient(subject, body, recipient) {
	const key = "your api key here";
	const sender = "no-reply@company.com";
	return sendWithService(key, sender, recipient, subject, body);
}

2nd Backend File (sendGrid.js)

//sendGrid.js

import {fetch} from 'wix-fetch';  

export function sendWithService(key, sender, recipient, subject, body) {
  const url = "https://api.sendgrid.com/api/mail.send.json";
 
  const headers = {
    "Authorization": "Bearer " + key,
    "Content-Type": "application/x-www-form-urlencoded"
  };

  const data = `from=${sender}&to=${recipient}&subject=${subject}&text=${body}`;
 
  const request = {
    "method": "post", 
    "headers": headers, 
    "body": data
  };
 
  return fetch(url, request)
   .then(response => response.json());
}

Page Code:

//automatedEmails

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

$w.onReady(function () {
  $w("#dataset1").onAfterSave(sendFormData);
});

function sendFormData() {
  const subject1 = `Booking Confirmation`;
  const body1 = `body of mail`;
    const recipient = $w("#input4").value;
    
    const subject2 = `English Client Booking`;
    const body2 = `body of mail`;
 
 sendEmailWithRecipient(subject1, body1, recipient)
 .then(response => console.log(response));
 
  sendEmail(subject2, body2)
    .then(response => console.log(response)); 
}

Hope this helps,

Have Fun

Thank you for sharing!