Automated email on document upload

I have a page on our website that has applications for employment. I’ve coded it to where the visitor can upload the document after they’ve completed it. Is there a way to have it automatically email me once a document has been uploaded?
I’ve seen articles where the triggered emails can email the currently logged in user using the wix-users API, but I haven’t found a way to code this particular situation.
Any ideas?

You should use sendGrid in Wix Code. The trigger email functionality demands that all visitors become users on your site. If they are registred users you should use Trigger Emails. If not use sendGrid.

I usually store applications in a Data Collection first because it is nice to have them stored for later use.

If you store them in a Data Collection I recommend that you create a Data Hook for that Data Collection. This means that every time there is a new record saved in that Data Collection this hook will be triggered.

This code goes into the data.js file.

export function datacollectionName_afterInsert(item, context) {

	let name = item.title; // All items in data collection can be reached using item.fieldKey
	
	let doccvurl = "";
	const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
  	const matches = cvurl.match(convertRegex);
  	const documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
	doccvurl = "https://" + documentUrl;
	const subject = `Email subject`;
	
	const body = `Your body goes here: ${name}	  
	const fromemail = "from@wixshow.com";
	  const toemail =  "to@wixshow.com";
	 
	  sendEmail(subject, body, fromemail,toemail)
	    .then(response => console.log(response)); 
}

Then in the backend folder create sendGrid.js with

//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}&cc=${sender}&to=${recipient}&subject=${subject}&text=${body}`; 
 
  const request = {
    "method": "post", 
    "headers": headers, 
    "body": data
  };
 
  return fetch(url, request)
   .then(response => response.json());
}

And also in the backend folder create sendgrid.jsw with this code

//sendgrid.jsw
// Andreas Kviby of WixShow

import {sendWithService} from 'backend/sendGrid';

export function sendEmail(subject, body,fromemail, toemail) {
  const key = "YOUR SEND GRID KEY HERE";
  const sender = fromemail;
  const recipient = toemail;
  return sendWithService(key, sender, recipient, subject, body);
}

That’s it :slight_smile: It seem a bit complicated but when up and running it works like a clock.

Hi what is " cvurl " here getting error as cvurl is not defined