In the blog post Best Free SMTP Servers, we touched on the cloud-based solutions for sending emails. These are quite common for all sizes of projects. But what if you don’t want to use the email relay services of Gmail SMTP or its alternatives? In this article, 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?
- Requirements for a local SMTP server
- Quick SMTP server setup
- Is an SMTP server good for testing?
- To wrap up
Do you really need an SMTP server?
Using an outgoing SMTP server is the traditional method of sending emails. An alternative solution is to bypass the SMTP server and send directly to the destination. It’s 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:
- It cannot be used if port 25 is blocked by your ISP.
- It’s unfit for bulk email due to multiple connections to many different servers.
- It cannot send non-personalized emails.
- It can be blocked by email filters because emails are not sent from a regular SMTP server.
Let’s put Direct Send away. What you may be interested in is why you would run your own SMTP server.
Pros and cons of a local SMTP server
Some cloud-based solutions such as Mailgun and SendPulse provide all the advantages above as well. However, all these features are paid, and you still have to rely on a third-party service. Instead, you can set up an SMTP server on your computer using a specific software.
Requirements for a local SMTP server
There are no specific requirements so far. Nevertheless, you should be prepared for the following:
- Some ISPs may block the port 25. You can solve this by contacting your ISP provider and learning about the limitations they may impose. Also, you need to request a correct DNS server and permission for MX record resolution.
- For bulk emails, you should probably opt for a domain and a fixed IP address associated with it – this will increase your resistance to spam filters.
Quick SMTP server setup
Now, let’s do the job. Since you’re going to set up an SMTP server on your computer, it’s crucial to know what OS is used. That’s why we’ll review three ways of how to do this for macOS, Windows, and Linux.
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 the spam folder. ESPs like Gmail, Yahoo, and others are suspicious of new mail transfer agents.
The latest versions of macOS X come pre-installed with Postfix – a default email server. So, all you need to do is tweak the SMTP server configuration as follows:
Postfix config file
- The Postfix main configuration file is the first to work with. Run:
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 firstname.lastname@example.org.
- The relay host is the machine that will accept mails after authentication and relay them to the SMTP server.
- You need to enable SASL with an additional configuration file, where the password is stored.
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/saslpasswd
- This line means that Postfix will only use TLS-encrypted connections.
smtp_tls_security_level = encrypt
- Here is how
main.cflooks in full:
myhostname = john.example.com myorigin = example.com relayhost=mail.example.com:25 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/saslpasswd smtp_tls_security_level = encrypt
- Save the file with:
sudo postmap /etc/postfix/main.cf
File with SASL password
- Create the
sudo vim /etc/postfix/sasl_passwd
- Add the following line:
mail.example.com:25 email@example.com:<your password>
- Save the file with:
sudo postmap /etc/postfix/sasl_passwd
- And restart Postfix:
sudo postfix reload
The local SMTP server is ready. Try it out by sending a test email:
date | mail -s "Test email" firstname.lastname@example.org
Note: this setup flow is suitable for non-macOS machines that have a regular Postfix daemon.
Most Linux distributions are shipped with the two most common SMTP implementations: Sendmail and Postfix. Sendmail has quite a complex design and is less secure; that’s why we picked Postfix again.
If you don’t have Postfix on your machine, install it first. 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.
Now, let’s configure Postfix to process requests to send emails from localhost. For this, you need to tweak the main configuration file
sudo nano /etc/postfix/main.cf
Scroll down to the line
inet_interfaces = all and replace it with
inet_interfaces = loopback-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 with:
sudo systemctl restart postfix
At the end, try to send a test email:
echo "Test email body" | mail -s "Test email subject line" receipient’s_email_address
There are several software options to set up an SMTP server on Windows including MailEnable and Apache James. But we’ve opted for the most popular one, called hMailServer.
Download the latest version here, and install it. At the beginning of the installation, you need to pay attention to the following:
- In the Select Components window, make sure that full installation is chosen. Server and Administrative components must be checked.
- In the Select database server type window, check the built-in database engine.
- In the hMailServer Security window, create a password for the default user.
Once installed, run hMailServer, and now let’s configure it.
- Click Connect on the starting window and enter the password you set up during the installation.
- Click Add domain and enter your domain name in the corresponding field. By the way, you can use a fake domain name since the real one is not required to send emails. But, in this case, make sure that the fake domain does not actually exist. Click Save, and you’ll see your domain created in the left tab.
- Select the Accounts folder in the left tab and click Add… to configure one. All you need to do here is enter a name in the Address field. This will set up your email address. Optionally, you can specify a password. Click Save and head on to the Settings in the left tab.
- Expand the Settings thread and select Protocols. Uncheck POP3 and IMAP. For more on these protocols, read our blog post IMAP vs. POP3 vs. SMTP. Click Save.
- Now, select Advanced in the Settings thread and enter localhost in the Default domain field in the right tab. Click Save. To end up with the Advanced settings, select Auto-ban and uncheck the Enabled box. Click Save.
- In the end, expand the Utilities thread and select Diagnostics. Select your domain to run tests on and click Start. We’re not interested in all the tests, just Collect server details and Test outbound port. These ones should be marked green.
That’s it. Now you can send your test email. This can be done with PowerShell – it’s fast and easy. For more on this, read our blog post Send 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.
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 one hand, it’s not troublesome to install and configure a local SMTP server. However, there is a much better solution.
If you use Mailtrap for testing, you’ll get an advanced tool based on a fake SMTP server. It means that your app will send emails but won’t actually deliver them. Your outgoing mail will end up in the Mailtrap Demo inbox. So, you won’t need to worry about real email recipients and checking the spam folder each time you run tests. No installation is required, at all. All you need to do is update your SMTP settings with Mailtrap credentials. Alternatively, you can copy and paste a ready-to-use code snippet. Different integrations are available for the most common frameworks and programming languages. For more information on other benefits and capabilities, read the Mailtrap Getting Started Guide.
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. Perhaps it is a good option for testing purposes, but Mailtrap, or other email testing tools, will do it better. So, do you actually need a local SMTP server? You are the only one that can answer that. Good luck!