From 002cb7e6f4a4da27018a64e2111e8a6320febd98 Mon Sep 17 00:00:00 2001
From: yumoqing <yumoqing@gmail.com>
Date: Sat, 15 Mar 2025 11:31:13 +0800
Subject: [PATCH] bugfix

---
 mailserver/mailserver.sh | 200 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 200 insertions(+)
 create mode 100644 mailserver/mailserver.sh

diff --git a/mailserver/mailserver.sh b/mailserver/mailserver.sh
new file mode 100644
index 0000000..3cf21f4
--- /dev/null
+++ b/mailserver/mailserver.sh
@@ -0,0 +1,200 @@
+#!/bin/bash
+
+##############################################################
+# 参数占位(请根据实际情况替换)
+# 注意事项:请在执行前确认所有参数均被正确替换
+##############################################################
+MAIL_DOMAIN="{{your-mail-domain.com}}"          # 邮件域名(如:example.com)
+MAIL_IP="{{your-server-ip}}"                    # 邮件服务器IP地址
+ADMIN_EMAIL="{{admin@your-domain.com}}"         # 管理员邮箱(用于Postfix收件)
+USER_NAME="{{username}}"                        # 系统用户账号(用于邮箱存储)
+USER_PASSWORD="{{password}}"                    # 用户密码
+WEBROOT_PATH="{{/var/www/html}}"                # Certbot使用HTTP验证的网站目录
+
+##############################################################
+# 初始配置
+##############################################################
+echo "==> 开始初始化..."
+
+# 预检测
+echo ">> 检查root权限..."
+if [ "$(id -u)" != "0" ]; then
+  echo "请使用root权限执行脚本" >&2
+  exit 1
+fi
+
+# 更新系统
+echo ">> 更新软件源..."
+apt update && apt upgrade -y
+
+# 安装必要组件
+echo ">> 安装Postfix/Dovecot..."
+apt install -y postfix mailutils dovecot-core dovecot-imapd dovecot-lmtpd openssl certbot
+
+echo ">> 初始配置完成"
+
+##############################################################
+# 配置Postfix邮件服务器
+##############################################################
+echo "==> 配置Postfix..."
+
+# 配置Postfix主配置文件
+cat > /etc/postfix/main.cf <<EOF
+# 主配置参数
+myhostname = $MAIL_DOMAIN
+mydomain = $MAIL_DOMAIN
+myorigin = \$mydomain
+inet_interfaces = all
+inet_protocols = all
+
+# 通用邮件参数
+relayhost =
+mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
+mailbox_size_limit = 0
+recipient_delimiter = +
+
+# 虚拟域名配置
+virtual_mailbox_domains = $MAIL_DOMAIN
+virtual_mailbox_base = /var/mail/vhosts
+virtual_mailbox_maps = hash:/etc/postfix/vmailbox
+virtual_alias_maps = hash:/etc/postfix/virtual
+virtual_alias_domains =
+virtual_uid_maps = static:5000
+virtual_gid_maps = static:5000
+virtual_transport = lmtp:unix:private/dovecot-lmtp
+
+# TLS设置(自动替换SSL路径)
+smtpd_use_tls = yes
+smtpd_tls_security_level = may
+smtpd_tls_cert_file = /etc/letsencrypt/live/$MAIL_DOMAIN/fullchain.pem
+smtpd_tls_key_file = /etc/letsencrypt/live/$MAIL_DOMAIN/privkey.pem
+
+# 防垃圾邮件设置
+disable_vrfy_command = yes
+smtpd_helo_restrictions = permit_mynetworks,reject_invalid_helo_hostname,permit
+smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
+EOF
+
+echo ">> Postfix基础配置完成"
+
+##############################################################
+# 配置邮箱用户与域名映射
+##############################################################
+echo "==> 创建虚拟邮箱配置..."
+
+# 创建邮箱存储目录
+mkdir -p /var/mail/vhosts/$MAIL_DOMAIN
+chown -R vmail:vmail /var/mail
+
+# 创建虚拟别名(管理员收件)
+echo "$ADMIN_EMAIL" > /etc/postfix/virtual
+
+# 创建用户邮箱映射(示例用户)
+echo "user1@example.com  $MAIL_DOMAIN/user1/" > /etc/postfix/vmailbox
+
+# 生成哈希数据库
+postmap /etc/postfix/virtual
+postmap /etc/postfix/vmailbox
+
+echo ">> 邮箱账户配置完成"
+
+##############################################################
+# Dovecot 配置
+##############################################################
+echo "==> 配置Dovecot..."
+
+# 创建主配置
+cat > /etc/dovecot/dovecot.conf <<EOF
+protocols = imap lmtp
+listen = *
+disable_plaintext Authentication = yes
+
+# 账户配置
+defaultibble_uid = vmail
+defaultibble_gid = vmail
+mail_location = maildir:/var/mail/vhosts/%d/%n
+
+# SSL证书路径(自动替换)
+ssl = required
+ssl_cert = </etc/letsencrypt/live/$MAIL_DOMAIN/fullchain.pem
+ssl_key = </etc/letsencrypt/live/$MAIL_DOMAIN/privkey.pem
+EOF
+
+# 配置IMAP/LMTP协议
+cat > /etc/dovecot/conf.d/10-master.conf <<EOF
+service lmtp {
+  unix_listener /var/spool/postfix/private/dovecot-lmtp {
+    mode = 0600
+    user = postfix
+    group = postfix
+  }
+}
+
+service imap {
+  inet_listener imap {
+    port = 143
+  }
+  inet_listener imaps {
+    port = 993
+    ssl = yes
+  }
+}
+EOF
+
+echo ">> Dovecot配置完成"
+
+##############################################################
+# 获取SSL证书(Let's Encrypt)
+##############################################################
+echo "==> 申请SSL证书..."
+
+if [ ! -d "/etc/letsencrypt/live/$MAIL_DOMAIN" ]; then
+  certbot certonly --standalone --agree-tos --no-eff-email \
+    --email $ADMIN_EMAIL \
+    -d $MAIL_DOMAIN
+
+  # 设置定时更新
+  (crontab -l ; echo "0 0 * * * certbot renew --quiet") | crontab -
+fi
+
+echo ">> SSL证书获取完成"
+
+##############################################################
+# 防火墙配置
+##############################################################
+echo "==> 配置防火墙..."
+
+if ! systemctl is-active --quiet ufw; then
+  ufw allow 'OpenSSH'
+  ufw allow "$MAIL_IP"/4
+  ufw enable
+fi
+
+ufw allow 25/tcp  # SMTP
+ufw allow 587/tcp # Submission
+ufw allow 465/tcp # SMTPS
+ufw allow 143/tcp # IMAP
+ufw allow 993/tcp # IMAPS
+ufw reload
+
+echo ">> 防火墙配置完成"
+
+##############################################################
+# 启动服务并测试
+##############################################################
+echo "==> 启动服务..."
+
+systemctl restart postfix
+systemctl restart dovecot
+
+# 检查服务状态
+systemctl status postfix
+systemctl status dovecot
+
+echo "--------------------"
+echo "部署完成,请验证服务:"
+echo "1. 检查端口 25/587/993 是否开放"
+echo "2. 发送测试邮件:echo \"测试邮件正文\" | mail -s \"测试标题\" $ADMIN_EMAIL"
+echo "3. 查看日志:tail -f /var/log/mail.log"
+echo "--------------------"
+