Search code examples
node.jsdefaultnodemailer

Nodemailer default options issue


Using Nodemailer v.6.9.4 on Node.js v18.16.0.

Doing a basic send against our hardware appliance works in both cases, but when the "from:" is a default option I get nothing in FROM: (FROM: <>)... our appliance delivers it internally anyway, but it looks funny. Tested against a gmail address and it is never delivered (which would be expected missing a FROM:). When I move it to mailOptions in sendMail, it works as expected and I get a proper FROM: in the email (delivered both internally and to my gmail address). I'm not entirely sure I've set my defaults in the transporter correctly, but I've tried a couple different ways with the same results. The docs give no example on using defaults (that I've found) but suggest this very use case, so I must be doing something wrong.

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    logger: true,
    debug: true,
    host: "my.hardware.appliance",
    port: 25,
    defaults: {
        from: '"SysAdmin" <[email protected]',
    },
})

async function monthlyStorageMetrics() {
    const doMail = await transporter.sendMail({
        //from: '"SysAdmin" <[email protected]>',
        to: "[email protected]",
        subject: "Monthly Metrics",
        text: "Your report",
    })
}

module.exports = {
    monthlyStorageMetrics,
}

Solution

  • Follow my steps to fix your question

    1. Your usage is wrong, missing a matched > symbol.

    change your code from <[email protected] to <[email protected]>

    1. Make sure that you have the right config for your host and port
    • host: pop or imap
    • port: 25 or 994, or other port
    1. Please follow the official API usage of defaults options, if you are not sure how to config.
    let transporter = nodemailer.createTransport(options[, defaults])
    

    https://nodemailer.com/smtp/

    1. If you're still not sure if it's correct, in VS Code Ctrl-click to go to the Typescript method type the definition file, and make sure it's correct.
    export function createTransport(
        transport?: SMTPTransport | SMTPTransport.Options | string,
        defaults?: SMTPTransport.Options,
    ): Transporter<SMTPTransport.SentMessageInfo>;
    // ...
    
    

    The correct default configuration is shown in the code below.

    const nodemailer = require('nodemailer');
    
    const transporter = nodemailer.createTransport({
      // Check both of the host and port is right ✅
      host: "imap.mydomain.com",
      port: 25,
      secure: true,
      auth: {
        user: "[email protected]",
        pass: "sysadmin's password",
      },
    }, {
      // Here is the default message options object ✅
      from: 'SysAdmin <[email protected]>',
    })
    
    async function monthlyStorageMetrics() {
      const doMail = await transporter.sendMail({
        // from: 'SysAdmin <[email protected]>',
        to: 'ManagerJoe <[email protected]>',
        subject: "Monthly Metrics",
        text: "Your report",
      })
    }
    
    // test
    monthlyStorageMetrics().catch(console.error);
    
    // module.exports = {
    //   monthlyStorageMetrics,
    // }
    
    

    screenshot ✅

    enter image description here

    refs

    https://nodemailer.com/