node-msmtp v1.0.2
node-msmtp
A simple msmtp wrapper for node.
Usage
Install
node-msmtp.npm install node-msmtpAlso,
msmtpbinary should exist on the system. You may want to install one with your package manager.apt-get install msmtp # Debian/Ubuntu apk add msmtp # Alpine Linux brew install msmtp # Homebrew on macOSCreate an object out of this library. This will spawn an
msmtpprocess, which waits for your composed email.const Msmtp = require("node-msmtp"); let msmtp = new Msmtp({ smtpHost: "smtp.example.com", smtpPort: 465, security: "tls", username: "yourname@example.com", password: "yourpassword", });To use STARTTLS, start the instance with
security: "starttls", or omit it if you want to use unencrypted SMTP.Compose your mail and send with the
sendmail()method.msmtpprocess will read recipients from the mail's header.let mail = ""; // Mail header starts here. mail += "From: YourName <yourname@example.com>\r\n"; mail += "To: Someone <someone@example.com>\r\n"; mail += "Subject: Greetings\r\n"; mail += "\r\n"; // Mail body starts here. mail += "It's a nice day!"; msmtp.sendMail(mail).then(() => { console.log("Send successfully."); }).catch(e => { console.log("Failed to send."); });msmtpuses exit codes defined bysysexits(3). Exit codes other than0will be thrown as a Javascript error.Typically, you will receive an
EX_NOHOSTif the host is not reachable (either a wrong input or a broken connection is possible), orEX_NOPERMif the username/password combination is not correct.See sysexits for more information.