<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TymoteuszGluch</title>
    <description>The latest articles on DEV Community by TymoteuszGluch (@tymoteuszgluch).</description>
    <link>https://dev.to/tymoteuszgluch</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3978428%2Fd210fbda-bb2b-46e3-8b33-781d06c73be1.jpg</url>
      <title>DEV Community: TymoteuszGluch</title>
      <link>https://dev.to/tymoteuszgluch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tymoteuszgluch"/>
    <language>en</language>
    <item>
      <title>How I accidentally learned email infrastructure while trying to avoid a SendGrid bill</title>
      <dc:creator>TymoteuszGluch</dc:creator>
      <pubDate>Sun, 21 Jun 2026 16:17:45 +0000</pubDate>
      <link>https://dev.to/tymoteuszgluch/how-i-accidentally-learned-email-infrastructure-while-trying-to-avoid-a-sendgrid-bill-5e6a</link>
      <guid>https://dev.to/tymoteuszgluch/how-i-accidentally-learned-email-infrastructure-while-trying-to-avoid-a-sendgrid-bill-5e6a</guid>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Why am I paying for this when I can host it myself?&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;The hidden subscription fee: your time and your RAM&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;The moment I realized SendGrid wasn't expensive&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;That day, I learned how spammers ruined SMTP for everyone else&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;The day I realized my mail server needed adult supervision&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;The code I didn't plan to write&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you have ever worked on a side project or an open source project, you probably know that feeling. You spend hours trying to reduce costs, comparing providers, looking for ways to save at least one dolar more.&lt;br&gt;
But not becouse you have to, becouse that thought of paying for something, you may probably host yourself for free feel offensive.&lt;/p&gt;

&lt;p&gt;That exacly how i ended with my own self-hosted mail server.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why am I paying for this when I can host it myself?
&lt;/h2&gt;

&lt;p&gt;Before I started working on it, i thought it couldn't be that hard. I probably only need to buy a server to store data, add a simple &lt;em&gt;Nginx&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/em&gt; config, buy a domain, and set up a few common &lt;em&gt;DNS records&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After all, I just wanted to stop paying for an email delivery service and host everything myself.&lt;/p&gt;

&lt;p&gt;Then the world introduced me to &lt;em&gt;SPF&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/em&gt;, &lt;em&gt;DKIM&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;/em&gt;, &lt;em&gt;DMARC&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/em&gt;, and many other strange acronyms that I had never heard of before.&lt;/p&gt;




&lt;h2&gt;
  
  
  The hidden subscription fee: your time and your RAM
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mission: Find place to storage mail server
&lt;/h3&gt;

&lt;p&gt;First of all, I had to find a server as cheap as possible, and there were a few possibilities, like buying a physical server or using a &lt;em&gt;VM provider&lt;sup id="fnref6"&gt;6&lt;/sup&gt;&lt;/em&gt;, which is probably a better option for scaling.&lt;/p&gt;

&lt;p&gt;For example, Oracle has one of the most affordable &lt;em&gt;VM-s&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/em&gt;, but in my experience their technical support is terrible. If you're not going to have any problems, then it might be an option for you.&lt;/p&gt;

&lt;p&gt;In my opinion, the best &lt;em&gt;VM&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/em&gt; provider is Hetzner, which is probably the only option where you don't pay for transfer, and the bill is based on the time your server is running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mission: Find the most afordable, but trusted domain provider
&lt;/h3&gt;

&lt;p&gt;Sounds simple, but then internet introduced me to strange words like &lt;em&gt;SPF&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/em&gt;, &lt;em&gt;DKIM&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;/em&gt; and &lt;em&gt;DMARC&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/em&gt;. I had no idea what those acronyms means, but I quickly found out that I needed all them. Without correct configuration, Mail providers will reject emails coming from a your brand new domain.&lt;/p&gt;

&lt;p&gt;In my case, I chose Cloudflare. Their pricing is reasonable, DNS management is excellent, and they make it relatively easy to configure all the records needed for a mail server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mission: Start to Setup my first Mail Service
&lt;/h3&gt;

&lt;p&gt;At some point, I had to stop reading articles and actually choose a mail server. One of the most popular open-source solutions is &lt;em&gt;Mailcow Dockerized&lt;sup id="fnref8"&gt;8&lt;/sup&gt;&lt;/em&gt;, and for good reason. It comes with a web interface, account management, built-in Nginx, rate limiting, authentication, and most of the things you would expect from a modern mail service and it also has a simple setup script&lt;/p&gt;

