Skip to content

Deploying Postfix

Introduction

Postfix is a free, open-source mail transfer agent (MTA) that routes and delivers electronic mail. Originally written as an alternative to Sendmail, Postfix has become one of the most widely used MTAs on the internet due to its security, performance, and ease of configuration.

Postfix is designed with security in mind, using a modular architecture where different components run with minimal privileges. It handles millions of messages daily on mail servers worldwide and is known for its reliability and straightforward configuration.

Key highlights of Postfix:

  • High Performance: Handles large volumes of mail efficiently with minimal resource usage
  • Security Focused: Modular design with privilege separation and extensive security features
  • Flexible Configuration: Easy-to-understand configuration files with sensible defaults
  • Standards Compliant: Full support for SMTP, TLS encryption, and modern email standards
  • Extensive Documentation: Well-documented with a large community and knowledge base
  • Virtual Domains: Support for multiple domains and virtual mailboxes
  • Content Filtering: Built-in support for spam filtering and content inspection
  • Open Source: Licensed under IBM Public License

This guide walks through deploying Postfix on Klutch.sh using Docker for sending outbound emails.

Why Deploy Postfix on Klutch.sh

Deploying Postfix on Klutch.sh provides several advantages:

Simplified Deployment: Klutch.sh automatically detects your Dockerfile and builds Postfix without complex configuration.

Persistent Storage: Attach persistent volumes for mail queues and configuration, ensuring data survives restarts.

Environment Variable Management: Securely store sensitive SMTP credentials and configuration through Klutch.sh’s environment variable system.

Scalable Resources: Allocate CPU and memory based on your mail volume requirements.

Custom Domains: Configure your own domain for professional email delivery.

Always-On Availability: Your mail server remains operational 24/7 for reliable message delivery.

Prerequisites

Before deploying Postfix on Klutch.sh, ensure you have:

  • A Klutch.sh account
  • A GitHub account with a repository for your Postfix configuration
  • A domain name with DNS access for configuring mail records
  • Basic understanding of email protocols and DNS configuration
  • (Optional) SSL certificates for TLS encryption

Understanding Postfix Architecture

Postfix uses a modular architecture with specialized components:

Master Daemon: The main supervisor process that manages all other Postfix components.

SMTP Server: Receives incoming mail from other servers and clients.

Queue Manager: Manages the mail queue and schedules delivery attempts.

Local Delivery: Delivers mail to local mailboxes.

SMTP Client: Sends outgoing mail to remote servers.

Cleanup Service: Processes incoming mail and adds missing headers.

Preparing Your Repository

Create a GitHub repository containing your Dockerfile and Postfix configuration.

Repository Structure

postfix-deploy/
├── Dockerfile
├── main.cf
├── master.cf
└── .dockerignore

Creating the Dockerfile

Create a Dockerfile in the root of your repository:

FROM alpine:3.19
# Install Postfix and dependencies
RUN apk add --no-cache \
postfix \
postfix-pcre \
ca-certificates \
cyrus-sasl \
cyrus-sasl-plain \
cyrus-sasl-login \
mailx
# Create mail directories
RUN mkdir -p /var/spool/postfix /var/mail
# Copy configuration files
COPY main.cf /etc/postfix/main.cf
COPY master.cf /etc/postfix/master.cf
# Set proper permissions
RUN postfix set-permissions
# Expose SMTP ports
EXPOSE 25 587
# Start Postfix
CMD ["postfix", "start-fg"]

Creating main.cf Configuration

Create a main.cf file with your Postfix configuration:

# Basic Configuration
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost
# Network settings
inet_interfaces = all
inet_protocols = ipv4
# TLS Configuration
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls = yes
smtpd_tls_security_level = may
smtp_tls_security_level = may
# SASL Authentication
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = cyrus
smtpd_sasl_security_options = noanonymous
# Relay restrictions
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
# Queue settings
maximal_queue_lifetime = 1d
bounce_queue_lifetime = 1d
# Message size limit (25MB)
message_size_limit = 26214400
# Mailbox settings
mailbox_size_limit = 0
recipient_delimiter = +

Creating the .dockerignore File

Create a .dockerignore file:

.git
.github
*.md
LICENSE
.gitignore
*.log
.DS_Store

Deploying Postfix on Klutch.sh

    Configure DNS Records

    Before deployment, configure DNS records for your domain:

    Record TypeNameValue
    Amail.example.comYour server IP
    MXexample.commail.example.com (priority 10)
    TXTexample.comv=spf1 a mx ip4:YOUR_IP -all
    TXT_dmarc.example.comv=DMARC1; p=quarantine; rua=mailto:admin@example.com

    Push Your Repository to GitHub

    Initialize your repository and push to GitHub:

    Terminal window
    git init
    git add Dockerfile main.cf master.cf .dockerignore
    git commit -m "Initial Postfix deployment configuration"
    git remote add origin https://github.com/yourusername/postfix-deploy.git
    git push -u origin main

    Create a New Project on Klutch.sh

    Navigate to the Klutch.sh dashboard and create a new project. Give it a descriptive name like “postfix” or “mail-server”.

    Create a New App

    Within your project, create a new app. Connect your GitHub account if you haven’t already, then select the repository containing your Postfix Dockerfile.

    Configure Traffic Settings

    Postfix uses SMTP ports. In the deployment settings:

    • Configure port 25 for SMTP
    • Configure port 587 for submission (authenticated SMTP)

    Set Environment Variables

    Add the following environment variables:

    VariableValue
    MAIL_HOSTNAMEmail.example.com
    MAIL_DOMAINexample.com

    Attach Persistent Volumes

    Add the following volumes:

    Mount PathRecommended SizePurpose
    /var/spool/postfix10 GBMail queue storage
    /var/mail50 GBMailbox storage
    /etc/postfix100 MBConfiguration files

    Deploy Your Application

    Click Deploy to start the build process. Klutch.sh will:

    • Detect your Dockerfile automatically
    • Build the container image
    • Attach the persistent volumes
    • Start the Postfix container

    Verify Deployment

    Test your mail server by sending a test email and checking the logs.

Configuring DKIM Signing

For better email deliverability, configure DKIM signing:

  1. Generate DKIM keys for your domain
  2. Add the public key to your DNS records
  3. Configure OpenDKIM to sign outgoing messages
  4. Test DKIM signing with online verification tools

Security Best Practices

Prevent Open Relay

Ensure your Postfix configuration prevents unauthorized relay:

  • Restrict relay to authenticated users only
  • Use smtpd_relay_restrictions properly
  • Monitor mail logs for abuse attempts

Enable TLS Encryption

Always use TLS for secure mail transmission:

  • Configure valid SSL certificates
  • Enforce TLS for sensitive connections
  • Use smtp_tls_security_level = encrypt for outbound mail

Implement Rate Limiting

Protect against abuse with rate limiting:

  • Configure smtpd_client_message_rate_limit
  • Set smtpd_client_connection_rate_limit
  • Monitor for unusual sending patterns

Monitoring and Logging

Viewing Mail Logs

Monitor Postfix activity through logs:

  • Check /var/log/mail.log for general mail activity
  • Review /var/log/mail.err for errors
  • Use mailq to view the mail queue

Common Log Messages

MessageMeaning
status=sentMessage delivered successfully
status=deferredTemporary failure, will retry
status=bouncedPermanent delivery failure
NOQUEUE: rejectMessage rejected before queuing

Troubleshooting Common Issues

Mail Not Sending

Solutions:

  • Check DNS MX records are configured correctly
  • Verify SPF and DKIM records
  • Review mail logs for rejection reasons
  • Ensure ports 25 and 587 are accessible

Messages Marked as Spam

Solutions:

  • Configure proper SPF, DKIM, and DMARC records
  • Ensure reverse DNS is set correctly
  • Check IP reputation with blacklist tools
  • Warm up new IP addresses gradually

Connection Timeouts

Solutions:

  • Verify firewall allows SMTP ports
  • Check network connectivity
  • Review Postfix timeout settings

Additional Resources

Conclusion

Deploying Postfix on Klutch.sh provides a reliable, high-performance mail transfer agent for your email infrastructure. With proper configuration of DNS records, security settings, and monitoring, Postfix handles email delivery efficiently while maintaining strong security practices.

Whether you’re sending transactional emails, running a small mail server, or building a complete email infrastructure, Postfix on Klutch.sh offers the flexibility and reliability needed for production email systems.