AWS Lambda Node.js电子邮件发件人

时间:2018-12-19 22:54:38

标签: javascript node.js amazon-web-services amazon-ses

我是aws的新手,我正尝试使用lambda无服务器服务发送电子邮件。我找到了ses电子邮件发件人功能,并进行了部署,但出现错误。 代码的第一部分(出现问题的地方):

    const AWS = require('aws-sdk'),
  SES = new AWS.SES(),
  processResponse = require('./process-response.js'),
  FROM_EMAIL = process.env.FROM_EMAIL,
  UTF8CHARSET = 'UTF-8';

exports.handler = (event) => {
  if (event.httpMethod === 'OPTIONS') {
    return Promise.resolve(processResponse(true));
  }

  if (!event.body) {
    return Promise.resolve(processResponse(true, 'Please specify email parameters: toEmails, subject, and message Wywala pierwsze ', 400));
  }
  const emailData = JSON.parse(event.body);

  if (!emailData.toEmails || !emailData.subject || !emailData.message) {
    return Promise.resolve(processResponse(true, 'Please specify email parameters: toEmails, subject and message', 400));
  }

  const destination = {
    ToAddresses: emailData.toEmails
  }

  if (emailData.ccEmails) {
    destination.CcAddresses = emailData.ccEmails;
  }

  const body = (emailData.message && isHTML(emailData.message)) ?
    { Html: { Charset: UTF8CHARSET, Data: emailData.message } } :
    { Text: { Charset: UTF8CHARSET, Data: emailData.message } };

  const emailParams = {
    Destination: destination,
    Message: {
      Body: body,
      Subject: {
        Charset: UTF8CHARSET,
        Data: emailData.subject
      }
    },
    Source: FROM_EMAIL
  };

  if (emailData.replyToEmails && Array.isArray(emailData.replyToEmails)) {
    emailParams.ReplyToAddresses = emailData.replyToEmails;
  }

  return SES.sendEmail(emailParams).promise()
    .then(() => (processResponse(true)))
    .catch(err => {
      console.error(err, err.stack);
      const errorResponse = `Error: Execution update, caused a SES error, please look at your logs.`;
      return processResponse(true, errorResponse, 500);
    });
};

function isHTML(value) {
  value = value.trim();
  return value.startsWith('<') && value.endsWith('>') &&
    (value.includes('<body') || value.includes('<div') || value.includes('<s') || value.includes('<h') || value.includes('<p'));
}
    }

这是我的发帖请求的样子:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"message":"messageexample", "subject":"sbujectexample", "toEmails":"myemail@gmail.com"}' https://mypath.execute-api.eu-west-1.amazonaws.com/Prod/send

并且在卷曲中出现此错误:

curl: (3) Port number ended with 't'
curl: (3) [globbing] unmatched close brace/bracket in column 35
{"message": "Internal server error"}

我也尝试过通过邮递员应用发送它,但是它也不起作用。

我在做什么错了?

这是我的process-response.js:

module.exports = (isCors, body, statusCode) => {
  const status = statusCode || (body ? 200 : 204);
  const headers = { 'Content-Type': 'application/json' };
  if (isCors) {
    Object.assign(headers, {
      'Access-Control-Allow-Headers': 'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token',
      'Access-Control-Allow-Methods': 'OPTIONS,POST',
      'Access-Control-Allow-Origin': process.env.CORS_ORIGIN,
      'Access-Control-Max-Age': '86400'
    });
  }
  return {
    statusCode: status,
    body: JSON.stringify(body) || '',
    headers: headers
  };
};

1 个答案:

答案 0 :(得分:0)

escaping double quote内的cURL怎么样。通常,由于double quote会破坏您的编码,因此应该会遇到这类错误。

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{\"message\":\"messageexample\", \"subjec\t":\"sbujectexample\", \"toEmails\":\"myemail@gmail.com\"}' https://mypath.execute-api.eu-west-1.amazonaws.com/Prod/send