Mail Servers in Linux – Postfix

Mail Servers in Linux – Postfix


Table of Contents

  1. Introduction
  2. What is Postfix?
  3. Prerequisites
  4. Implementation
  5. Conclusion

1. Introduction

In Linux, a mail server allows applications, scripts, and workloads to send email notifications.

For example, when a backup job finishes running, the system can send an email to notify the administrator whether the backup completed successfully or failed.

2. What is Postfix?

Postfix is a Mail Transfer Agent (MTA) that runs on Linux to send and receive emails.

Key features:

  • Security
  • Easy configuration
  • Excellent performance
  • Active community support

3. Prerequisites

  • Linux server (RHEL / AlmaLinux)
  • Internet connectivity (for package installation)
  • A recipient email address

4. Implementation

  1. Check whether Postfix is installed
    rpm -qa | grep postfix
  2. Install Postfix

    Install Postfix and S-nail using DNF.

    dnf install -y postfix s-nail
    • postfix : Handles email delivery on the Linux server.
    • s-nail : Provides the mail command to compose and send emails from the terminal.
  3. Start and Enable Postfix
    systemctl enable --now postfix

    Verify the service:

    systemctl status postfix
  4. Explore Postfix Configuration Files

    Postfix configuration files are stored in:

    cd /etc/postfix/

    Important configuration files:

    File Description
    main.cf Main Postfix configuration
    master.cf Controls Postfix services and processes

    Open the main configuration:

    vi /etc/postfix/main.cf

    SMTP Relay Server:

    A relay server is an SMTP server that forwards outgoing emails to their destinations.

    For example, you can configure Gmail SMTP as a relay server:

    relayhost = [smtp.gmail.com]:587

    This configuration requires additional SMTP authentication and TLS settings. (read step 5)

  5. Configure Gmail SMTP Relay

    To allow Postfix to send emails through Gmail, configure SMTP authentication and TLS.

    1. Install SASL authentication packages
      dnf install -y cyrus-sasl-plain
    2. Create a Gmail App Password
      • Enable 2-Step Verification on your Google account.
      • Generate an App Password from your Google Account security settings.
      • Use the generated App Password instead of your normal Gmail password.
      vvdk fpjl fwqx xnvh
    3. Create SMTP authentication file
      vi /etc/postfix/sasl_passwd

      Add:

      [smtp.gmail.com]:587 tmeytngai@gmail.com:vvdk fpjl fwqx xnvh
    4. Secure and create the authentication database
      chmod 600 /etc/postfix/sasl_passwd
      
      postmap /etc/postfix/sasl_passwd
      
      chmod 600 /etc/postfix/sasl_passwd.db
    5. Configure Postfix Main configuration

      Open the main configuration file:

      vi /etc/postfix/main.cf

      Add the following settings at the bottom of the file:

      # Gmail SMTP Relay
      relayhost = [smtp.gmail.com]:587
      
      # SMTP Authentication
      smtp_sasl_auth_enable = yes
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_sasl_security_options = noanonymous
      
      # TLS Encryption
      smtp_tls_security_level = encrypt
      smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt

      View the last 11 line

      cat /etc/postfix/main.cf | tail -n 11
    6. Restart Postfix
      systemctl restart postfix
      systemctl status postfix
  6. Send an Email

    Use the mail command to send an email

    mail -s "Linux Mail" recipient@gmail.com
    • mail: Email command.
    • s: Specifies the email subject.
    • "Linux Mail": Email subject.
    • recipient@gmail.com: Recipient's email address.

    You can also send an email with a predefined message:

    echo "Testing email from Linux server" | mail -s "Linux Mail" recipient@gmail.com

    In my email inbox, I got this

  7. Important Notes

    SMTP Ports

    Port Purpose
    25 SMTP server-to-server delivery
    465 SMTP over implicit TLS
    587 SMTP submission with STARTTLS
    • Direct email delivery from Postfix to Gmail generally uses port 25.
    • Using Gmail as an SMTP relay generally uses port 587.
  8. Verify Email Delivery

    Check postfix service status

    systemctl status postfix

    Check mail queue:

    postqueue -p

    Check Postfix logs:

    journalctl -u postfix

    On RHEL-based systems, you can also check:

    tail -f /var/log/maillog

5. Conclusion

Postfix provides email delivery capabilities on Linux. It is useful for sending automated notifications from scripts, applications, backup jobs, and system monitoring tasks.