简体   繁体   中英

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. Unfortunately the AWS SDK for nodejs does not work in AppScript which leaves me with two options.

  1. Using the SMTP Interface
  2. Make a direct HTTP request using the REST API

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. The SendMail Action describes the request but there is not field where I could put in the attachment. Does this mean I have to use the RAW type and create a MIME-formatted email to add an attachment?

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?

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

To do that, as you mentioned, you need to use the REST API and create some functions to call it. You can use JavaScript and you can deploy it on your Apsp Script project with clasp

On this thread , some users have been able to do it with others AWS services

2. Use the Drive API

If you need Apps Script because you need to handle Google Drive files and send it via email. It'll be better to use the Drive API with Node JS and send your emails from your local environment.

Here's my way to achieve this:

  1. AWS \ Privacy
  2. AWS \ Role
  3. AWS \ Lambda Function
  4. AWS \ Add Trigger => API Getaway
  5. AWS \ API Getaway
  6. Apps Script

All steps are described here:

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

Sample code for 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:

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM