- Installed
node,npmandgit - Created project folder and
npm initandgit initdone - Installed node modules with
npm install:express, nodemailer, dotenv - Created Google Cloud Platform account: https://console.cloud.google.com/
- 'Open https://console.cloud.google.com/home
- Press New Project

- Insert project name - Location can be left empty - Click "Create"

- Now the project should be selected like that

- Select "APIs & Services" - "OAuth consent screen"

- Select External and click "Create"

- Enter mandatory fields "App name" / "User support mail" / "Developer contact information" - click "Save And Continue"

- Skip the Scopes page with again clicking "Save and Continue"
- Add Test User clicking on "+ Add Users"

- Select "Credentials" form the left menue

- Create Credentials for OAuth client ID

- Select "Application type" - Web application

- Add https://developers.google.com/oauthplayground at "Authorized redirect URIs" with clicking "+ Add URI" and then click "Create"

- You now should see your Client-ID and Client-Secret - we need them later - press "OK"

- Open OAuth 2.0 Playground: https://developers.google.com/oauthplayground/
- Select on the left side "Gmail API v1" and there "https://mail.google.com" and click "Authorize APIs"

- Select your GMail-Account you want to use

- Allow permissions

- Allow again - to work with emails from GMail

- Click on "Exhange authorization code for tokens" to get Refresh Token and Access Token

(its important that you NEVER write such secure informations directly in a code which can be unindentionally uploaded to eg. GitHub - allways use instead dotenv and hidden-files like .env which should never get uploaded anywhere stays only locally)
- Create
.envfile - Include informations from the GMail configuration (Client-ID and Client-Secret from step 14 and Refresh-Token from stemp 20)
OAUTH_CLIENTID=yourClientID
OAUTH_CLIENT_SECRET=yourClientSecret
OAUTH_REFRESH_TOKEN=yourRefreshToken
MAIL_USERNAME=yourGMailAdress
MAIL_PASSWORD=yourGMailPassword
- Create
.gitignorefile (if not exists) and add at least to it
.env
node_modules
- Create new file
app.js - Require necessary modules
const express = require('express')
const nodemailer = require('nodemailer');
require("dotenv").config({ path: "./.env" });
const { gmail } = require('googleapis/build/src/apis/gmail')
- Create running express server
const app = express()
const port = 3000
app.listen(port, () => {
console.log(`nodemailerProject is listening at http://localhost:${port}`)
})
- Define the transporter from nodemail (take all the sensitive information for the .env-variables)
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.MAIL_USERNAME,
pass: process.env.MAIL_PASSWORD,
clientId: process.env.OAUTH_CLIENTID,
clientSecret: process.env.OAUTH_CLIENT_SECRET,
refreshToken: process.env.OAUTH_REFRESH_TOKEN
}
});
- Define mail options
let mailOptions = {
from: process.env.MAIL_USERNAME,
to: "towhomiwillsend@gmail.com",
subject: 'Nodemailer Test Project',
text: 'Hallo from my nodemailer project'
};
- Send mail with error handling and successfull-message
transporter.sendMail(mailOptions, function(err, data) {
if (err) {
console.log("Error " + err);
} else {
### console.log("Email sent successfully");
}
});