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,
}
>
symbol.change your code from
<[email protected]
to<[email protected]>
host
and port
pop
or imap
25
or 994
, or other portdefaults
options, if you are not sure how to config.let transporter = nodemailer.createTransport(options[, defaults])
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>;
// ...
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 ✅