keys-for-all/scripts/setup-mail-server.sh

178 lines
5.1 KiB
Bash
Raw Permalink Normal View History

2025-07-22 18:27:21 -07:00
#!/bin/bash
# Mail Server Setup Script for Keys For All
# This script sets up the mail server environment using Docker
set -e
echo "🚀 Setting up Mail Server for Keys For All..."
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo "❌ Docker is not installed. Please install Docker first."
exit 1
fi
# Check if Docker Compose is installed
if ! command -v docker-compose &> /dev/null; then
echo "❌ Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
# Create necessary directories
echo "📁 Creating mail server directories..."
mkdir -p docker-data/mail-data
mkdir -p docker-data/mail-state
mkdir -p docker-data/mail-logs
mkdir -p docker-data/mailhog
mkdir -p docker-data/redis
mkdir -p config/mailserver
# Get domain from user or use default
if [ -z "$MAIL_DOMAIN" ]; then
read -p "Enter your mail domain (e.g., example.com): " MAIL_DOMAIN
MAIL_DOMAIN=${MAIL_DOMAIN:-localhost}
fi
# Copy mailserver environment file if it doesn't exist
if [ ! -f .env.mailserver ]; then
if [ -f .env.mailserver.example ]; then
echo "📋 Creating mailserver environment file..."
sed "s/your-domain.com/$MAIL_DOMAIN/g" .env.mailserver.example > .env.mailserver
echo "✅ Created .env.mailserver with domain: $MAIL_DOMAIN"
else
echo "⚠️ .env.mailserver.example not found. Creating a basic one..."
cat > .env.mailserver << EOF
OVERRIDE_HOSTNAME=mail.$MAIL_DOMAIN
DOMAINNAME=$MAIL_DOMAIN
CONTAINER_NAME=keys-mailserver
ENABLE_LDAP=0
ENABLE_SASLAUTHD=0
SMTP_ONLY=0
LOG_LEVEL=info
DMS_DEBUG=0
EOF
fi
fi
# Function to create mail accounts
create_mail_account() {
local email=$1
local password=$2
echo "Creating mail account: $email"
docker exec -it keys-mailserver setup email add "$email" "$password" 2>/dev/null || true
}
# Start the mail servers
echo "🔧 Starting mail servers..."
docker-compose -f docker-compose.mail.yml up -d
# Wait for mail server to be ready
echo "⏳ Waiting for mail server to initialize..."
sleep 30
# Generate passwords
NOREPLY_PASS=$(openssl rand -base64 12)
SUPPORT_PASS=$(openssl rand -base64 12)
ADMIN_PASS=$(openssl rand -base64 12)
# Create default mail accounts
echo "📧 Creating default mail accounts..."
create_mail_account "noreply@$MAIL_DOMAIN" "$NOREPLY_PASS"
create_mail_account "support@$MAIL_DOMAIN" "$SUPPORT_PASS"
create_mail_account "admin@$MAIL_DOMAIN" "$ADMIN_PASS"
# Determine which env file to update
ENV_FILE=".env"
if [ "$1" == "--production" ] || [ "$NODE_ENV" == "production" ]; then
ENV_FILE=".env.production"
fi
# Create env file if it doesn't exist
if [ ! -f "$ENV_FILE" ]; then
echo "📝 Creating $ENV_FILE from .env.example..."
cp .env.example "$ENV_FILE"
fi
# Update mail configuration in env file
echo "📝 Updating mail configuration in $ENV_FILE..."
# Function to update or add env variable
update_env() {
local key=$1
local value=$2
local file=$3
if grep -q "^$key=" "$file"; then
# Update existing value
sed -i.bak "s|^$key=.*|$key=$value|" "$file"
else
# Add new value
echo "$key=$value" >> "$file"
fi
}
# Update mail settings
update_env "MAIL_USER" "noreply@$MAIL_DOMAIN" "$ENV_FILE"
update_env "MAIL_PASS" "$NOREPLY_PASS" "$ENV_FILE"
update_env "MAIL_FROM" "Keys For All <noreply@$MAIL_DOMAIN>" "$ENV_FILE"
update_env "MAIL_SUPPORT" "support@$MAIL_DOMAIN" "$ENV_FILE"
update_env "IMAP_USER" "support@$MAIL_DOMAIN" "$ENV_FILE"
update_env "IMAP_PASS" "$SUPPORT_PASS" "$ENV_FILE"
# Clean up backup files
rm -f "$ENV_FILE.bak"
echo "✅ Mail credentials updated in $ENV_FILE"
# Also save a backup of credentials
CREDS_BACKUP=".mail-credentials.backup"
cat > "$CREDS_BACKUP" << EOF
Mail Server Credentials Backup
Generated: $(date)
Domain: $MAIL_DOMAIN
Accounts:
noreply@$MAIL_DOMAIN : $NOREPLY_PASS
support@$MAIL_DOMAIN : $SUPPORT_PASS
admin@$MAIL_DOMAIN : $ADMIN_PASS
EOF
chmod 600 "$CREDS_BACKUP"
# Show status
echo ""
echo "✅ Mail server setup complete!"
echo ""
echo "📊 Service Status:"
docker-compose -f docker-compose.mail.yml ps
echo ""
echo "🌐 Access Points:"
echo " - SMTP: localhost:587 (submission)"
echo " - SMTP: localhost:25 (standard)"
echo " - IMAP: localhost:143"
echo " - IMAPS: localhost:993"
echo " - MailHog Web UI: http://localhost:8025 (development only)"
echo " - Redis: localhost:6379"
echo ""
echo "📧 Mail accounts created for domain: $MAIL_DOMAIN"
echo " - noreply@$MAIL_DOMAIN"
echo " - support@$MAIL_DOMAIN"
echo " - admin@$MAIL_DOMAIN"
echo ""
echo "⚠️ IMPORTANT:"
echo " 1. Mail credentials automatically updated in: $ENV_FILE"
echo " 2. Credentials backup saved to: $CREDS_BACKUP"
echo " 3. For production, use proper SSL certificates"
echo " 4. Configure proper DNS records (MX, SPF, DKIM, DMARC)"
echo " 5. The admin password is only in $CREDS_BACKUP (not in .env)"
echo ""
echo "🔐 To add more email accounts:"
echo " docker exec -it keys-mailserver setup email add user@$MAIL_DOMAIN password"
echo ""
echo "📖 For more information, see the documentation in docs/MAIL_SERVER_SETUP.md"