Ubuntu之邮件服务器

本教程详细介绍了如何在Ubuntu系统中配置Postfix作为邮件服务器、使用Dovecot进行SMTP验证,并运行SquirrelMail作为网页客户端。包括邮件账户设置、配置Postfix与Dovecot的SMTP验证、生成数字证书、安装与配置Dovecot、测试SMTP-AUTH和SMTP/POP3接口等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先说一下此方法的局限

  • Postfix发送;Dovecot接收;SquirrelMail运行于nginx,为网页客户端
  • 邮件账户为Ubuntu实际用户,不支持虚拟账户
  • 如果不与DNS结合,就会导致对于其他域的邮箱,只能发送不能接收,因为其他域的机器不知道本文的zjc.com是啥东东
  • 暂未想到

前提

  • 配置的机器hostname为zjc.com, IP地址为10.1.6.197

准备工作

  • 如果软件源不给力,可以换源,然后更新源sudo apt-get update,此处不细表

安装postfix

  • $ sudo apt-get install postfix
  • 安装后默认配置选择Internet Sites选项好了,其余的默认就好了

配置postfix

  • $ sudo dpkg-reconfigure postfix
  • 在接下来的选项中依次选择
    1. Internet Site
      这里写图片描述
    2. zjc.com
      这里写图片描述
    3. zjc
      这里写图片描述
    4. zjc.com, localhost.zjc, localhost
      这里写图片描述
    5. No
    6. 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 10.1.6.197/24
      这里写图片描述
    7. 0
    8. +
    9. all

配置Postfix和Dovecot的SMTP验证(使用TLS)

Configure Postfix for SMTP-AUTH using Dovecot SASL

`$ sudo gedit /etc/postfix/main.cf` 在/etc/postfix/main.cf中增加以下行
home_mailbox = Maildir/
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes

为TLS生成数字证书

$ cd ~
$ openssl genrsa -des3 -out server.key 2048
$ openssl rsa -in server.key -out server.key.insecure
$ mv server.key server.key.secure
$ mv server.key.insecure server.key
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
$ sudo cp server.crt /etc/ssl/certs
$ sudo cp server.key /etc/ssl/private

为postfix配置证书和秘钥路径

$ sudo postconf -e 'smtpd_tls_key_file = /etc/ssl/private/server.key'
$ sudo postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/server.crt'

配置postfit:支持smtps(465)和submission(587)

`$ sudo gedit /etc/postfix/master.cf` 对下面一些行取消注释
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       n       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

安装Dovecot

  • $ sudo apt-get install dovecot-common
  • 在弹出的安装界面选择
    1. yes
    2. mail.zjc.com

配置Dovecot

  • $ sudo gedit /etc/dovecot/conf.d/10-master.conf
  • 找到# Postfix smtp-auth(95行),修改如下
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }
  • $ sudo gedit /etc/dovecot/conf.d/10-auth.conf
    替换
    auth_mechanisms = plain

    auth_mechanisms = plain login

重启服务

$ sudo service postfix restart
$ sudo service dovecot restart

测试SMTP-AUTH和SMTP/POP3接口

zjc@zjc:~$ telnet mail.zjc.com smtp
Trying 10.1.6.197...
Connected to mail.zjc.com.
Escape character is '^]'.
220 zjc.com ESMTP Postfix (Ubuntu)

接下来,键入echlo mail.zjc.com,并确信得到了下面的行

ehlo mail.zjc.com
250-zjc.com
--------
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
---------
250 DSN

然后,端口换位587 (telnet mail.zjc.com 587)再测试一遍
Postfix配置就完成了

安装devecot

  • $ sudo apt-get install dovecot-imapd dovecot-pop3d

配置devcot

  • $ sudo gedit /etc/dovecot/conf.d/10-mail.conf


    mail_location = mbox:~/mail:INBOX=/var/mail/%u
    替换为
    mail_location = maildir:~/Maildir

  • $ sudo gedit /etc/dovecot/conf.d/20-pop3.conf

    pop3_uidl_format = %08Xu%08Xv这一行取消注释

  • $ sudo gedit /etc/dovecot/conf.d/10-ssl.conf

    打开ssl
    ssl = yes

  • 重启devecot
    $ sudo service dovecot restart

测试pop3和imap功能


通过telnet命令,测试端口

telnet mail.zjc.com 110
Trying 10.1.6.197...
Connected to zjc.com.
Escape character is '^]'.
+OK Dovecot (Ubuntu) ready.

