Site icon Mailtrap

Set Up SMTP Server

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?

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:

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

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%
  • 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:

    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. 

    For macOS 

    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

    sudo vim /etc/postfix/main.cf
    myhostname = john.example.com
    myorigin = example.com

    If your local username is John, the mail will appear to be from john@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
    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
    sudo postmap /etc/postfix/main.cf

    File with SASL password

    sudo vim /etc/postfix/sasl_passwd
    mail.example.com:25 john@example.com:<your password>
    sudo postmap /etc/postfix/sasl_passwd
    sudo postfix reload

    The local SMTP server is ready. Try it out by sending a test email:

    date | mail -s "Test email" recipient@test.com

    Note: this setup flow is suitable for non-macOS machines that have a regular Postfix daemon.

    For Linux 

    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.

    Installation

    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

    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

    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

    For Windows 

    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

    Installation

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

    Once installed, run hMailServer, and now let’s configure it.

    Configuration

    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!

    Exit mobile version