This article explored how to use the NodeMailer library to send E-mail the way the API is used locally.

Nodemailer website

The installation

npm install nodemailer -S

Basic implementation

// @doc https://nodemailer.com/ const nodemailer = require("nodemailer"); Async function main () {let from = nodemailer. CreateTransport ({/ / support 465 and 587, and both can port: 587, secure: False, // true for 465, false for other ports // Port: 465, // Secure: true, // { user: "[email protected]", // Email SMTP authorization code, very important, Equivalent to ordinary passwords / / @ question at https://note.youdao.com/ynoteshare1/index.html?id=f9fef46114fb922b45460f4f55d96853&type=note pass: "xxx", }, }); let info = await transporter.sendMail({ from: '"frost" <[email protected]>', // sender address to: "[email protected],[email protected]", // List of receivers subject: "Hello TAB ", // TAB line HTML: "<b>Hello world2?</b>", // html body }); console.log("Message sent: %s", info.messageId); } main().catch(console.error);

Run node index.js directly, go to the mailbox to check the email.

The EMAIL SMTP authorization code must be obtained from the corresponding email Settings.

163 Mailbox – How do I enable the client authorization code?

Add attachments

const path = require("path"); async function main() { let info = await transporter.sendMail({ // ... attachments: [ { filename: "hello.txt", content: "hello world", }, { filename: "network_image.jpg", href: "Https://img0.baidu.com/it/u=3212920999, 1827079299 & FM = 26 & FMT = auto&gp = 0. JPG",}, {filename: "local. TXT", the path: path.resolve(__dirname, "local.txt"), }, ], }); }

The third file is to create a TXT file in the local, found the attachment has been brought.

More and more

HTML templates -Documentation for MJML