3
6 Comments

Sending automatic emails with Nodemailer in EC2

As I was starting to get familiar with AWS and its services (developing a node app in an EC2 instance). I have encountered an interesting hurdle regarding emails.

I set up my domain and email services with "Route53" and "Simple Email Service". Interestingly, when an automated email is sent out with nodemailer, while it works in my local environment it crashes the instance in production - yaaaay! I tried with packages like "aws-sdk", "nodemailer-ses-transport", etc with no luck :)

I have fought out for 2 days and I think it's time to reach out for help :) If you encountered the same issue or have any pointers, please let me know. If you are having the same issues we can cry together :P Thanks!

on May 28, 2020
  1. 2

    A couple of thoughts:

    • Check your zone (as mentioned)
    • Are you sending to the simulator addresses in SES?
    • Did you white list your sending account?

    There are more. Have you gone through the developer guide?
    https://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html

    1. 1

      Yes, all checked :) Thanks for the link, I'll check the extended list :) Really appreciated!

  2. 2

    Crashed an instance? You need to check the logs. SES only allows you to send emails to verified users - you can add a few to test with in the SES console. Eventually, you will need to request quota limit to exit their sandbox -- until you do that, you will not be able to send emails to anyone else other than verified users.

    I e other thing to watch out for is that SES is not available in every zone, so check where you are for availability.

    1. 1

      Thank you so much for your input.

      • I was testing with verified emails. Also tested it after the quota limit was extended.
      • The zone also supports SES as per aws documentation
      • You bring a great point of my problem, which is not been able to see the EC2 logs. I'm a newb here, and don't know how to get them. Tried settings up Watchdog agents with no luck, I checked log in routes /var/logs... but those logs are not useful.
        Any pointers? Thanks a lot for your help :) Really appreciated!
      1. 2

        EC2 logs wont tell you anything -- these are system logs and you should be separately logging from your app. Winston should help here. I did check what I use for my SES lib and its just aws-sdk lib.

        const aws = require('aws-sdk');
        const util = require('util');
        const router = express.Router();
        const logger = require('winston');

        aws.config.update({
        accessKeyId: '##############',
        secretAccessKey: '####################################',
        region: 'us-east-1'
        });
        const ses = new aws.SES({ apiVersion: 'latest' });

        1. 1

          Thank you so much! it's really helpful, I'll try your structure and see if I can make it work.
          BTW - I also discover that my app logs are under "user/.pm2/logs" I'm using pm2 and didn't know it's storing the logs by itself.
          Thanks again!!!!