-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendGrid_GoogleScript.gs
More file actions
69 lines (59 loc) · 2.06 KB
/
sendGrid_GoogleScript.gs
File metadata and controls
69 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Function to send email via SendGrid on form submission
function sendEmailOnFormSubmit(e) {
// Your SendGrid API Key
// At the beginning of your script
var SCRIPT_PROPERTIES = PropertiesService.getScriptProperties();
var SENDGRID_API_KEY = SCRIPT_PROPERTIES.getProperty('SENDGRID_API_KEY');
// Extract the submitted data
var response = e.namedValues;
// Get the values
var timestamp = response['Timestamp'][0];
var contractAddress = response['Contract Address'][0];
var ownerAddress = response['Owner Address'][0];
var version = response['Version'][0];
// Compose the email content
var subject = 'New Contract Deployed';
var message = 'A new contract has been deployed.\n\n' +
'Timestamp: ' + timestamp + '\n' +
'Contract Address: ' + contractAddress + '\n' +
'Owner Address: ' + ownerAddress + '\n' +
'Version: ' + version + '\n';
// Recipient email
var recipients = ['insert_Email@blahblahblah.com', 'insert_Email@blahblahblah.com'];
// Sender email
var senderEmail = 'info@savetheworldwithart.io';
var senderName = 'Save The World With Art';
// Prepare the payload for SendGrid API
var payload = {
personalizations: [{
to: recipients.map(email => ({ email })),
subject: subject
}],
from: {
email: senderEmail,
name: senderName
},
content: [{
type: 'text/plain',
value: message
}]
};
// Send the email via SendGrid API
var options = {
method: 'post',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer ' + SENDGRID_API_KEY
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch('https://api.sendgrid.com/v3/mail/send', options);
// Check the response
if (response.getResponseCode() === 202) {
Logger.log('Email sent successfully via SendGrid.');
} else {
Logger.log('Failed to send email via SendGrid. Response code: ' + response.getResponseCode());
Logger.log('Response body: ' + response.getContentText());
}
}