Postfix-Dovecot-Roundcube新部署:Web端发件失败终端正常求助
看起来你遇到的是Roundcube无法连接Postfix SMTP服务的问题,我帮你梳理下问题根源和修复步骤:
核心问题定位
从Nginx的错误日志里能直接抓到关键:
PHP message: fsockopen(): unable to connect to ssl://mail.test.com:587 (Unknown error)
你实际没有注册mail.test.com这个域名,仅用VPS IP运行邮件系统,但Roundcube却在尝试连接这个不存在的域名的587端口,加上你用的SSL证书是绑定这个域名的,双重问题导致连接失败。另外Postfix的日志显示来自localhost的连接刚建立就断开,说明Roundcube的请求根本没完成正常的握手流程。
具体修复步骤
1. 修正Roundcube的SMTP配置
Roundcube的配置文件一般在/etc/roundcube/config.inc.php,你现在的配置里肯定写了指向mail.test.com的SMTP服务器,这是核心问题。改成本地连接更可靠:
打开配置文件,找到SMTP相关的配置项,修改成:
// 用本地127.0.0.1连接,开启TLS $config['smtp_server'] = 'tls://127.0.0.1'; $config['smtp_port'] = 587; // 如果是自签名证书,关闭证书验证(仅内部使用时) $config['smtp_conn_options'] = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, ), );
如果不想用加密(本地连接安全有保障),也可以改成:
$config['smtp_server'] = 'localhost'; $config['smtp_port'] = 587; $config['smtp_secure'] = '';
2. 检查Postfix的Submission端口配置
确保Postfix的587端口(submission服务)正常监听并允许本地认证连接。打开/etc/postfix/master.cf,找到submission段,确保它的配置是这样的(去掉注释,调整参数):
submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject -o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING
修改后重启Postfix:
sudo systemctl restart postfix
3. 修正本地Hosts配置
你的/etc/hosts配置有点混乱,既然没有注册域名,把VPS的公网IP和主机名对应起来,确保本地解析正常:
sudo nano /etc/hosts
修改成:
127.0.0.1 localhost <你的VPS公网IP> mail mail.test.com
然后重启相关服务:
sudo systemctl restart dovecot sudo systemctl restart nginx
验证修复
做完上面的步骤后,先在终端测试本地SMTP连接:
telnet 127.0.0.1 587
如果能看到220 mail.test.com ESMTP Postfix (Ubuntu)的提示,说明连接正常。然后再用Roundcube尝试发送邮件,同时查看/var/log/mail.log和/var/log/nginx/error.log,确认没有新的错误。
内容的提问来源于stack exchange,提问作者Mohamed Ibrahim

