Created
February 24, 2023 11:00
-
-
Save mountainash/38f9f1b92a6b1d0384c01168c47794f4 to your computer and use it in GitHub Desktop.
A Node.JS script that can be run from the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* eslint-disable no-console */ | |
import { createTransport } from 'nodemailer'; | |
/* Usage: | |
1. Copy this file to a directory of your choice | |
2. Edit the config object below to match your SMTP server | |
3. `npm install nodemailer` | |
4. Run the script with `node smtp-test.mjs` | |
*/ | |
console.log('Starting smtp-test.mjs'); | |
const config = { | |
host: 'smtp.provider.net', | |
port: 587, | |
sender: 'Scriptkiddies 👻', | |
from: '[email protected]', | |
username: '[email protected]', | |
password: 'XXX', | |
}; | |
const message = { | |
to: '[email protected]', | |
subject: 'Mailer library Mail node.js', | |
text: 'Mail by Mailer library', | |
html: '<span> Hello World Mail sent from mailer library', | |
}; | |
async function main() { | |
// create reusable transporter object using the default SMTP transport | |
let transporter = createTransport({ | |
host: config.host, | |
port: Number(config.port), | |
secure: config.port == 465, // true for 465, false for other ports | |
auth: { | |
user: config.username, | |
pass: config.password, | |
}, | |
}); | |
// send mail with defined transport object | |
let info = await transporter.sendMail({ | |
from: `${config.sender} <${config.from}>`, | |
to: message.to, | |
subject: message.subject, | |
text: message.text, | |
html: message.html, | |
}); | |
console.log('Message sent: %s', info.messageId); | |
// Message sent: <[email protected]> | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment