Nodemailer

Nodemailer - is a package that connects sender mailbox for sending messages (so messages will be saved in Sent folder of the mailbox).

  • npm init -y
  • Add script "start:dev": "nodemon app"
  • npm i nodemailer dotenv
  • npm i nodemon -D
  • Create in root of the proj .prettierrc.json
  • Add .env file in root of project with necessary
    • EMAIL_SENDER=... - mailbox from what messages send;
    • EMAIL_EXTAPP_PASS=... - some post services use additional password for external applications.
      f.e. EMAIL_EXTAPP_PASS is took
      - "gmail" - in Security/How you sign in to Google/2-Step Verification/App passwords
      - "ukr.net" - in Management of IMAP access/Access for external applications
      So configs in node.js will be different, f.e. gmail, ukr.net etc.
  • Before pushing the initial repository to the github remember about adding .gitignore to the project including in the beginning .env

Example 1 - synchro fn send to a one receiver

app.js

See the Pen 123__app.js__nodemailer_from_gmail by Andrii (@imitator) on CodePen.

Example 2 - app.js without any web-servers

Look at async function to many receivers and with another text message (html), also in place of "service" could be used gmail smtp host.

Example 3 - send a message from any CTRL

Example 4 - "approve email after registration" message and verifyCodeCTRL