20

My structure:

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

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

site/webclient/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);
        }
    });

This code is part of a controller where all other functions within it work perfectly.

  • Is there something I am missing?
  • Perhaps I'm calling for the incorrect nodemailer module?
  • Or should the transport be SMTP, not SES?

I'm stuck.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
O P
  • 2,327
  • 10
  • 40
  • 73
  • You might consider describing what the actual problem is. Do you get error messages, etc. – Sunil D. Jun 01 '14 at 18:12
  • The actual problem is it does not send an email. No error messages. Where should I be seeing it? The console doesn't return anything. – O P Jun 01 '14 at 18:24
  • Are you sure you're passing in the right arguments to the `transport.sendMail()` function? You only pass in one function, but typically you pass in a function to handle success and a separate function to handle errors. Granted, I'm not familiar w/nodemailer, that seems a little funky for it not to have a callback for the success path. – Sunil D. Jun 02 '14 at 04:34
  • please mark as correct answer below – Toolkit May 20 '17 at 05:13

6 Answers6

19

Please use aws-sdk directly. It work for me. Hope it will help you.`

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);
});
Pramod Jaiswal
  • 656
  • 6
  • 7
  • I dont understand this answer. aws-sdk do I have to install it as seperated package or this nodemiler built in? what about AWS.config what they are exactly? what is apiVersion from where I can get it? I have only SMTP credintials – Anas Sep 21 '22 at 11:22
15

The below code is what I used and it worked for me. This is for the current NodeMailer. Where the transport module needs to be included separately. In the previous versions the transport modules were built in.

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

UPDATE

The nodemailer library has been updated since I last wrote this answer. The correct way to use the library with AWS SES is provided in their docs. The link for a working example

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Deepak Puthraya
  • 1,325
  • 2
  • 17
  • 28
9

I use this code in a controller and it worked for me very well. It is using the SMTP auth from AWS SES.

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

Following nodemailer transport configuration worked for me. This doesn't require AWS SDK.

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

Where to obtain configurations:

<aws.smpt.host> can be found in Server Name value of SMTP Settings page of AWS SES console.

<aws_smtp_user> and <aws_smtp_pass> are obtained by creating SMTP credentials with Create My SMTP Credentials button in SMTP Settings page of AWS SES console.

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
0

With new firebase v9 i used following code and it worked:

exports.sendEmail = functions.https.onRequest(async (req, res) => {
  const params = {
    Destination: {
      ToAddresses: ['recipient@example.com'], // Add recipient email addresses
    },
    Message: {
      Body: {
        Text: {
          Data: 'Hello, this is the email content!', // Add your email content here
        },
      },
      Subject: {
        Data: 'Firebase Cloud Function Email', // Add your email subject here
      },
    },
    Source: 'sender@example.com', // Add the sender email address
  };

  try {
    await ses.sendEmail(params).promise();
    res.status(200).send('Email sent successfully');
  } catch (error) {
    console.error('Error sending email:', error);
    res.status(500).send('Error sending email');
  }
});

Full tutorial here

Lonare
  • 3,581
  • 1
  • 41
  • 45
-1

You can do it like this:

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

export default async function mail({ toAdress, subject, text, html = null }) {
  let transporter = nodemailer.createTransport({
    // Credentials for user with SES access in AWS.
    SES: new aws.SES({
      accessKeyId: process.env.AWS_SES_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SES_SECRET_ACCESS_KEY,
    }),
  });
  try {
    await transporter.sendMail({
      from: 'your@email.com',
      to: toAdress,
      subject: subject,
      html: html,
    });
  } catch (error) {
    if (error.response) {
      console.error(error.response.body);
    }
  }
}
user1665355
  • 3,324
  • 8
  • 44
  • 84