«   2019/08   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
54
Total
1,222,648
안정적인 DNS서비스 DNSEver
관리 메뉴

Blue Breeze

AWS SES(Amazone Simple Email Service) 본문

AWS

AWS SES(Amazone Simple Email Service)

푸른바람 C/H 2019.04.12 17:26

AWS SES

SES 설정

code

npm i aws-sdk
npm i express-validator
npm i express
npm i joi
npm i log4js
# mail.validation.js
module.exports = {
  to: {
    exists: true
  },
  cc: {
    exists: false
  },
  bcc: {
    exists: false
  },
  subject: {
    exists: true
  },
  text: {
    exists: true
  }
};
const express = require('express');
const router = express.Router();

const { checkSchema, validationResult } = require('express-validator/check');
const schema = require('./mail.validation');
const logger = require('../../modules/logger');

const AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname+'/config.json');

const charset = "UTF-8";

var send = function(req, res, next){
  try{
    var ses = new aws.SES(); // Create a new SES object. 
    var params = { // Specify the parameters to pass to the API.
      Source: req.body.form||req.query.from||'INFO <info@domain.com>', 
      Destination: { 
        ToAddresses: [
          req.body.to||req.query.to||null
        ],
      },
      Message: {
        Subject: {
          Data: req.body.subject||req.query.subject||null,
          Charset: charset
        },
        Body: {
          Text: {
            Data: req.body.text||req.query.text||null,
            Charset: charset 
          },
          Html: {
            Data: req.body.html||req.query.html||null,
            Charset: charset
          }
        }
      },
      ConfigurationSetName: 'ConfigSet'
    };

    ses.sendEmail(params, function(err, data) { //Try to send the email.
      if(err) { // If something goes wrong, print an error message.
        var result = {ok:false, params: params, errors: err};
        logger.email.error(result);
        return res.status(422).json(result);
      }

      var result = {ok:false, params: params, data: data};
      logger.mail.info(result);
      return res.status(200).json(result);
    });
  }catch(e){
    var result = {ok:false, params: params, errors:  err.array?err.array():err||null};
    logger.mail.error(result);
    return res.status(422).json(result);
  }
};


router.get('/', checkSchema(schema), send);
router.post('/', checkSchema(schema), send);

module.exports = router;
0 Comments
댓글쓰기 폼