Skip to content

Deploying Sendmail

Introduction

Sendmail is one of the oldest and most widely-used mail transfer agents (MTA) on the Internet. First released in 1983, Sendmail has been a foundational component of email infrastructure, handling the routing and delivery of email messages. While newer alternatives like Postfix and Exim have gained popularity, Sendmail remains a robust choice for organizations requiring a proven, highly configurable email delivery system.

Sendmail’s architecture is built around flexibility and power, with its m4 macro-based configuration system allowing administrators to customize virtually every aspect of mail handling. The software supports multiple authentication methods, encryption, and integrates with various spam filtering and antivirus solutions.

Key highlights of Sendmail:

  • Proven Reliability: Decades of production use worldwide
  • Highly Configurable: Extensive customization through m4 macros
  • SMTP Support: Full implementation of SMTP and ESMTP protocols
  • TLS Encryption: Support for encrypted mail transport
  • Authentication: SMTP AUTH support with various mechanisms
  • Milter Interface: Integration with mail filters for spam and virus scanning
  • Virtual Hosting: Support for multiple domains
  • Queue Management: Sophisticated message queueing and retry logic
  • 100% Open Source: Licensed under Sendmail License

This guide covers deploying Sendmail on Klutch.sh as a relay for application email sending.

Why Deploy Sendmail on Klutch.sh

Deploying Sendmail on Klutch.sh provides several advantages:

Simplified Deployment: Klutch.sh handles container orchestration while you focus on mail configuration.

Persistent Storage: Attach volumes for mail queues and configuration that survive restarts.

Scalable Resources: Allocate appropriate resources based on email volume.

Internal Mail Relay: Use as a relay for other applications deployed on Klutch.sh.

Prerequisites

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

  • A Klutch.sh account
  • A GitHub account with a repository for your Sendmail configuration
  • Basic familiarity with Docker and email systems
  • Understanding of DNS records (SPF, DKIM, DMARC)
  • (Optional) External SMTP relay credentials for production use

Important Considerations

Note: Running a mail server in a containerized environment has limitations:

  • Many cloud providers block port 25 to prevent spam
  • Proper email delivery requires correct DNS configuration (PTR records, SPF, DKIM)
  • Consider using Sendmail as a relay to an external SMTP service

For production email, consider using Sendmail as a relay to services like:

  • Amazon SES
  • SendGrid
  • Mailgun
  • Your organization’s SMTP server

Preparing Your Repository

Create a GitHub repository with your Sendmail configuration.

Repository Structure

sendmail-deploy/
├── Dockerfile
├── sendmail.mc
└── .dockerignore

Creating the Dockerfile

Create a Dockerfile in the root of your repository:

FROM alpine:latest
# Install sendmail and dependencies
RUN apk add --no-cache sendmail sendmail-cf cyrus-sasl cyrus-sasl-plain
# Create required directories
RUN mkdir -p /var/spool/mqueue /var/spool/clientmqueue /etc/mail
# Copy configuration
COPY sendmail.mc /etc/mail/sendmail.mc
# Generate sendmail.cf from sendmail.mc
WORKDIR /etc/mail
RUN m4 /usr/share/sendmail/cf/m4/cf.m4 sendmail.mc > sendmail.cf
# Set permissions
RUN chmod 644 /etc/mail/sendmail.cf && \
chown root:smmsp /var/spool/clientmqueue && \
chmod 770 /var/spool/clientmqueue
# Expose SMTP port
EXPOSE 25 587
# Start sendmail
CMD ["sendmail", "-bd", "-q15m"]

Creating sendmail.mc Configuration

Create a basic sendmail.mc for relay operation:

divert(-1)
divert(0)dnl
VERSIONID(`Klutch.sh Sendmail Configuration')dnl
OSTYPE(linux)dnl
dnl # Define smart host for relaying
define(`SMART_HOST', `[smtp.example.com]')dnl
dnl # Enable SMTP authentication for relay
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
dnl # Authentication settings
define(`confAUTH_MECHANISMS', `PLAIN LOGIN')dnl
FEATURE(`authinfo', `hash -o /etc/mail/authinfo.db')dnl
dnl # TLS settings
define(`confCACERT_PATH', `/etc/ssl/certs')dnl
define(`confCACERT', `/etc/ssl/certs/ca-certificates.crt')dnl
dnl # Accept connections from anywhere (for container networking)
DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl
DAEMON_OPTIONS(`Port=587, Name=MSA, M=Ea')dnl
dnl # Domain masquerading
MASQUERADE_AS(`yourdomain.com')dnl
FEATURE(masquerade_envelope)dnl
dnl # Local delivery
FEATURE(`local_procmail', `/usr/bin/procmail')dnl
MAILER(local)dnl
MAILER(smtp)dnl

Environment Variables Reference

VariableRequiredDefaultDescription
SMART_HOSTNo-Upstream SMTP server for relaying
SMTP_USERNo-Username for SMTP relay authentication
SMTP_PASSWORDNo-Password for SMTP relay authentication
MASQUERADE_DOMAINNo-Domain for email masquerading

Deploying Sendmail on Klutch.sh

    Configure Your SMTP Relay

    If using an external relay, gather your SMTP credentials and server details.

    Push Your Repository to GitHub

    Initialize and push your repository:

    Terminal window
    git init
    git add Dockerfile sendmail.mc .dockerignore
    git commit -m "Initial Sendmail deployment configuration"
    git remote add origin https://github.com/yourusername/sendmail-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 with a descriptive name like “sendmail” or “mail-relay”.

    Create a New App

    Within your project, create a new app. Connect your GitHub account and select your Sendmail repository.

    Configure Traffic

    For internal relay use:

    • Configure the service for internal access from other Klutch.sh apps

    Set Environment Variables

    Configure relay settings:

    VariableValue
    SMART_HOSTYour SMTP relay server
    SMTP_USERRelay authentication username
    SMTP_PASSWORDRelay authentication password

    Attach Persistent Volumes

    Add the following volume:

    Mount PathRecommended SizePurpose
    /var/spool/mqueue5 GBMail queue storage

    Deploy Your Application

    Click Deploy to start the build process.

Using Sendmail as a Relay

Configuring Applications

Point your applications to use Sendmail for outgoing email:

  • SMTP Host: Your Sendmail service internal address
  • SMTP Port: 25 or 587
  • Authentication: As configured (if any)

Testing Email Delivery

Send a test email using the sendmail command:

Terminal window
echo "Subject: Test Email" | sendmail -v recipient@example.com

Queue Management

View the mail queue:

Terminal window
mailq

Force queue processing:

Terminal window
sendmail -q

Troubleshooting

Email Not Delivering

Symptoms: Emails stay in queue indefinitely.

Solutions:

  • Check SMART_HOST configuration
  • Verify relay authentication credentials
  • Review /var/log/maillog for errors
  • Ensure the relay server accepts connections

Authentication Failures

Symptoms: Relay rejects authentication.

Solutions:

  • Verify credentials are correct
  • Check that authentication mechanism matches relay requirements
  • Ensure TLS is properly configured

DNS Issues

Symptoms: Domain lookup failures.

Solutions:

  • Verify container has DNS access
  • Check resolver configuration
  • Test with direct IP addresses

Additional Resources

Conclusion

Deploying Sendmail on Klutch.sh provides a reliable mail relay for your applications. While Sendmail offers extensive customization, using it as a relay to an external SMTP service provides the best balance of reliability and deliverability in a containerized environment.