简体   繁体   English

如何从 Google AppScript 通过 AWS SES 发送 email

[英]How to send email via AWS SES from Google AppScript

I have a AWS SES User credentials and I want to send an email with an attached pdf via this user in Google AppScript.我有一个 AWS SES 用户凭证,我想通过 Google AppScript 中的这个用户发送一个带有附加 pdf 的 email。 Unfortunately the AWS SDK for nodejs does not work in AppScript which leaves me with two options.不幸的是,用于 nodejs 的 AWS SDK 在 AppScript 中不起作用,这让我有两个选择。

  1. Using the SMTP Interface 使用 SMTP 接口
  2. Make a direct HTTP request using the REST API使用REST API发出直接 HTTP 请求

I could not find a way to send a SMTP request from AppScript which leaves only option 2. However I could not find any code example where this one was achieved.我找不到从 AppScript 发送 SMTP 请求的方法,它只留下选项 2。但是我找不到任何实现此选项的代码示例。 The SendMail Action describes the request but there is not field where I could put in the attachment. SendMail操作描述了请求,但没有可以放入附件的字段。 Does this mean I have to use the RAW type and create a MIME-formatted email to add an attachment?这是否意味着我必须使用 RAW 类型并创建 MIME 格式的 email 才能添加附件?

I also don't understand the needed Parameter, how the signature is created from the SES user I have and so on, do I even need AWS credentials?我也不明白所需的参数,如何从我拥有的 SES 用户创建签名等等,我什至需要 AWS 凭证吗?

I am quite lost, any help is highly appreciated.我很迷茫,非常感谢任何帮助。

Unfortunately there's no easy way to do it.不幸的是,没有简单的方法可以做到这一点。 Two alternatives:两种选择:

1. Create your own AWS library 1. 创建您自己的 AWS 库

To do that, as you mentioned, you need to use the REST API and create some functions to call it.为此,正如您所提到的,您需要使用 REST API 并创建一些函数来调用它。 You can use JavaScript and you can deploy it on your Apsp Script project with clasp您可以使用 JavaScript 并且可以使用 clasp 将其部署在您的Apsp脚本项目中

On this thread , some users have been able to do it with others AWS services在这个线程上,一些用户已经能够使用其他AWS 服务做到这一点

2. Use the Drive API 2.使用驱动API

If you need Apps Script because you need to handle Google Drive files and send it via email.如果您需要 Apps 脚本,因为您需要处理 Google Drive 文件并通过 email 发送。 It'll be better to use the Drive API with Node JS and send your emails from your local environment.最好使用带有Node JSDrive API并从本地环境发送电子邮件。

Here's my way to achieve this:这是我实现这一目标的方法:

  1. AWS \ Privacy AWS \ 隐私
  2. AWS \ Role AWS \ 角色
  3. AWS \ Lambda Function AWS\Lambda Function
  4. AWS \ Add Trigger => API Getaway AWS \ 添加触发器 => API Getaway
  5. AWS \ API Getaway AWS \ API 逍遥游
  6. Apps Script应用脚本

All steps are described here:此处描述了所有步骤:

Medium\send-emails-with-amazon-aws-ses-from-google-scripts Medium\send-emails-with-amazon-aws-ses-from-google-scripts

Sample code for Lambda: Lambda 的示例代码:

var aws = require("aws-sdk");
var ses = new aws.SES({ region: "eu-west-2" });
exports.handler = async function (event) {
  var params = {
    Destination: {
      ToAddresses: [event.email_to],
    },
    Message: {
      Body: {
        Html: { Data: event.body }, 
      },
    Subject: { Data: event.subject },
    },
    Source: event.email_from,
  };
 
  return ses.sendEmail(params).promise()
};

Sample code for Apps-Script: Apps-Script 的示例代码:

function test_send_ses() {
  var options = {
    method: 'post', 
    payload : JSON.stringify({
      // 👉change
      email_to: "YOUREMAIL@gmail.com",
      body: '<p>Hello from <i>Amazon</i>!</p>',
      subject: 'test mail from Amazon',
      // 👉change
      email_from: 'YOUREMAIL@gmail.com'
    }),
    contentType: 'application/json',
    muteHttpExceptions: true
  };
// 👉👉👉change to your url
  var path = 'YOUR_URL_GOES_HERE';
  var result = UrlFetchApp.fetch(path, options)
  Logger.log(result.getResponseCode()); // ➝ 200
  Logger.log(result.getContentText());
}

暂无
暂无

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

相关问题 使用 AppScript 将 Google 电子表格转换为 Excel 并通过电子邮件发送 - Converting Google Spreadsheet to Excel with AppScript and send it via email 使用Google Appscript - 如何使用位于Gmail中的草稿模板中的自定义正文发送自动电子邮件? - With Google Appscript - How to send a automated email using a custom body from a draft template located in Gmail? 如何获取 Google 表单字段值并在 Appscript 中使用它来发送电子邮件 - How to get Google Form field value and use it in Appscript to send an email 如何将数据从谷歌 appscript 发送到 html 页面 - How to send data from google appscript to an html page 如何通过Google Apps脚本通过电子邮件发送Google? - How to send a Google from via email through Google Apps Scripts? 如何将谷歌 Appscript 错误日志发送到 slack - How to send google Appscript Error logs to slack 如果 3 列中的任何日期 = 今天,Google Sheets Appscript 发送电子邮件 - Google Sheets Appscript Send Email if any of the Dates in 3 columns = today Appscript发送Email附件图片 - Appscript Send Email Attachment Image MailApp AppScript:如何使用 MailApp 将 email 发送到线程? - MailApp AppScript: How to send an email to a thread using MailApp? 关于如何从电子表格接收到的数据创建 pdf 并通过 email 发送 pdf 文件的 Google 应用脚本 - Google app script on how to create a pdf from the data received from the spreadsheet and send the pdf file via email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM