Skip to content

Instantly share code, notes, and snippets.

@lynndylanhurley
Created January 4, 2021 19:49
Show Gist options
  • Save lynndylanhurley/5e316c52fd4d26de3f8f4f837f1686c0 to your computer and use it in GitHub Desktop.
Save lynndylanhurley/5e316c52fd4d26de3f8f4f837f1686c0 to your computer and use it in GitHub Desktop.
const client = require('@sendgrid/mail');
require('dotenv').config();
function sendEmail(sendgridClient, message, senderEmail, senderName, receiverEmail, contactEmail) {
return new Promise((fulfill, reject) => {
const data = {
from: {
email: senderEmail,
name: senderName,
},
subject: `Third Eye Health | Support | New Message From ${contactEmail}`,
to: receiverEmail,
html: message,
};
sendgridClient
.send(data)
.then(([response, body]) => {
fulfill(response);
})
.catch((error) => reject(error));
});
}
exports.handler = async function(event) {
const {
SENDGRID_API_KEY,
SENDGRID_SENDER_EMAIL,
SENDGRID_SENDER_NAME,
SENDGRID_RECIPIENT_EMAIL,
} = process.env;
const body = JSON.parse(event.body);
const emailBody = `
Name: ${body.name}
Email: ${body.emailAddress}
Organization: ${body.organization}
Phone Number: ${body.phoneNumber}
Message:
${body.message}
`.replace(/(?:\r\n|\r|\n)/g, '<br />');
client.setApiKey(SENDGRID_API_KEY);
try {
await sendEmail(
client,
emailBody,
SENDGRID_SENDER_EMAIL,
SENDGRID_SENDER_NAME,
SENDGRID_RECIPIENT_EMAIL,
body.emailAddress
);
return {
statusCode: 200,
body: 'success',
};
} catch (err) {
return {
statusCode: 500,
body: 'failure',
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment