Nodemailer: Configure Dev Email for development environment

Created on 29 Aug 2017  路  3Comments  路  Source: nodemailer/nodemailer

Hi,

I have several functions that send emails to customers or accounting. But in the development environment, I would like all emails to be sent to my developer email address that I would provide in some config.

What's the nodemailer-way of doing this?

Example:

orderTransporter.sendMail({
                    from: 'Order System <[email protected]>',
                    to: '[email protected]',
                    subject: 'Some Subject',
                    html: 'Some Message'
           });

Now the system should detect if it is running in ENV=development and if that's the case, it should NOT send to [email protected] but to my developer email address.

I know how to do that for each method by manipulation the "to" parameter depending on environment, but I find it cumbersome. Or should I write a function that passes all the stuff to the transporter and replaces the "to" depending on environment?

Thank you!

1. Is this a bug in Nodemailer?

  • [ ] Yes
  • [x] No

Note. Non bug issues are closed by default. Head to Stack Overflow for support questions: https://stackoverflow.com/search?q=nodemailer

2. Is this related to Gmail / Hotmail / Yahoo?

  • [ ] Yes
  • [x] No

    Note. If you can not log in to Gmail then Gmail is blocking you. These issues are closed by default.

3. Is your issue related to SSL/TLS errors?

  • [ ] Yes
  • [x] No

    Note. Most probably you are trying to connect to a server with a self-signed/expired cert or the server supports only ancient ciphers or you are using an antivirus that intercepts your internet connection. Such issues are closed by default.

4. Is your issue related to connection timeouts

  • [ ] Yes
  • [x] No

Note. Most probably you are firewalled out from the server. Such issues are closed by default.

5. Do you get SyntaxError when using Nodemailer?

  • [ ] Yes
  • [x] No

Note. You are using too old version of Node.js, please upgrade. Such issues are closed by default.

6. Nodemailer version you are having problems with (eg. v1.3.7)

^2.6.4

7. Node.js version you are using (run node -v to see it, eg v5.5.0)

5.5.0

8. Your operating system (eg. Windows 10, Ubuntu 14.04 etc.)

MacOS Sierra

Thanks!

Most helpful comment

The correct way would be to use different transport configurations for different environments. A "real" one for production and a test configuration for development. Take a look at Ethereal.email which is built exactly for that - you can send emails through it as you normally would but no email is actually delivered, all are catched by the Ethereal service.

var transporter;
if (process.env.NODE_ENV === 'production' ){
    // all emails are delivered to destination
    transporter = nodemailer.createTransport({
        host: 'smtp.sendgrid.net',
        port: 587,
        auth: {
            user: 'real.user',
            user: 'verysecret'
        }
    });
} else {
    // all emails are catched by ethereal.email
    transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        auth: {
            user: '[email protected]',
            user: 'verysecret'
        }
    });
}
transporter.sendMail(...)

All 3 comments

Hey @molerat619

I met such issue in the past, that's why I created a plugin to intercept such emails. Take a look https://github.com/killmenot/nodemailer-trap-plugin

The correct way would be to use different transport configurations for different environments. A "real" one for production and a test configuration for development. Take a look at Ethereal.email which is built exactly for that - you can send emails through it as you normally would but no email is actually delivered, all are catched by the Ethereal service.

var transporter;
if (process.env.NODE_ENV === 'production' ){
    // all emails are delivered to destination
    transporter = nodemailer.createTransport({
        host: 'smtp.sendgrid.net',
        port: 587,
        auth: {
            user: 'real.user',
            user: 'verysecret'
        }
    });
} else {
    // all emails are catched by ethereal.email
    transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        auth: {
            user: '[email protected]',
            user: 'verysecret'
        }
    });
}
transporter.sendMail(...)

Thanks for your help. I have decided to go with the first version and I have written a small interceptor myself (was good to learn).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pranavpunjabi picture pranavpunjabi  路  3Comments

Nedudi picture Nedudi  路  5Comments

RobertoDieguez picture RobertoDieguez  路  4Comments

uiteoi picture uiteoi  路  5Comments

ghost picture ghost  路  3Comments