Hi this is John with this week’s Coding Challenge.
🙏 Thank you for being one of the 83,446 software developers who have subscribed, I’m honoured to have you as a reader. 🎉
If there is a Coding Challenge you’d like to see, please let me know by replying to this email📧
Coding Challenge #90 - SMTP Server
This challenge is to build your own SMTP Server.
SMTP, short for Simple Mail Transfer Protocol is the Internet standard for sending email between mail servers and mail transfer agents. If you’ve ever set up a mail client locally you’ve probably had to provide details of the SMTP server to use.
SMTP was designed in 1981 and was widely used for email soon after. Modern email has become a bit more complicated as layers have been added to protect against spam.
You can read about SMTP and this evolution on the Wikipedia SMTP page.
🚨NEWS: Learn Go By Building Coding Challenges! 🚨
I’m running the Coding Challenges Learn Go By Building Live Course in June.
It is a live course that runs for three working weeks from June the 2nd to the June the 20th. During the course I’ll introduce you to ever aspect of Go that you need to build the following five real-world projects (based off five of my coding challenges):
🏗️ cat - By building cat you learn how to build and run command line programs in Go.
🏗️ sort - By building sort you learn how to use Go's data structures and control flow to implement sort.
🏗️ curl - By building curl you learn how to write network clients in Go.
🏗️ wc - By building wc you learn how to process text data and handle locales with Go.
🏗️ Memcached (Capstone Project) - By building a Memcached server clone you learn how to build efficient network servers in Go.
Having built these five real-world applications you will be well equipped to take on new projects in Go!
If you are a paid subscriber you can get 20% off - please visit the paid subscriber benefits page for the code.
If you are ready to learn Go with me, you can sign up to Coding Challenges Learn Go By Building Live Course here.
If You Enjoy Coding Challenges Here Are Three Ways You Can Help Support It
Refer a friend or colleague to the newsletter. 🙏
Sign up for a paid subscription - think of it as buying me a coffee ☕️ twice a month, with the bonus that you also get 20% off any of my courses.
Buy one of my courses that walk you through a Coding Challenge.
The Challenge - Building An SMTP Server
In this coding challenge we will be building a simple SMTP server. One that an SMTP client can connect to and deliver an email to.
Optionally you might also choose to build a simple client to connect to the SMTP server. I’ve been using Go a lot recently and it’s trivial to build a client in Go (there’s an example one in the documentation for Go’s standard library).
Step Zero
In this introductory step you’re going to set your development environment up ready to begin developing and testing your SMTP server.
I’ll leave you to choose your target platform, setup your editor and programming language of choice. I’d encourage you to pick one that you’re comfortable doing network programming in. We’re building a server after all!
Step 1
In this step your goal is to create a server that listens on port 25 for incoming TCP connections.
When a connection is received the server should respond with a 220 message. See the RFC or the Wikipedia page on SMTP for and example of the protocol in action.
Here’s the key bit from the RFC:
An SMTP session is initiated when a client opens a connection to a server and the server responds with an opening message.
SMTP server implementations MAY include identification of their software and version information in the connection greeting reply after the 220 code
So you could respond with: 220 CC SMTP Server
for example.
Step 2
In this step your goal is to handle the HELO
or EHLO
command from the client. The commands are described in the Client Initiation section of the RFC. HELO
is all you need to support for this coding challenge.
You’ll need to work through the RFC to understand the details of whichever you decide to support.
Step 3
In this step your goal is to handle the header part of the mail transactions. You will need to refer to the Mail Transactions section of the RFC. Add support for the headers:
MAIL FROM:
RCPT TO:
Step 4
In this step your goal is to handle the email body part of the mail transactions. This is the DATA part of the message being sent to the server.
Be sure to read the section on transparency in the RFC to ensure you’re handling command sequences in the email body.
Step 5
In this step your goal is to handle concurrent clients. Be sure to test by creating a client that connects and sends email either slowly or many emails giving you time to run others concurrently.
Going Further
You can this project further by adding support for storing the emails and provide access to them for an email client via POP3 or IMAP. Alternately you can extend the server to support more of SMTP from the relevant RFCs.
Two Other Ways I Can Help You:
I write another newsletter Developing Skills that helps you level up the other skills you need to be a great software developer.
I have a YouTube channel sharing advice on software engineering.
Share Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know via Bluesky or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo
Request for Feedback
I’m writing these challenges to help you develop your skills as a software engineer based on how I’ve approached my own personal learning and development. What works for me, might not be the best way for you - so if you have suggestions for how I can make these challenges more useful to you and others, please get in touch and let me know. All feedback greatly appreciated.
You can reach me on Bluesky, LinkedIn or through SubStack
Thanks and happy coding!
John