β Step-by-Step: Set up Gmail App Password for Nodemailer
π 1. Enable 2-Step Verification
- Go to https://myaccount.google.com/security
- Scroll to the "Signing in to Google" section.
- Find 2-Step Verification.
- Click it and set it up (if not already enabled).
- You may be asked to verify your phone number.
- Complete the process.
π 2. Generate an App Password
Once 2-Step Verification is enabled, stay on the Security page.
Scroll down to App passwords (under "Signing in to Google").
Youβll only see this option after enabling 2FA.
Click App passwords (You might be asked to re-enter your password).
Under "Select app", choose "Mail".
Under "Select device", choose "Other", and type something like
DocumentStamp App
.Click Generate.
Copy the 16-character app password (example:
jgme rbts kixk lwvd
).
-
This is your
EMAIL_PASS
.
π οΈ 3. Update Your .env
File
In your projectβs .env
file:
EMAIL_USER=yourmail@gmail.com
EMAIL_PASS= # β Your generated app password
Make sure you DO NOT commit this
.env
file to GitHub or share it publicly.
π 4. Use in Nodemailer Code
Example Nodemailer setup (sendEmail.js
):
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
module.exports = async function sendEmail({ to, subject, html }) {
await transporter.sendMail({
from: `"DocumentStamp" <${process.env.EMAIL_USER}>`,
to,
subject,
html,
});
};
π₯ 5. Send Mail to a Specific Email (e.g., demo@gmail.com)
In your controller (submitBookForm
), import and call sendEmail
:
import sendEmail from "../utils/sendEmail.js"; // adjust path
await sendEmail({
to: "demo@gmail.com", // receiver email
subject: "New Book Form Submission",
html: `
<h2>New Submission Received</h2>
<p><strong>Name:</strong> ${name} ${lastName}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong> ${message}</p>
${image_url ? `<p><strong>File:</strong> <a href="https://yourdomain.com${image_url}">Download</a></p>` : ""}
`,
});
β Final Checks
- Make sure
.env
is loaded usingdotenv
:
import dotenv from "dotenv";
dotenv.config();
- Deploy only after
.env
is properly configured. - If you get Gmail auth errors: regenerate the app password.
Top comments (0)