# Mailcow Complete Email Server

> Full-featured email server with Dovecot, Postfix, SOGo webmail, and rspamd spam filtering.

- Category: Productivity & Collaboration
- Difficulty: advanced
- Components: mailcow, dovecot, postfix, sogo, rspamd, redis, mysql
- Tags: email, mailcow, dovecot, postfix, sogo
- Source: https://docker.recipes/productivity/mailcow-full-stack

## Overview

Mailcow is a complete, dockerized mail server solution that combines the most robust open-source email components into a unified platform. Originally developed as an alternative to complex manual email server setups, mailcow integrates Postfix for SMTP services, Dovecot for IMAP/POP3, SOGo for webmail and groupware functionality, and rspamd for advanced spam filtering and reputation management. This creates a production-ready email infrastructure that rivals commercial solutions while maintaining full control over your data. The architecture leverages Postfix as the mail transfer agent handling incoming and outgoing messages, while Dovecot manages mailbox storage and client connections through IMAP and POP3 protocols. SOGo provides a modern web interface for email, calendar, and contacts with ActiveSync support for mobile devices. Rspamd performs real-time spam analysis using machine learning algorithms, DKIM signing, and reputation scoring, with Redis caching frequently accessed data and MySQL storing user accounts, configurations, and message metadata. This combination addresses the critical challenge of running enterprise-grade email infrastructure without the complexity of manually configuring and maintaining individual components. Small to medium businesses, privacy-conscious organizations, and system administrators benefit from mailcow's integrated approach, which provides advanced features like two-factor authentication, alias management, automatic backup capabilities, and comprehensive logging while eliminating the traditional pain points of email server administration.

## Features

- Rspamd advanced spam filtering with Bayes classification, neural networks, and real-time reputation scoring
- SOGo ActiveSync protocol support for native mobile device integration with push notifications
- Postfix multi-instance architecture with automatic DKIM key generation and DNS record management
- Dovecot full-text search with Solr backend integration and server-side mail filtering via Sieve
- Built-in quarantine management system with user self-service portal for reviewing blocked messages
- Automatic Let's Encrypt certificate provisioning and renewal with DANE TLSA record support
- Real-time monitoring dashboard with mail queue status, spam statistics, and system resource usage
- Multi-domain support with per-domain policies, quotas, and administrator delegation

## Use cases

- Small business email infrastructure replacing expensive hosted solutions like Microsoft 365
- Privacy-focused organizations requiring full control over email data and encryption keys
- Managed service providers offering white-label email hosting to multiple clients
- Educational institutions needing cost-effective email with calendar and contact synchronization
- Development teams requiring dedicated email services for application notifications and user communications
- Remote work environments needing reliable email with mobile device management capabilities
- Compliance-sensitive industries requiring email archiving, audit trails, and data residency control

## Prerequisites

- Minimum 4GB RAM and 20GB storage space for production deployment with multiple domains
- Valid domain name with ability to configure MX, SPF, DKIM, and DMARC DNS records
- Clean IP address reputation (not blacklisted) with reverse DNS properly configured
- SSL certificate or ability to obtain Let's Encrypt certificates for mail.yourdomain.com
- Open firewall ports 25, 80, 110, 143, 443, 465, 587, 993, 995 for full functionality
- Basic understanding of email protocols (SMTP, IMAP, POP3) and DNS record management

## docker-compose.yml

