简体   繁体   English

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

[英]AWS Lambda nodejs email sender

I'm new at the aws and I'm trying to send email using lambda serverless service. 我是aws的新手,正在尝试使用lambda无服务器服务发送电子邮件。 I've found ses email sender function and I deployed it but I'm getting errors. 我找到了ses电子邮件发件人功能,并进行了部署,但出现错误。 This first part of the code (where problem appears): 代码的第一部分(出现问题的地方):

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

This is how my post request looks like: 这是我的帖子请求的样子:

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

and in curl I'm getting this error: 在卷曲我得到这个错误:

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

I've also tried to send it via Postman app but its not working either. 我也尝试过通过邮递员应用程序发送它,但也不起作用。

What am I doing wrong? 我究竟做错了什么?

This is my process-response.js: 这是我的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
  };
};

How about escaping double quote inside your cURL . 如何在cURL escaping double quote Normally, those kinda of error should be encountered because of double quote break your coding. 通常,由于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

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

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