Nodemailer和Amazon SES

时间:2014-06-01 17:04:26

标签: javascript amazon-ses nodemailer

我的结构:

site
-- node_modules
---- nodemailer
-- webclient
---- js
------- controller.js

site/node_modules/nodemailer
site/webclient/js/controller.js

网站/ Web客户端/ JS / controller.js:

    var nodemailer = require('../../node_modules/nodemailer');

    var transport = nodemailer.createTransport('SES', {
        AWSAccessKeyID: 'xxx', // real one in code
        AWSSecretKey: 'xxx', // real one in code
        ServiceUrl: 'email-smtp.us-west-2.amazonaws.com'
    });

    var message = {
        from: 'example@mail.com', // verified in Amazon AWS SES
        to: 'example@mail.com', // verified in Amazon AWS SES
        subject: 'testing',
        text: 'hello',
        html: '<p><b>hello</b></p>' +
              'test'
    };

    transport.sendMail(message, function(error) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ' + response.message);
        }
    });

此代码是控制器的一部分,其中的所有其他功能都可以正常工作。

  • 我有什么遗失的东西吗?
  • 也许我正在呼唤错误的nodemailer模块?
  • 或者传输应该是SMTP,而不是SES?

我被卡住了。

5 个答案:

答案 0 :(得分:5)

您可以直接使用aws-sdk:

let nodemailer = require('nodemailer');
let AWS = require('aws-sdk');

// configure AWS SDK
AWS.config.update({
  accessKeyId: "<SES_ACCESS_KEY>",
  secretAccessKey: "<SES_SECRET_KEY>",
  region: "<SES_REGION>" ,
});

// create Nodemailer SES transporter
let transporter = nodemailer.createTransport({
  SES: new AWS.SES({
    apiVersion: '2010-12-01'
  })
});

// send some mail
transporter.sendMail({
  from: 'sender@example.com',
  to: 'recipient@example.com',
  subject: 'Message',
  text: 'I hope this message gets sent!'
}, (err, info) => {
  console.log(info.envelope);
  console.log(info.messageId);
});

答案 1 :(得分:4)

以下代码是我使用的,它对我有用。这适用于当前的NodeMailer。需要单独包含运输模块的地方。在以前的版本中,传输模块是内置的。 https://andrisreinman.com/nodemailer-v1-0/#migrationguide

var nodemailer = require('nodemailer');
    var sesTransport = require('nodemailer-ses-transport');

    var SESCREDENTIALS = {
      accessKeyId : "accesskey" ,
      secretAccessKey : "secretkey"
    };

    var transporter = nodemailer.createTransport(sesTransport({
        accessKeyId: SESCREDENTIALS.accessKeyId,
        secretAccessKey: SESCREDENTIALS.secretAccessKey,
        rateLimit: 5
    }));



      var mailOptions = {
          from: 'FromName <registeredMail@xxx.com>',
          to: 'registeredMail@xyz.com', // list of receivers
          subject: 'Amazon SES Template TesT', // Subject line
          html: <p>Mail message</p> // html body
      };

      // send mail with defined transport object
      transporter.sendMail(mailOptions, function(error, info){
          if(error){
              console.log(error);
          }else{
              console.log('Message sent: ' + info);
          }
      });

答案 2 :(得分:2)

我在控制器中使用此代码,对我来说很好。它使用的是来自AWS SES的SMTP身份验证。

var nodemailer = require('nodemailer');
var dotenv = require('dotenv').config();

var mailOptions = {
  from: 'example@email.com',
  to: 'other@email.com',
  text: 'This is some text',
  html: '<b>This is some HTML</b>',
};

// Send e-mail using SMTP
mailOptions.subject = 'Nodemailer SMTP transporter';
var smtpTransporter = nodemailer.createTransport({
  port: 465,
  host: process.env.AWS_REGION,
  secure: true,
  auth: {
    user: process.env.AWS_ACCESS_KEY_ID,
    pass: process.env.AWS_SECRET_ACCESS_KEY,
  },
  debug: true
});

smtpTransporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log(error);
    res.status(error.responseCode).send(error.response);
  } else {
    console.log('Message sent: ' + info.response);
    res.status(200).send(info);
  }
});

答案 3 :(得分:1)

以下nodemailer传输配置对我有用。这不需要AWS开发工具包。

const nodeMailerTransporter = require('nodemailer').createTransport({
    host: '<aws.smpt.host>',
    port: 465,
    auth: {
        user: '<aws_smtp_user>',
        pass: '<aws_smtp_pass>'
    }
});

在哪里获取配置:

可以在AWS SES控制台的SMTP设置页面的服务器名称值中找到

<aws.smpt.host>

<aws_smtp_user><aws_smtp_pass>是通过使用AWS SES控制台的SMTP设置页面中的 Create My SMTP Credentials (创建我的SMTP凭据)按钮创建SMTP凭据获得的。

答案 4 :(得分:0)

您可以这样做:

@Valid