```yaml
services:
  unbound-mailcow:
    image: mailcow/unbound:latest
    volumes:
      - ./data/hooks/unbound:/hooks:Z
      - ./data/conf/unbound/unbound.conf:/etc/unbound/unbound.conf:ro,Z
    networks:
      mailcow-network:
        aliases:
          - unbound

  mysql-mailcow:
    image: mariadb:10.11
    command: --innodb-buffer-pool-size=256M
    volumes:
      - mysql-vol-1:/var/lib/mysql
      - mysql-socket-vol-1:/var/run/mysqld
      - ./data/conf/mysql/:/etc/mysql/conf.d/:ro,Z
    environment:
      MYSQL_ROOT_PASSWORD: ${DBROOT}
      MYSQL_DATABASE: ${DBNAME}
      MYSQL_USER: ${DBUSER}
      MYSQL_PASSWORD: ${DBPASS}
    networks:
      - mailcow-network
    restart: unless-stopped

  redis-mailcow:
    image: redis:alpine
    volumes:
      - redis-vol-1:/data
    networks:
      - mailcow-network
    restart: unless-stopped

  rspamd-mailcow:
    image: mailcow/rspamd:latest
    depends_on:
      - redis-mailcow
    volumes:
      - ./data/conf/rspamd/custom/:/etc/rspamd/custom:Z
      - ./data/conf/rspamd/override.d/:/etc/rspamd/override.d:Z
      - ./data/conf/rspamd/local.d/:/etc/rspamd/local.d:Z
      - rspamd-vol-1:/var/lib/rspamd
    networks:
      - mailcow-network
    restart: unless-stopped

  dovecot-mailcow:
    image: mailcow/dovecot:latest
    depends_on:
      - mysql-mailcow
    volumes:
      - ./data/conf/dovecot:/etc/dovecot:Z
      - ./data/assets/ssl:/etc/ssl/mail:ro,Z
      - vmail-vol-1:/var/vmail
    environment:
      DBNAME: ${DBNAME}
      DBUSER: ${DBUSER}
      DBPASS: ${DBPASS}
    networks:
      - mailcow-network
    restart: unless-stopped

  postfix-mailcow:
    image: mailcow/postfix:latest
    depends_on:
      - mysql-mailcow
    volumes:
      - ./data/conf/postfix:/etc/postfix:Z
      - ./data/assets/ssl:/etc/ssl/mail:ro,Z
      - postfix-vol-1:/var/spool/postfix
    environment:
      DBNAME: ${DBNAME}
      DBUSER: ${DBUSER}
      DBPASS: ${DBPASS}
    ports:
      - "25:25"
      - "465:465"
      - "587:587"
    networks:
      - mailcow-network
    restart: unless-stopped

  sogo-mailcow:
    image: mailcow/sogo:latest
    depends_on:
      - mysql-mailcow
      - redis-mailcow
    environment:
      DBNAME: ${DBNAME}
      DBUSER: ${DBUSER}
      DBPASS: ${DBPASS}
      MAILCOW_HOSTNAME: ${MAILCOW_HOSTNAME}
    networks:
      - mailcow-network
    restart: unless-stopped

  nginx-mailcow:
    image: nginx:alpine
    depends_on:
      - sogo-mailcow
      - rspamd-mailcow
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./data/conf/nginx:/etc/nginx/conf.d:ro,Z
      - ./data/assets/ssl:/etc/ssl/mail:ro,Z
    networks:
      - mailcow-network
    restart: unless-stopped

volumes:
  mysql-vol-1:
  mysql-socket-vol-1:
  redis-vol-1:
  rspamd-vol-1:
  vmail-vol-1:
  postfix-vol-1:

networks:
  mailcow-network:
    driver: bridge
```

## .env

```bash
# Hostname
MAILCOW_HOSTNAME=mail.example.com

# Database
DBNAME=mailcow
DBUSER=mailcow
DBPASS=secure_db_password
DBROOT=secure_root_password

# Timezone
TZ=UTC
```

## Usage notes

- Access admin at https://mail.example.com/admin
- SOGo webmail at https://mail.example.com/SOGo
- Configure DNS records (MX, SPF, DKIM, DMARC)
- Use mailcow generate_config.sh for initial setup

## Troubleshooting

- Database connection errors in dovecot logs: Verify DBPASS environment variable matches MySQL password and mysql-mailcow container is running
- Redis connection timeouts in rspamd: Check redis-vol-1 volume permissions and ensure Redis container has sufficient memory allocation
- DKIM verification failures: Regenerate DKIM keys through mailcow admin interface and update DNS TXT records with new public key
- SOGo calendar sync issues with mobile devices: Verify MAILCOW_HOSTNAME environment variable matches certificate common name and ActiveSync is enabled
- Mail delivery delays or queue buildup: Check postfix logs for DNS resolution issues and verify sending IP reputation with major providers

