<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Atani blog ]]></title><description><![CDATA[This blog covers technical articles, tutorials, system architecture and advanced programming and computer science concepts. It's an avenue for me to share what i learn along the way and to also help others struggling with understanding various technical concept.]]></description><link>https://blog.atanioyinbunua.com</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 04:20:50 GMT</lastBuildDate><atom:link href="https://blog.atanioyinbunua.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Send Emails for Free Using Nodemailer and Gmail SMTP]]></title><description><![CDATA[Sending emails for marketing campaigns, OTP verification, or customer support can sometimes be expensive. If you're looking for a way to send emails without spending a dime, you're in the right place.]]></description><link>https://blog.atanioyinbunua.com/how-to-send-emails-for-free-using-nodemailer-and-gmail-smtp</link><guid isPermaLink="true">https://blog.atanioyinbunua.com/how-to-send-emails-for-free-using-nodemailer-and-gmail-smtp</guid><dc:creator><![CDATA[Atani Oyinbunua]]></dc:creator><pubDate>Thu, 12 Mar 2026 19:00:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/668dcfff41a19ff2eb4906bb/388147ed-4ac0-4990-ac7e-b277aeb0b1f6.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Sending emails for marketing campaigns, OTP verification, or customer support can sometimes be expensive. If you're looking for a way to send emails without spending a dime, you're in the right place.</p>
<p>In this tutorial, we'll walk through exactly how to set up free email sending using <strong>Nodemailer</strong> as our SMTP client and <strong>Gmail</strong> as our mail provider. That said, the core concepts apply to any SMTP client or email provider of your choice.</p>
<hr />
<h2>Understanding the Basics: Key Email Terminology</h2>
<p>Before we write a single line of code, let's get familiar with a few terms you'll encounter when working with email infrastructure.</p>
<h3>What is SMTP?</h3>
<p><strong>SMTP</strong> stands for <strong>Simple Mail Transfer Protocol</strong>. Think of it as the shared language that mail clients and mail servers use to communicate. Just like two people need a common language to understand each other, a mail client and mail server need SMTP to exchange messages.</p>
<p>We won't go deep into the internals of the protocol, but there are a handful of key concepts worth understanding.</p>
<hr />
<h3>SMTP Host</h3>
<p>The <strong>SMTP host</strong> is the email server responsible for sending, receiving, and relaying outgoing emails between the sender and recipient. For Gmail, this is <code>smtp.gmail.com</code>.</p>
<hr />
<h3>SMTP Port</h3>
<p>The <strong>SMTP port</strong> is the specific entry point on a server where the SMTP protocol operates. Different ports serve different purposes:</p>
<table>
<thead>
<tr>
<th>Port</th>
<th>Protocol</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td><strong>587</strong></td>
<td>STARTTLS</td>
<td>Upgrades a plain-text connection to an encrypted one over the same port. Recommended for most use cases.</td>
</tr>
<tr>
<td><strong>465</strong></td>
<td>SSL/TLS</td>
<td>Sends email over a fully encrypted connection from the start.</td>
</tr>
<tr>
<td><strong>25</strong></td>
<td>None</td>
<td>The original, unencrypted SMTP port.</td>
</tr>
<tr>
<td><strong>2525</strong></td>
<td>STARTTLS</td>
<td>An unofficial backup port, useful when 587 is blocked.</td>
</tr>
</tbody></table>
<hr />
<h3>SMTP User</h3>
<p>Your <strong>SMTP username</strong> is simply your email address (e.g., <code>you@gmail.com</code>). It's used to authenticate your application with the mail server.</p>
<hr />
<h3>SMTP Password</h3>
<p>Your <strong>SMTP password</strong> is used alongside your username for authentication. For Gmail, this won't be your regular account password. We'll cover exactly what to use in the setup section below.</p>
<hr />
<h2>Setting Up Gmail for SMTP Authentication</h2>
<p>Gmail doesn't allow you to authenticate with your regular account password when sending emails programmatically. Instead, you'll need to generate an <strong>App Password</strong>, which is a separate credential specifically for your application.</p>
<h3>Step 1: Enable Two-Factor Authentication</h3>
<p>Before you can create an App Password, 2FA must be active on your Google account.</p>
<img src="https://cdn.hashnode.com/uploads/covers/668dcfff41a19ff2eb4906bb/19d9c5b3-ee59-47f5-a555-afb595ae39bc.png" alt="" style="display:block;margin:0 auto" />

<ol>
<li><p>Go to your <a href="https://myaccount.google.com/security">Google Account Security Settings</a></p>
</li>
<li><p>Under <strong>"How you sign in to Google"</strong>, enable <strong>2-Step Verification</strong></p>
</li>
</ol>
<h3>Step 2: Generate an App Password</h3>
<img src="https://cdn.hashnode.com/uploads/covers/668dcfff41a19ff2eb4906bb/9f5d92be-45bf-4d35-bb75-c5fd60eee8b2.png" alt="" style="display:block;margin:0 auto" />

<ol>
<li><p>Visit <a href="https://myaccount.google.com/apppasswords">Google App Passwords</a></p>
</li>
<li><p>Enter your preferred app name</p>
</li>
<li><p>Click <strong>Create</strong> and <strong>copy the password</strong>. You'll only see it once.</p>
</li>
</ol>
<blockquote>
<p>⚠️ <strong>Keep this password safe.</strong> Store it in your <code>.env</code> file and never commit it to version control.</p>
</blockquote>
<hr />
<h2>Installing Nodemailer</h2>
<p>Install Nodemailer via npm:</p>
<pre><code class="language-plaintext">npm install nodemailer
</code></pre>
<hr />
<h2>Configuring Environment Variables</h2>
<p>Create a <code>.env</code> file in your project root and add the following:</p>
<pre><code class="language-env">SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
</code></pre>
<blockquote>
<p><strong>Note on ports:</strong> Use port <code>465</code> with <code>SMTP_SECURE=true</code> for SSL/TLS. Use port <code>587</code> with <code>SMTP_SECURE=false</code> for STARTTLS.</p>
</blockquote>
<hr />
<h2>Creating the Nodemailer Transporter</h2>
<p>The <strong>transporter</strong> is the core object in Nodemailer. It manages the connection to your email provider and handles sending messages.</p>
<pre><code class="language-javascript">import nodemailer from "nodemailer";

const transporter = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: Number(process.env.SMTP_PORT),
  secure: process.env.SMTP_SECURE === "true", // true for port 465, false for 587
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS,
  },
});
</code></pre>
<hr />
<h2>Sending an Email</h2>
<p>With your transporter configured, sending an email is straightforward:</p>
<pre><code class="language-javascript">await transporter.sendMail({
  from: `"Your Name" &lt;${process.env.SMTP_USER}&gt;`,
  to: "recipient@example.com",
  subject: "Hello from Nodemailer!",
  text: "This is a plain text email.",
  html: "&lt;p&gt;This is an &lt;strong&gt;HTML&lt;/strong&gt; email.&lt;/p&gt;",
});
</code></pre>
<p>That's it. You're now sending emails for free using Gmail's SMTP server.</p>
<hr />
<h2>Putting It All Together</h2>
<p>Here's a complete, reusable <code>sendEmail</code> utility function:</p>
<pre><code class="language-javascript">import nodemailer from "nodemailer";

const transporter = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: Number(process.env.SMTP_PORT),
  secure: process.env.SMTP_SECURE === "true",
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS,
  },
});

export async function sendEmail({ to, subject, text, html }) {
  return await transporter.sendMail({
    from: `"My App" &lt;${process.env.SMTP_USER}&gt;`,
    to,
    subject,
    text,
    html,
  });
}
</code></pre>
<hr />
<h2>Conclusion</h2>
<p>In this guide, we covered:</p>
<ul>
<li><p>The core SMTP concepts you need to know (host, port, credentials)</p>
</li>
<li><p>How to set up Gmail with an App Password for secure programmatic access</p>
</li>
<li><p>How to configure Nodemailer and send your first email</p>
</li>
</ul>
<p>Gmail's free SMTP tier is a great starting point for low-to-medium volume sending. As your application scales, you may want to explore dedicated transactional email services like <strong>Resend</strong>, <strong>Mailgun</strong>, or <strong>SendGrid</strong>. For getting started without spending anything, though, this setup is hard to beat.</p>
<p>Happy sending!</p>
]]></content:encoded></item></channel></rss>