&lt;p&gt;Unfortunately, Mailcow is focused on managing email and don't have email templates. In my case, I still needed a separate backend service responsible for storing templates, generating emails, and communicating with the mail server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mission: Add some infrastructure
&lt;/h3&gt;

&lt;p&gt;At this point, I needed more than just a mail server. I also wanted to run my own application responsible for managing email templates and communicating with Mailcow. There was only one problem. Mailcow was already using &lt;em&gt;port 443&lt;sup id="fnref9"&gt;9&lt;/sup&gt;&lt;/em&gt;. As usual, the simple idea turned into a networking problem. To make everything work together, I had to override part of Mailcow's Nginx configuration, move its services behind &lt;em&gt;localhost&lt;sup id="fnref10"&gt;10&lt;/sup&gt;&lt;/em&gt;, and place another Nginx instance in front of everything. The result was one public HTTPS entry point routing traffic to the right service.&lt;/p&gt;

&lt;p&gt;I'm still working on this setup. Right now, it includes the basic infrastructure needed for my application, and I'm experimenting with &lt;em&gt;mTLS&lt;sup id="fnref11"&gt;11&lt;/sup&gt;&lt;/em&gt; and &lt;em&gt;VPN-only&lt;sup id="fnref12"&gt;12&lt;/sup&gt;&lt;/em&gt; access for administrative endpoints. Because apparently running a mail server wasn't enough infrastructure for one project.&lt;/p&gt;




&lt;h2&gt;
  
  
  The moment I realized SendGrid wasn't expensive
&lt;/h2&gt;

&lt;p&gt;When i started this project, i felt all popular mail services are to expensive, then i go to work, and it turned out that the math wasn't on my side.&lt;/p&gt;

&lt;p&gt;Only my Mailcow instance already consumes almost 4 GB of RAM while do nothing. No users, whos need to recive mails, only exitsting. Its work but its doesnt leave place to growth. I'd like to add proper monitoring with &lt;em&gt;Grafana&lt;sup id="fnref13"&gt;13&lt;/sup&gt;&lt;/em&gt;, &lt;em&gt;Prometheus&lt;sup id="fnref14"&gt;14&lt;/sup&gt;&lt;/em&gt; and &lt;em&gt;Loki&lt;sup id="fnref15"&gt;15&lt;/sup&gt;&lt;/em&gt;. Of course, I also need backups, but even now, while using a very cheap VPS, I'm already paying around $10/month. &lt;/p&gt;

&lt;p&gt;Then there is IP reputation. Sometimes a cheap VPS comes with an IP address that has seen things. Things you would rather not know about and if you're unlucky, getting a different IP address may cost extra. Fortunately, I ended up with an IP address that already had a good reputation.&lt;/p&gt;




&lt;h2&gt;
  
  
  That day, I learned how spammers ruined SMTP for everyone else
&lt;/h2&gt;

&lt;p&gt;After setting up my mail server, I thought, I was ready to send my first email. There was only one problem. I couldn't.&lt;/p&gt;

&lt;p&gt;As it turns out, that a lot of VPS providers block ports 25 and 465 by default. At first, I thought it was some annoying limitation. Then I discovered it was the result of years of abuse, spam campaigns, and providers trying to protect their IP reputation.&lt;/p&gt;

&lt;h3&gt;
  
  
  how scammers use cheap &lt;em&gt;VM-s&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/em&gt; to spam everyone
&lt;/h3&gt;

&lt;p&gt;Unfortunately, not only developers are interesting in cheap VPS. Scammers often use stolen credit cards, fake identities and compromised accounts to buy large numbers of servers. They then use those servers to send millions of spam emails, phishing campaigns, and other messages nobody asked for.&lt;/p&gt;

&lt;p&gt;From a VPS provider's perspective, a brand-new mail server is statistically more likely to be a spam machine than an honest developer trying to save a few dollars. Which, unfortunately, explains why my perfectly innocent mail server was treated like a criminal from day one.&lt;/p&gt;

&lt;h3&gt;
  
  
  3 potentialy solution
&lt;/h3&gt;

&lt;p&gt;At this point, I had three options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Use a trusted popular &lt;em&gt;SMTP&lt;sup id="fnref16"&gt;16&lt;/sup&gt;&lt;/em&gt; service for outgoing emails.&lt;/em&gt;&lt;/strong&gt; - Congratulations, you are now paying for the thing, that you where trying  to avoid paying for.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Wait&lt;/em&gt;&lt;/strong&gt; - Many VPS providers become more trusting after a few weeks. This may take approximately 30 days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Prove that you're not a impostor.&lt;/em&gt;&lt;/strong&gt; - configure your DNS records, set up your mail server properly, and send reque for the ports to be unblocked. If everything looks legitimate, some providers will approve the request much sooner.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I went with option three. Mostly because after spending all that time setting up my own mail server, giving up felt emotionally unacceptable.&lt;/p&gt;




&lt;h2&gt;
  
  
  The day I realized my mail server needed adult supervision
&lt;/h2&gt;

&lt;p&gt;Getting a mail server running is only half the battle. The other half is making sure it doesn't suddenly start sending emails on behalf of someone else.&lt;/p&gt;

&lt;p&gt;The first thing I would recommend is monitoring. &lt;em&gt;Grafana&lt;sup id="fnref13"&gt;13&lt;/sup&gt;&lt;/em&gt;, &lt;em&gt;Prometheus&lt;sup id="fnref14"&gt;14&lt;/sup&gt;&lt;/em&gt; and &lt;em&gt;Loki&lt;sup id="fnref15"&gt;15&lt;/sup&gt;&lt;/em&gt; can help you spot unusual activity, such as a sudden spike in outgoing emails at 3 AM. If that happens and it wasn't you, congratulations - you've found a problem before the rest of the Internet does.&lt;/p&gt;

&lt;p&gt;The second thing is protecting your APIs. If someone wants to abuse your mail server, your application's email endpoints are probably the easiest target. A &lt;em&gt;VPN&lt;sup id="fnref12"&gt;12&lt;/sup&gt;&lt;/em&gt; can be a simple solution for internal tools, while mTLS provides stronger protection if you're willing to deal with certificates and the occasional headache they bring.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Mutual TLS&lt;sup id="fnref11"&gt;11&lt;/sup&gt;&lt;/em&gt; can be surprisingly tricky with self-hosted solutions like Mailcow. Updates, custom Nginx configurations, &lt;em&gt;reverse proxies&lt;sup id="fnref17"&gt;17&lt;/sup&gt;&lt;/em&gt; and certificate management all add extra complexity, But that not only problem, becouse thats all can be owerriten if you pull latest Mailcow changes&lt;/p&gt;

&lt;p&gt;Finally, keep an eye on your &lt;em&gt;IP reputation&lt;sup id="fnref18"&gt;18&lt;/sup&gt;&lt;/em&gt;. A perfectly configured mail server with a bad reputation is still a perfectly configured mail server whose emails land in spam.&lt;/p&gt;

&lt;p&gt;And, of course, backups. Because every self-hosting story eventually ends with someone explaining why they should have had backups.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Was it worth it?&lt;/p&gt;

&lt;p&gt;Probably not.&lt;/p&gt;

&lt;p&gt;Was it fun?&lt;/p&gt;

&lt;p&gt;Absolutely.&lt;/p&gt;

&lt;p&gt;Would I do it again?&lt;/p&gt;

&lt;p&gt;Unfortunately, yes.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The code I didn't plan to write
&lt;/h2&gt;

&lt;p&gt;One unexpected side effect of this project was that I ended up writing much more code than I originally planned. What started as "I don't want to pay for SendGrid" eventually turned into a couple of open-source repositories that I'm still actively working on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/TymekGluch/Morphyxis-Infra" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Morphyxis-Infra&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; -  infrastructure and deployment configuration used to run the platform.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/TymekGluch/Morphyxis-mail-service" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Morphyxis-Mail-Service&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; - an experimental service for email template management, rendering and delivery.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither project is production-ready, and both are still evolving as I learn more about email infrastructure, DevOps and self-hosting. But if you're curious how all of these lessons, mistakes and experiments ended up in code, feel free to take a look.&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful articles:
&lt;/h2&gt;

&lt;p&gt;This post is more focused on my experience and doesn't go too deep into the technical details. However, if these topics are important to you, I highly recommend reading the articles below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.spamhaus.org/restrict-port-25" rel="noopener noreferrer"&gt;https://www.spamhaus.org/restrict-port-25&lt;/a&gt; - Wondering why your VPS blocks port 25? This article explains the decades of spam abuse that led to it. Spamhaus is to email security what OWASP is to application security, making this one of the best sources on the topic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.spamhaus.org/resource-hub/ip-reputation/email-compliance-and-reputation-the-inbox-remembers" rel="noopener noreferrer"&gt;https://www.spamhaus.org/resource-hub/ip-reputation/email-compliance-and-reputation-the-inbox-remembers&lt;/a&gt; - So your mail server works? Great. Now comes the part where Gmail decides whether anyone will actually see your emails. This article explains why email reputation matters and why a working &lt;em&gt;SMTP&lt;sup id="fnref16"&gt;16&lt;/sup&gt;&lt;/em&gt; server is only the beginning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cfenollosa.com/blog/after-self-hosting-my-email-for-twenty-three-years-i-have-thrown-in-the-towel-the-oligopoly-has-won.html" rel="noopener noreferrer"&gt;https://cfenollosa.com/blog/after-self-hosting-my-email-for-twenty-three-years-i-have-thrown-in-the-towel-the-oligopoly-has-won.html&lt;/a&gt; - If my article is about accidentally learning email infrastructure, this one is about what happens after spending twenty-three years learning it. A widely shared essay on the realities of self-hosted email in the modern internet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://emailr.dev/en/blog/self-hosted-vs-managed-email" rel="noopener noreferrer"&gt;https://emailr.dev/en/blog/self-hosted-vs-managed-email&lt;/a&gt; - Consider this the email infrastructure equivalent of gym advice from the biggest guy in the room. There aren't many sources or benchmarks, but the authors have clearly accumulated plenty of battle scars, which makes for an interesting perspective.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Footnotes:
&lt;/h2&gt;

&lt;p&gt;Definitions of selected terms used in this article. &lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;em&gt;Nginx - a web server and reverse proxy commonly used to host websites and route traffic to applications.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;em&gt;DNS records - settings that connect a domain name to services such as websites or email servers. For example, an A or CNAME record can point a domain to a server hosting a website.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;em&gt;SPF (Sender Policy Framework) - a DNS record that specifies which servers are allowed to send emails on behalf of a domain.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;em&gt;DKIM (DomainKeys Identified Mail) - a method of digitally signing emails to prove that they were sent by an authorized server and were not modified in transit.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;em&gt;DMARC (Domain based Message Authentication, Reporting, and Conformance) - a policy that tells email providers how to handle messages that fail SPF or DKIM checks.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;em&gt;VM provider - a company that rents virtual servers, allowing you to run applications without buying and maintaining physical hardware f.g. AWS, Hetzner or Oracle cloude&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;&lt;em&gt;VM (Virtual Machine) - a virtual computer that runs inside a physical server and behaves like an independent server&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;&lt;em&gt;Mailcow Dockerized - an open-source mail server suite distributed as Docker containers. It includes a web interface, account management, spam protection, webmail, and other components needed to run a modern mail service.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;&lt;em&gt;Port 443 (HTTPS) - the standard network port used for secure HTTPS connections. Most websites and web applications use this port.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;&lt;em&gt;localhost - a special hostname that refers to the current machine. Services bound to localhost are accessible only from the server itself.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn11"&gt;
&lt;p&gt;&lt;em&gt;mTLS (Mutual Transport Layer Security) - an extension of TLS where both the client and the server authenticate each other using certificates.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn12"&gt;
&lt;p&gt;&lt;em&gt;VPN (Virtual Private Network) - a private network connection that allows users to securely access internal services over the Internet.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn13"&gt;
&lt;p&gt;&lt;em&gt;Grafana - an open-source platform used to visualize metrics, logs, and monitoring data through dashboards.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn14"&gt;
&lt;p&gt;&lt;em&gt;Prometheus - an open-source monitoring system that collects and stores metrics from applications and servers.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn15"&gt;
&lt;p&gt;&lt;em&gt;Loki - an open-source log aggregation system designed to collect, store, and search application and server logs.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn16"&gt;
&lt;p&gt;&lt;em&gt;SMTP (Simple Mail Transfer Protocol) - the standard protocol used to send emails between mail servers and email clients.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn17"&gt;
&lt;p&gt;&lt;em&gt;Reverse proxy - a server that receives incoming requests and forwards them to other services running behind it. Commonly used with Nginx to expose multiple applications under a single public address.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn18"&gt;
&lt;p&gt;&lt;em&gt;IP reputation - a trust score associated with an IP address. Mail providers use it to decide whether emails should be delivered, flagged as spam, or rejected.&lt;/em&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>devops</category>
      <category>opensource</category>
      <category>linux</category>
      <category>infrastructure</category>
    </item>
  </channel>
</rss>
