简体   繁体   English

在 Google 表格中添加一封订阅电子邮件时发送电子邮件

[英]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如果收集列表中的电子邮件,但不向任何人发送电子邮件

Intro介绍

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) (如果您在使用这些附加功能时遇到问题,请确保提出有关它们的问题,而不是尝试在此线程中获得答案)

Example Web App to Update Sheet and Send Mail on POST Request.在 POST 请求时更新工作表和发送邮件的示例 Web 应用程序。

  1. I made a new spreadsheet with only a few headers - "name", "timestamp", and "email" (this is not real data).我制作了一个只有几个标题的新电子表格——“姓名”、“时间戳”和“电子邮件”(这不是真实数据)。 "timestamp" needs to be written exactly the same (no capitals etc). “时间戳”需要完全相同(无大写等)。

样品表

  1. I opened the script editor to start a container bound script (container bound to the spreadsheet).我打开脚本编辑器以启动容器绑定脚本(容器绑定到电子表格)。
  2. I wrote this code:我写了这段代码:
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.
}
  1. Made a 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 请求中使用的电子邮件发送了一封电子邮件。

References参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google 表格脚本 - 将 email 添加到 Google 表格时自动发送带有值的值 - Google Sheet script - Automatically send an email with values when they are added to a Google Sheet 如何从谷歌表格发送 email 表格作为 PDF 附件 - How to send email from google sheet with the sheet as a PDF attachment 发送电子邮件并取消WordPress订阅 - Send an email and cancel subscription on WordPress 想让脚本仅将 email 发送到 Google 电子表格中最后添加的一个 - Want to make the script send email only to the last added one in Google Spreadsheets 将Google工作表转换为html并使用Google App脚本作为电子邮件发送 - Convert google sheet to html and send as email using google app scripts 填充某些列时向某些用户发送自动电子邮件 - 谷歌表 - Send automated email to certain users when certain column has been filled - google sheet 当特定单元格的值更改时,如何使Google表格脚本发送电子邮件? - How do I make a Google Sheet script send an email when a specific cell's value changes? 满足多个列中的多个条件时发送电子邮件-Google表格脚本 - Send email when multiple conditions across multiple columns are met - Google Sheet Script 在 Google 电子表格中向多个地址发送一封电子邮件 - Send one Email to Multiple Addresses in Google Spreadsheet Google表格:获取单元格信息,并在单击时将其全部发送到电子邮件地址 - Google Sheet : get cell informations and send all of it on click to an email address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM