[英]Send email when one subscription email is added on Google sheet
I have a code that collect emails from my website when someone clicks subscribe to a google sheet.我有一个代码,当有人点击订阅谷歌表格时,它会从我的网站收集电子邮件。 The process is based on these steps here: https://github.com/jamiewilson/form-to-google-sheets/blob/master/README.md However I want to send also an email to the person who subscribed to my website through the google script but I am not able to do so.该过程基于以下步骤: https : //github.com/jamiewilson/form-to-google-sheets/blob/master/README.md但是我还想向订阅我网站的人发送电子邮件通过谷歌脚本,但我无法这样做。 Here is the code:这是代码:
function doPost (e) {
var lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
var sheet = doc.getSheetByName(sheetName)
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'timestamp' ? new Date() : e.parameter[header]
})
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
var row = sheet.getLastRow()-1
var column = sheet.getLastColumn()
var dataRange = sheet.getRange(2, 1,row,column)
var data = dataRange.getValues();
var email = data[row-1][1];
MailApp.sendEmail("man@test.com", "Wellcome To Engineers", email);
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
if collects the emails on the list but it does not send emails to anyone如果收集列表中的电子邮件,但不向任何人发送电子邮件
There are various parts to your script, but I believe the central question you are asking is about getting a doPost()
function to send emails, so here is a snippet of an quick example I made that will work in Apps Script.您的脚本有各个部分,但我相信您要问的中心问题是关于获取doPost()
函数来发送电子邮件,所以这里是我制作的一个快速示例的片段,该示例将在 Apps 脚本中运行。 It doesn't use any of the extras such as the Lock Service, or try catch blocks, and it doesn't return anything, but it does update the spreadsheet and it does send an email.它不使用任何附加功能,例如锁定服务,也不使用 try catch 块,也不返回任何内容,但它会更新电子表格并发送电子邮件。
Adapt this to your needs first, then see about adding in the Lock Service, Try and Catch Blocks and Return messages.首先根据您的需要调整它,然后查看有关添加锁定服务、尝试和捕获块和返回消息的信息。
(if you are having trouble with these extras make sure to ask new questions about them and not try to get an answer in this thread) (如果您在使用这些附加功能时遇到问题,请确保提出有关它们的新问题,而不是尝试在此线程中获得答案)
function doPost (e) {
var doc = SpreadsheetApp.getActiveSpreadsheet() // works if container bound
var sheet = doc.getSheetByName("Sheet1")
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'timestamp' ? new Date() : e.parameter[header]
})
var newRange = sheet.getRange(nextRow, 1, 1, newRow.length)
newRange.setValues([newRow])
MailApp.sendEmail(e.parameter["email"], "Wellcome To Engineers", "Welcome"); // you seemed to have the order of the parameters wrong.
}
POST
request with curl from the command line to test it:从命令行使用 curl 发出POST
请求来测试它:curl -X POST -F 'name={{name}}' -F 'email={{email}}' https://script.google.com/macros/s/{{script id}}/exec
This updated the spreadsheet and sent an email to the email I used in the POST request.这更新了电子表格并向我在 POST 请求中使用的电子邮件发送了一封电子邮件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.