Menu
User

DropVPS Team

Writer: Cooper Reagan

How to Configure SMTP on Your VPS for Sending Emails

How to Configure SMTP on Your VPS for Sending Emails

Publication Date

01/05/2025

Category

Articles

Reading Time

6 Min

Table of Contents

Setting up an SMTP server on your VPS involves selecting the right software, installing it, and configuring it for secure and reliable email delivery. Popular SMTP server software options include Postfix, Exim, and Sendmail. Among these, Postfix is widely favored for its simplicity, flexibility, and strong community support.

Step 1: Update Your VPS

Before installing any software, ensure your VPS is up-to-date. Use the following commands:

sudo apt update && sudo apt upgrade -y  # For Debian/Ubuntu
sudo yum update -y                     # For CentOS/RHEL

Step 2: Install the SMTP Server

Choose the SMTP software you prefer and install it. Here’s how to install Postfix on a Debian-based system:

sudo apt install postfix -y

During the installation process, you’ll be prompted to configure Postfix. Select Internet Site when asked about the type of mail server configuration. Enter the domain name you want to use for email (e.g., example.com).

Step 3: Configure Postfix for Basic Functionality

After installation, the main configuration file for Postfix is /etc/postfix/main.cf. Open it using a text editor:

sudo nano /etc/postfix/main.cf

Ensure the following basic settings are configured:

  • myhostname: Set it to your domain or subdomain.
    myhostname = mail.example.com
  • mydomain: Specify your domain.
    mydomain = example.com
  • myorigin: Emails appear as if sent from your domain.
    myorigin = $mydomain
  • inet_interfaces: Listen on all interfaces.
    inet_interfaces = all

Save the changes and restart Postfix:

sudo systemctl restart postfix

Step 4: Test the SMTP Server

Use the telnet command or tools like sendmail to test your SMTP server’s functionality:

telnet mail.example.com 25

This will establish a connection to your SMTP server. You can then issue SMTP commands to send a test email or use a mail client for verification.

Once the SMTP server is running, proceed to configure authentication, enable TLS/SSL, and set up DNS records like SPF, DKIM, and DMARC to ensure proper email delivery and security.

Configuring SMTP Authentication

SMTP authentication ensures that only authorized users can send emails through your SMTP server. This setup helps prevent abuse and spamming from your server. Below is a step-by-step guide to configure SMTP authentication.

Step 1: Install Necessary Packages

Ensure that your SMTP server software (e.g., Postfix or Exim) and authentication tools (like Dovecot or SASL) are installed on your VPS. For Postfix and Dovecot, use the following commands:

For Debian/Ubuntu:

sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd -y

For CentOS/RHEL:

sudo yum install postfix dovecot -y

Step 2: Enable Authentication in Postfix

Edit the Postfix configuration file to allow authentication and encrypted communication.

Open the configuration file:

sudo nano /etc/postfix/main.cf

Add or modify the following lines:

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_tls_cert_file = /etc/ssl/certs/your-domain-cert.pem
smtpd_tls_key_file = /etc/ssl/private/your-domain-key.pem
smtpd_tls_auth_only = yes

Save and exit the file.

Step 3: Configure Dovecot for Authentication

Dovecot is commonly used to provide SMTP authentication.

Edit the Dovecot configuration file:

sudo nano /etc/dovecot/conf.d/10-auth.conf

Ensure the following lines are uncommented and set correctly:

disable_plaintext_auth = no
auth_mechanisms = plain login

Next, edit the socket configuration for Postfix:

sudo nano /etc/dovecot/conf.d/10-master.conf

Locate the service auth section and modify it as follows:

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

Locate the service auth section and modify it as follows:

Step 4: Restart Services

Restart Postfix and Dovecot to apply the changes:

sudo systemctl restart postfix
sudo systemctl restart dovecot

Step 5: Test Authentication

You can test SMTP authentication using a tool like Telnet or an email client.

Using Telnet:

telnet your-server-ip 587

Once connected, type:

EHLO your-domain.com
AUTH LOGIN

Enter the Base64-encoded username and password to authenticate.

Step 6: Verify Logs for Authentication Attempts

Check your mail server logs to ensure authentication is working:

sudo tail -f /var/log/mail.log  # Debian/Ubuntu
sudo tail -f /var/log/maillog  # CentOS/RHEL

Look for entries confirming successful authentication. This ensures your SMTP server is securely configured with proper authentication mechanisms.

Prerequisites for Configuring SMTP on a VPS

Before setting up an SMTP server on your VPS, there are several key prerequisites to ensure smooth configuration and functionality. Addressing these requirements early on minimizes errors and guarantees reliable email delivery.

A Registered Domain Name

You need a registered domain name (e.g., example.com) to serve as the identity of your SMTP server. The domain is used to configure DNS records such as SPF, DKIM, and DMARC for email authentication.

A VPS with Root Access

A VPS with administrative/root access is essential for installing and configuring SMTP server software. Ensure your VPS meets the hardware and software requirements of the chosen SMTP server.

Static IP Address

A static IP address for your VPS is crucial for maintaining a stable email delivery system. Many email providers block emails from servers with dynamic or blacklisted IP addresses. Verify that your VPS IP is not listed in any email blacklists.

Fully Qualified Domain Name (FQDN)

Configure your VPS to have an FQDN, such as mail.example.com. This is required for identifying your mail server during communication with other email servers.

Set the hostname of your server with the following command:

sudo hostnamectl set-hostname mail.example.com

Verify the FQDN:

hostname -f

DNS Configuration

Proper DNS records are essential for email delivery and avoiding spam filters:

  • A Record: Maps your domain to the server’s IP address.
  • MX Record: Points to the domain’s mail server.
  • SPF Record: Authorizes your server to send emails on behalf of your domain.
  • DKIM Record: Ensures email authenticity by signing outgoing messages.
  • DMARC Record: Monitors email delivery and protects against spoofing.

Example for adding an SPF record to your DNS:

v=spf1 ip4:YOUR_SERVER_IP -all

Email Client or Testing Tool

To test the SMTP server once it’s configured, you’ll need an email client like Thunderbird or tools like Telnet, Sendmail, or Mailx. These are useful for verifying connectivity and sending test emails.

Firewall and Port Configuration

Ensure the required SMTP ports are open on your VPS:

  • Port 25: For traditional SMTP communication (not always supported by cloud providers).
  • Port 465: For SMTP over SSL.
  • Port 587: For SMTP with STARTTLS encryption.

Check your firewall rules and allow the necessary ports:

sudo ufw allow 25,465,587/tcp

System Updates

Keep your VPS operating system and installed packages updated to avoid compatibility issues or vulnerabilities:

sudo apt update && sudo apt upgrade -y  # Debian/Ubuntu
sudo yum update -y                      # CentOS/RHEL

With these prerequisites met, you can proceed to install and configure an SMTP server on your VPS with confidence.

Linux VPS
U
Loading...

Related Posts