然后重复测试以下端口:995, 993, 143


或者通过查看端口命令$ netstat -nl4

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:465             0.0.0.0:*               LISTEN     
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:993             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:995             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:587             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN     

创建测试账户mtest1

$ sudo useradd -m mtest1 -s /sbin/nologin
$ sudo passwd mtest1

配置thunderbird客户端,如下

这里写图片描述
这里有一个证书安全的问题(涉及到证书信任问题,留待以后解决)
点击Confirm Security Exception接受
在配置邮箱和发送邮件时候都会弹出错误提示,都选择接受就好了

测试发送邮件

  • 发送给自己

    可以发送接收

  • 发送给qq邮箱

    qq邮箱可以接收,但是回复失败:因为找不到zjc.com是啥东东

  • 发送给exchange邮箱

    貌似收不到 - (第二天)收到了,可能跟公司的邮件服务器反应有点慢有关系

APC

安装php apc

$ sudo apt-get install php-apc

重启php5-fpm

$ sudo /etc/init.d/php5-fpm restart

APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It’s similar to other PHP opcode cachers, such as eAccelerator and XCache. It is strongly recommended to have one of these installed to speed up your PHP page.

在nginx上配置mail.zjc.com虚拟目录

安装squirrelmail

$ sudo apt-get install squirrelmail

配置squirrelmail

$ squirrelmail-configure
以下是依次的选择:

D (Set pre-defined settings for specific IMAP servers)
Please select your IMAP server: devecot
S (Save data)

修改enginx的虚拟主机mail.zjc.com配置文件

sudo gedit /etc/nginx/sites-available/mail.zjc.com.vhost

文件内容为:


server {
       [...]
       location /squirrelmail {
            root /usr/share/;
            index index.php index.html index.htm;
            location ~ ^/squirrelmail/(.+\.php)$ {
                try_files $uri =404;
                root /usr/share/;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
            }
            location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                root /usr/share/;
            }
        }

       location /webmail {
           rewrite ^/* /squirrelmail last;
       }
       [...]
 }       

重启nginx服务器

$ sudo /etc/init.d/nginx reload

现在可以通过http://mail.zjc.com/squirrelmail来访问了,用户名密码为机器上的真实用户名密码

参考

http://www.krizna.com/ubuntu/setup-mail-server-ubuntu-14-04/
https://www.howtoforge.com/running-squirrelmail-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04

### 配置 Ubuntu 使用 QQ 邮件服务器 为了使 Ubuntu 能够通过 QQ 邮箱的 SMTP 服务器发送邮件,可以采用 `msmtp` 工具来实现这一功能。以下是具体的配置过程。 #### 安装 msmtp 首先需要更新软件包列表并安装 `msmtp`: ```bash sudo apt-get update && sudo apt-get install msmtp ``` #### 创建 msmtp 配置文件 创建或编辑 `/etc/msmtprc` 文件用于全局设置,或者在用户目录下创建 `.msmtprc` 文件用于特定用户的设置。这里以全局配置为例,在终端输入命令打开配置文件进行编辑: ```bash sudo nano /etc/msmtprc ``` 向其中添加如下内容: ```plaintext defaults tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt account qqmail host smtp.qq.com port 587 auth on from your_qq_number@qq.com # 将 "your_qq_number" 替换成实际的 QQ 号码 user your_qq_number # 同样替换为真实的 QQ 号码 password your_app_password # 这里填写的是授权码而非登录密码 logfile ~/.msmtp.log # 日志位置可自定义 # 设置默认账户 account default :qqmail ``` 注意:对于 `password` 字段中的值应为 QQ邮箱提供的SMTP服务专用授权码而不是常规登陆密码[^3]。 #### 测试发送邮件 完成上述配置之后可以通过简单的测试脚本来验证是否能够成功发送电子邮件。编写一个名为 `send_test_email.sh` 的 shell 脚本: ```bash #!/bin/bash echo "Subject: Test Email from Ubuntu via QQ Mail Server\nThis is a test email sent using the QQ mail server through an Ubuntu system." | msmtp -a default recipient@example.com ``` 赋予该脚本执行权限并通过它尝试发送一封测试信件给指定收件人地址(需将 `recipient@example.com` 更改为有效的电子邮箱): ```bash chmod +x send_test_email.sh ./send_test_email.sh ``` 如果一切正常,则应该可以在目标邮箱中看到这封来自Ubuntu系统的测试邮件。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值