DEV Community

Cover image for How Does an Email Reach Your Inbox? SMTP Explained with Mailtrap
Rijul Rajesh
Rijul Rajesh

Posted on

How Does an Email Reach Your Inbox? SMTP Explained with Mailtrap

Hello, I'm Rijul. I'm building git-lrc, a micro AI code reviewer that runs on every commit. It's free and source-available on GitHub. Star git-lrc to help more developers discover the project. Do give it a try and share your feedback

Suppose you hit Forgot Password on one of your apps while trying to log in.

A few seconds later, bam, an email appears in your inbox.

Sounds simple, right?

But have you ever thought about what actually happens behind the scenes?

In this article, we'll look at how this works and how you can simulate the whole process for free using Mailtrap.

What Happens When You Send an Email?

The flow we mentioned earlier looks roughly like this:

Your Application
       |
       | SMTP
       ↓
Mail Submission / SMTP Server
       |
       | SMTP
       ↓
Recipient's Mail Server
       |
       ↓
Recipient's Mailbox
Enter fullscreen mode Exit fullscreen mode

But what exactly are a mail server and a recipient's mail server?

Usually, you might think: I clicked Forgot Password, some magic happened, and the email reached my inbox.

That "magic" is handled by several components, including the sending mail server and the recipient's mail server.


What Is a Mail Server?

A mail server is a system responsible for handling email.

It can receive, store, process, and forward emails depending on its role.

For example, when your application needs to send a password reset email, it can hand that email off to a mail server.

The mail server then takes care of sending it toward the recipient.

What Is the Recipient's Mail Server?

The recipient also has a mail server responsible for receiving their email.

For example, if you send an email to someone with a Gmail address, Google's mail infrastructure receives the message and eventually makes it available in the user's inbox.

So the basic idea is:

Your Application
       ↓
Your Mail Server
       ↓
Recipient's Mail Server
       ↓
Recipient's Inbox
Enter fullscreen mode Exit fullscreen mode

Where Does SMTP Come In?

You may also see the term SMTP being thrown around.

SMTP stands for Simple Mail Transfer Protocol.

It is the protocol used for sending and relaying email.

Just like we use a language and set of rules to communicate with each other, applications and mail servers use SMTP to communicate when sending email.

So the application doesn't need to figure out all the details of how the email should be delivered.

It can communicate with an SMTP server and hand the message over to it.

What Is an SMTP Server?

An SMTP server is a server that communicates using SMTP. Depending on its role, it can accept submitted mail, relay mail to another server, or receive mail for its own domain.

An application typically needs configuration like this to connect to one:

SMTP Host: smtp.example.com
SMTP Port: 587
Username: abc123
Password: xyz456
Enter fullscreen mode Exit fullscreen mode

The exact values depend on the SMTP provider you're using.


Test It Yourself: Trying Out Mailtrap

Suppose you want to try this out yourself.

Normally, you might have:

Your App
   ↓
SMTP Server
   ↓
Real User's Inbox
Enter fullscreen mode Exit fullscreen mode

But during development, you probably don't want your test emails going to real users.

This is where Mailtrap can be useful.

With Mailtrap's email sandbox, you can have:

Your App
   ↓
Mailtrap SMTP
   ↓
Mailtrap Inbox
Enter fullscreen mode Exit fullscreen mode

Mailtrap captures the emails inside the sandbox instead of delivering them to the actual recipient.

That's extremely useful when you're developing and testing email workflows.

Setting Up Mailtrap to demo SMTP

To get started, go to Mailtrap and create an account.

Then go to the Sandboxes section:

Mailtrap Sandboxes

Click My Sandbox.

You should see the SMTP credentials required to connect your application.

Keep these credentials handy. We'll use them for the demo.

Sending a Test Email Using SMTP Credentials

You can clone the demo project here:

SMTP Demo GitHub Repository

The project contains a .env.example file with the configuration you need:

SMTP_HOST=
SMTP_PORT=
SMTP_USERNAME=
SMTP_PASSWORD=
FROM_EMAIL=
Enter fullscreen mode Exit fullscreen mode

Here:

  • SMTP_HOST: The hostname of the SMTP server
  • SMTP_PORT: The port used by the SMTP server
  • SMTP_USERNAME: Your SMTP username
  • SMTP_PASSWORD: Your SMTP password
  • FROM_EMAIL: The email address the message is sent from

Fill these values using the credentials provided by your Mailtrap sandbox.

Then run the demo application and trigger the email.

python send-email.py
Enter fullscreen mode Exit fullscreen mode

You should see the email appear inside your Mailtrap inbox instead of being delivered to a real recipient.

Wrapping Up

The next time you click Forgot Password and an email appears a few seconds later, there's quite a bit happening behind the scenes.

Your application hands the email to an SMTP server, which can then relay it through the mail system until it reaches the recipient's mail infrastructure.

Whether you're implementing a feature that involves email or simply trying to understand how these seemingly simple things work, SMTP is an important piece of the puzzle.

That's it for this article. See you in the next one.

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

Give it a ⭐ star on Github

Top comments (0)