SMTP with Nodemailer : How to send Email via SMTP with npm Nodemailer


Hey fellow devs❤! Have you tried sending email from nodemailer npm in NodeJs? Well, we're about to learn everything from what smtp server is and what are SMTP server settings to how to set up nodemailer createtransport with nodemailer👊. 

What are we waiting for👀? Let's start!

What is SMTP?

What is smtp definition? Well I would say - think of SMTP server like the postal service for emails! SMTP directs emails from one server to another on the internet, just like a letter goes from sender to receiver via the postal service. It guarantees that your message reaches its destination without a hitch, much like a reliable mail carrier.


But keep in mind that getting and viewing your emails from the server will require you to follow a different procedure, just like opening your inbox to get your letters does!

How SMTP Works?

Email sending begins when your email client meets the server with a "Hello." Then, just like a letter with its destination and contents, it packages up your email and sends it out with all of its data included.

When it comes to verifying addresses and directing your email through the online postal system, the SMTP server functions similarly to a Mail Transfer Agent (MTA). It seems like having your letter sent to several post offices until it gets to the recipient's inbox, which is its ultimate destination!

How Nodemailer and SMTP Connected?

Consider SMTP Protocol with Nodemailer to be the power couple of Node.js email delivery!

With its user-friendly UI, Nodemailer is like your reliable sidekick—it makes sending emails from your Node.js apps easier.

Nodemailer and SMTP setup work together easily to connect your app to the extensive network of email servers. With only a few lines of code, you can send messages far and wide, just like if you had your very own personal courier service. 

Install Nodemailer from NPM


 
  npm install nodemailer


Send an HTML email in NodeJs for smtp gmail

This line of code sends an email with a HTML body.


 
  const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
          user: 'your_email@gmail.com',
          pass: 'your_password'
      }
  });

 
  await transporter.sendMail({
      from: 'your_email@gmail.com',
      to: 'recipient_email@example.com',
      subject: 'Test Email Subject',
      html: '<h1>Example HTML Message Body</h1>'
  });


Send a Plain text email in NodeJs

This line of code does the same function as the last one, except it sends an email with a basic text body instead.



  const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
          user: 'your_email@gmail.com',
          pass: 'your_password'
      }
  });


  await transporter.sendMail({
      from: 'your_email@gmail.com',
      to: 'recipient_email@example.com',
      subject: 'Test Email Subject',
      text: 'Example Plain Text Message Body'
  });


Change SMTP Provider

To switch email providers, just modify the 'smtp host' parameter in the nodemailer.createTransport() method for smtp gmail settings. For example:

 
  const transporterGmail = nodemailer.createTransport({
      host: 'smtp.gmail.com',
  });


Wrap it in a sendEmail helper function

You can add a useful 'sendEmail' helper method to your Node.js application to make email sending easier. This smtp authentication helper function uses the settings set in the 'config.json' file to connect to the SMTP server seamlessly. Having this handy feature makes it easy to send emails from anywhere in your app!


 
  const nodemailer = require('nodemailer');
  const config = require('./config.json');

  module.exports = {
      sendEmail
  };

  async function sendEmail({ from, to, subject, html }) {
      try {
          const transporter = nodemailer.createTransport(config.smtpOptions);
          await transporter.sendMail({ from, to, subject, html });
          console.log('Email sent successfully');
      } catch (error) {
          console.error('Error sending email:', error);
      }
  }


Happy Coding✌

Frequently Asked Questions(FAQs)

Which smtp server to use?

  • Gmail SMTP Server
  • Amazon SES
  • SendGrid
  • SMTP.com

Which smtp port to use?

  • Port 25
  • Port 587
  • Port 465

Why smtp is not working in gmail?

  • Check SMTP settings
  • Enable less secure apps access
  • Enable two-factor authentication
  • Check for Gmail outage
  • Check for antivirus/firewall interference
  • Review Gmail storage quota

Why smtp port 25 is blocked?


ISPs (Internet Service Providers) and network managers frequently restrict SMTP port 25 to avoid spamming and illegal usage of their networks. 

Why smtp not sending emails?

  • Check SMTP settings
  • Verify internet connection
  • Check for SMTP server issues
  • Review email client settings
  • Check for email size limitations
  • Review email account status
  • Check for security software interference
  • Check spam folder

What are smtp settings?

  • SMTP Server Address
  • SMTP Port Number
  • Authentication
  • Encryption
  • Username and Password
  • Sender Email Address

What smtp credentials are?

  • Username 
  • Password

Can smtp be encrypted?


Yes, SMTP can be encrypted to enhance the security of email communication. 

codegirl

Hello, I’m Sangita, person behind "codegirl", a dedicated web developer. Crafting digital experiences is not just my job; it’s my passion. Let’s build something exceptional together!

Post a Comment

Previous Post Next Post