Skip to content

Instantly share code, notes, and snippets.

@misterfresh
Last active March 29, 2022 07:26
Show Gist options
  • Save misterfresh/b69d29a97cf415980be2 to your computer and use it in GitHub Desktop.
Save misterfresh/b69d29a97cf415980be2 to your computer and use it in GitHub Desktop.
Forward emails to SendGrid and log IP of senders
// Publish it as a Google App Script Web App to forward email from the contact form to your email using SendGrid
// This is intended for use with https://github.com/misterfresh/react-drive-cms
var sendGridApiKey =
'YOUR-SENDGRID-API-KEY'
var sendGridVerifiedSenderEmail = 'YOUR-SENDGRID-VERIFIED-SENDER-EMAIL'
var sendGridVerifiedSenderName = 'YOUR-SENDGRID-VERIFIED-SENDER-NAME'
var contactEmail = 'YOUR-CONTACT-EMAIL'
var contactName = 'YOUR-CONTACT-NAME'
var visitorsSpreadsheetName = 'Visitors'
// this function will receive the incoming request from the client posting from the form
function doGet(request) {
var formValues = request.parameter
var messageToContact = ''
messageToContact += '<h4>Name: ' + formValues.name + '</h4>'
messageToContact += '<h4>Company: ' + formValues.company + '</h4>'
messageToContact += '<h4>Email: ' + formValues.email + '</h4>'
messageToContact += '<h4>Phone: ' + formValues.phone + '</h4>'
messageToContact += '<h4>Message: <p>' + formValues.message + '</p></h4>'
var response = sendMail(
{
email: contactEmail,
name: contactName,
},
formValues.name + ' inquires about ReactDrive CMS',
messageToContact
)
var result = {
code: response.getResponseCode(),
headers: response.getAllHeaders(),
text: response.getContentText(),
}
var confirmationMessage = ''
confirmationMessage += '<h4>Hello ' + formValues.name + ',</h4>'
confirmationMessage +=
'<h4>Your email has been sent to ' +
contactName +
' at ' +
contactEmail +
' with message:</h4>'
confirmationMessage += '<h4><p>' + formValues.message + '</p></h4>'
confirmationMessage += '<h4>Best regards,</h4>'
confirmationMessage += '<h4>The ReactDrive CMS team</h4>'
var confirmationResponse = sendMail(
{
email: formValues.email,
name: formValues.name,
},
'Thanks for your interest in ReactDrive CMS, ' + formValues.name,
confirmationMessage
)
var logInfo = {
ip: request.parameter.ip,
country: request.parameter.location,
status: result.code,
}
logToSheet(logInfo)
return ContentService.createTextOutput(
request.parameters.callback + '(' + JSON.stringify(result) + ')'
).setMimeType(ContentService.MimeType.JAVASCRIPT)
}
// this function will post to Sendgrid the email information
function sendMail(destination, subject, htmlContent) {
var message = {
personalizations: [
{
to: [destination],
subject: subject,
},
],
content: [
{
type: 'text/html',
value: htmlContent,
},
],
from: {
email: sendGridVerifiedSenderEmail,
name: sendGridVerifiedSenderName,
},
reply_to: {
email: sendGridVerifiedSenderEmail,
name: sendGridVerifiedSenderName,
},
}
var url = 'https://api.sendgrid.com/v3/mail/send'
var options = {
method: 'post',
payload: JSON.stringify(message),
contentType: 'application/json',
headers: {
'content-type': 'application/json',
authorization: 'Bearer ' + sendGridApiKey,
},
}
return UrlFetchApp.fetch(url, options)
}
// log user information to the Visitors spreadsheet
function logToSheet(logInfo) {
var now = new Date()
var strDateTime = [
[
AddZero(now.getDate()),
AddZero(now.getMonth() + 1),
now.getFullYear(),
].join('/'),
[
AddZero(now.getHours()),
AddZero(now.getMinutes()),
AddZero(now.getSeconds()),
].join(':'),
].join(' ')
//Pad given value to the left with "0"
function AddZero(num) {
return num >= 0 && num < 10 ? '0' + num : num + ''
}
var visitorsSheets = DriveApp.getFilesByName(visitorsSpreadsheetName)
if (visitorsSheets.hasNext()) {
var visitorsFile = visitorsSheets.next()
var visitorsSheet = SpreadsheetApp.open(visitorsFile)
} else {
return false
}
var emailsSheet = visitorsSheet.getSheets()[0]
emailsSheet.insertRowsBefore(2, 1)
emailsSheet
.getRange('A2:D2')
.setValues([[logInfo.ip, logInfo.country, logInfo.status, strDateTime]])
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment