Site icon Mailtrap

Set Up SMTP Server – A Step-By-Step Tutorial 

Set up smtp server

In our blog post about the best free SMTP servers, we discussed cloud-based solutions for sending emails. These are pretty common for all sizes of projects. 

But what if you don’t want to use SMTP relay services provided by Gmail or its alternatives? In this blog post, we’ll teach you how to set up a local SMTP server and get rid of any email-sending dependencies. 

Do you really need an SMTP server?

Using an outgoing SMTP server is the traditional method of sending emails. SMTP server is a computer or an app that sends emails using Simple Mail Transfer Protocol (SMTP).

An alternative solution is to bypass the SMTP server and send emails directly to the destination. This is known as Direct Send and you can use it through specific email software programs. 

Direct Send is not very reliable, though, and here is why:

So, let’s put Direct Send away and figure out why someone would run their own SMTP server.

Pros and cons of a local SMTP server

ProsCons
No daily or hourly limit on the number of outgoing emails
Instant reporting 
Privacy and control of all your outgoing mail
No shared IP
Running a local SMTP server takes time and effort
The bounce rate may increase by 20-30%

To sum up the comparison, here’s the comment we found on Reddit:

Source: Reddit

Cloud-based sending solutions such as Mailtrap Email Sending also provide all the advantages listed above. Such third-party solutions are more secure while still providing complete control over your email infrastructure. 

However, they still have the dependability factor. So, if you want to eliminate dependability, you can of course set up an SMTP server using specific software. 

Requirements for a local SMTP server

There are no specific requirements so far. Nevertheless, you should be prepared for the following:

Quick SMTP server setup

Now, let’s get the job done. Since you’re going to set up an SMTP server on your computer, it’s crucial to keep the operating system or OS in mind. So, we’ll explain how to configure a local SMTP server on macOS, Windows, and Linux machines. 

Important note: If you send test emails to the real email address, you might not find them in the inbox. Don’t forget to check your spam folder. Email clients like Gmail, Yahoo, Outlook, and others are suspicious of new mail transfer agents (MTAs). 

Make sure you back up the Postfix configuration file before making any changes. 

How to set up local SMTP server on macOS

Back in the day, macOS server was a powerful app that people used to manage network services and, you guessed it, set up mail servers. It used Postfix (an MTA) by default. 

Slowly, Apple reduced the features of the macOS Server and, in 2022, discontinued it completely. The app will no longer be updated to match the new versions of macOS. 

The existing versions of the macOS Server are compatible with Monterey and earlier macOS versions. This means that you can still use it for your SMTP server configuration. 

Note: The configuration may differ slightly depending on the OS version you’re using. In the tutorial below, we’ll set up a local SMTP server on macOS Big Sur. For earlier or later versions of macOS, refer to the GitHub repository. 

Postfix should be pre-installed on your machine. If not, you can install it using a third-party package manager such as Homebrew or MacPorts. For Homebrew, run the following command: 

brew install postfix

Then create a new configuration file by using the following piece of code: 

sudo nano /etc/postfix/main.cf

If Postfix is already installed, you can access it by running:

sudo vim /etc/postfix/main.cf

Tell Postfix which name it should use to identify itself to other mail servers. Add the following lines:

myhostname = john.example.com
myorigin = example.com

If your local username is John, the mail will appear to be from john@example.com. 

Now it’s time to set the relay host. It’s the machine that will accept emails after authentication and relay them to the SMTP server.

relayhost = mail.example.com:25

The next step is to add the local network’s IP, define TLS parameters and enable SASL. The latter requires an additional configuration file in which the password is stored. 

Create the sasl_passwd file:

sudo vim /etc/postfix/sasl_passwd

Add the following line:

mail.example.com:25 john@example.com:<your password>

Save the file with:

sudo postmap /etc/postfix/sasl_passwd

Now go back to your main.cf file. 

The complete code sample will look like this: 

myhostname = mailserver.my.home
myorigin = $myhostname

mydestination = $myhostname, mailserver.my.home, localhost.localdomain, localhost
relayhost = mail.example.com:25
mynetworks = 127.0.0.0/8, [::1]/128

smtpd_client_restrictions = permit_mynetworks permit_sasl_authenticated permit
recipient_delimiter = +
tls_random_source = dev:/dev/urandom
smtpd_tls_ciphers = medium

inet_interfaces = all
smtpd_use_tls=yes
smtpd_tls_cert_file=/etc/certs/mailserver.my.home.crt
smtpd_tls_key_file=/etc/ssl/private/mailserver.my.home.key
smtpd_tls_security_level=may

smtp_tls_CApath=/opt/homebrew/etc/ssl/
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated permit_inet_interfaces defer_unauth_destination
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps =  hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

sender_canonical_maps = hash:/etc/postfix/sender_canonical

virtual_maps = hash:/etc/postfix/virtual

home_mailbox = Maildir/

sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map

Now we have to autorun Postfix on boot and restart it. For that, we need to copy Postfix master plist out of the System folder. 

sudo cp /System/Library/LaunchDaemons/com.apple.postfix.master.plist /Library/LaunchDaemons/org.postfix.custom.plist 

Then we need to run: 

sudo vi /Library/LaunchDaemons/org.postfix.custom.plist

Com.apple.postfix.master value should be substituted with org.postfix.custom

Now we should remove these lines to prevent exiting after 60s

<string>-e</string>
<string>60</string>

and add these before </dict>

<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>

It’s time to relaunch the daemon 

sudo launchctl unload /Library/LaunchDaemons/org.postfix.custom.plist
sudo launchctl load /Library/LaunchDaemons/org.postfix.custom.plist

and check if it has started.

sudo launchctl list | grep org.postfix

Then you can check the listen port 25 

sudo lsof -i -P | grep LISTEN 

and check log.

log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info

How to setup SMTP mail server on Linux

Most Linux distributions are shipped with the two most common SMTP implementations: Sendmail and Postfix. Sendmail has quite a complex design and less secure architecture; that’s why we picked Postfix again. 

Installation

If you don’t have Postfix on your machine, you’ll need to install it. Also, you’ll need the mail package Mailutils, which includes programs necessary for Postfix to function:

sudo apt install mailutils postfix

In the Postfix Configuration window, you’ll need to choose the Internet Site mail configuration. Finally, you’ll need to specify the system mail name. It should be the same as the name you assigned to the domain name pointing to your server. Let’s use example.com

Configuration

Now, let’s configure Postfix to process requests to send emails from localhost. For this, you need to tweak the main configuration file main.cf:

sudo nano /etc/postfix/main.cf

Scroll down to the line inet_interfaces = all and replace it with inet_interfaces = loopback-only. This will tell Postfix to accept the incoming emails on localhost only. 

Also, you need to modify mydestination, which specifies the list of domains. It should look like this:

mydestination = $myhostname, localhost.$example.com, $example.com

Save the file and restart Postfix.

sudo systemctl restart postfix

In the end, try to send a test email.

echo "Test email body" | mail -s "Test email subject line" receipient's_email_address

How to set up local SMTP relay server on Windows 

To set up SMTP server on Windows, you have two main options: 

Installation using hMailServer

Download the latest version of hMailServer and install it. At the beginning of the installation, you need to pay attention to the following:

Once installed, it’s time to run and configure hMailServer.

That’s it. Now you can send your test email. This can be done with PowerShell – and it’s fast and easy. For more on this, read our blog post about sending emails from PowerShell. Run the following line (don’t forget to input your data):

Send-MailMessage -To "<recipient's email address>" -From "<your email address>"  -Subject "Your email subject" -Body "Your email body text" -Credential "<your username>" -SmtpServer "127.0.0.1" -Port 25

In the pop-up window, you’ll need to enter your password and then you’ll be good to go. 

Installation using IIS

SMTP relay configuration with IIS requires any version of Windows Server. We’ll break down the general installation process. For details on your specific version, refer to the Microsoft documentation

To install the SMTP server, you’ll need to complete the following steps: 

After the installation, it’s necessary to configure the SMTP Virtual Server. For that, we need IIS 6.0 Manager. 

Once you complete these steps, you should test the settings with Telnet. If you’re able to send emails successfully, then this means that you have a working SMTP email server. 

Is an SMTP server good for testing?

In most cases, people set up local SMTP servers for testing purposes. If you’re developing an app that will send emails, you’ll need to test this function, and a real SMTP server running on your local machine might seem to be a good option. But, is it? 

On the one hand, installing and configuring a local SMTP server is not troublesome. However, there is a much better solution.

Mailtrap is an Email Delivery Platform that covers all your email-related needs. It lets you test, send, and control your email infrastructure through a single platform. 

Instead of lengthy configurations of the local SMTP server, you can use Mailtrap Email Testing – an Email Sandbox with a safe environment to test, inspect, and debug emails. All of this can be done at staging, meaning your app can send an email, but it won’t be delivered to the actual recipient. 

Try Mailtrap Now

Instead, the test email will go to the Sandbox’s inbox. Thus, there are no risks of spamming users. 

From the inbox, it’s possible to validate your email’s HTML/CSS and quickly see spam analysis results that are verified by the Apache SpamAssassin filter. 

The installation process is straightforward. All you have to do is update your SMTP settings with the relevant credentials: 

Alternatively, you can copy and paste a ready-to-use code snippet that appears once the relevant language is selected from the drop-down menu.

When your app goes into production, you can use Mailtrap Email Sending to reach the recipients’ inboxes. It’s an email infrastructure that offers high deliverability rates by design. 

Unlike the local SMTP server, we provide DNS records for SPF, DKIM, and DMARC authentication protocols. We also run security checks for maximum protection and support encrypted connections. 

Once the emails are sent, Email Sending will allow you to access Actionable In-Depth Analytics. 

To wrap up

Running a local mail server can sometimes be a pain. A great deal of knowledge and effort are required to achieve good email deliverability using your own SMTP server. That’s why we recommend choosing an API and SMTP service such as Mailtrap Email Sending

A local SMTP server is perhaps good for testing purposes, but Mailtrap Email Testing, or other email testing tools, will do it better. 

So, do you actually need a local SMTP server? You’re the only one who can answer that. Good luck!

Exit mobile version