当前位置: 代码迷 >> 综合 >> 阿里云CentOSLinux服务器上怎么用postfix搭建邮件服务器+openwebmail
  详细解决方案

阿里云CentOSLinux服务器上怎么用postfix搭建邮件服务器+openwebmail

热度:94   发布时间:2023-11-22 07:05:32.0

一、准备工作

  1. 为邮件服务器添加DNS解析

虽然不加DNS解析也能把邮件发出去,但会被大多数邮件服务器当作垃圾邮件。根据我们的实际经验,需要添加三条DNS解析记录:A记录、MX记录、TXT记录。比如域名cnblogs.info,对应的DNS记录如下:
查看我的DNS博客

  1. 准备存放邮件的硬盘空间

如果用的是阿里云入门级Linux服务器,有一块20G的数据盘未挂载,需要格式化并挂载(假设这里挂载的目录是/data),具体操作步骤见之前的博文阿里云云服务器硬盘分区及挂载。

二、配置postfix

postfix是CentOS默认安装的邮件服务器软件。以下配置示例假设要配置的域名是cnblogs.info,邮件服务器主机名是mail.cnblogs.info。

  1. 打开postfix的配置文件

vi /etc/postfix/main.cf
2. :75 光标移至第75行,修改myhostname

myhostname = mail.cnblogs.info
3. :83 光标移至第83行,修改mydomain

mydomain = cnblogs.info
4. :99 光标移至第99行,修改myorigin

myorigin = $mydomain
5. :116光标移至第116行,修改inet_interfaces

inet_interfaces = all
6. :119光标移至第119行,修改inet_protocols

inet_protocols = ipv4
7. :164光标移至第164行,添加$mydomain

mydestination = myhostname,localhost.myhostname, localhost.myhostname,localhost.mydomain, localhost, $mydomain
8. :264光标移至第264行,修改mynetworks

mynetworks = 127.0.0.0/8
子网掩码(netmask)转换器:Network and IP address calculator

  1. :419 光标移至第419行,设置home_mailbox

home_mailbox = Maildir/
10. :425 光标移至第425行,设置mail_spool_directory,/data是之前挂载的数据盘,mail目录需要通过mkdir命令创建

mail_spool_directory = /data/mail
11. 重启postfix使设置生效

service postfix restart

三、用telnet测试邮件服务器是否正常

  1. 在邮件服务器上安装telnet

yum install telnet
[root@localhost ~]# telnet 127.0.0.1 25 (这个不能用域名因为我还没有域名虽然dns配置的时候给自己配置域名了,但是是不存在的)
//连接成功,开始写信
helo mail.cyl.cn //声明本机的主机(这一句在阿里云上就不用了,在虚拟机上使用)
mail from:jack@cyl.cn //声明发件人地址
rcpt to:tom@cyl.cn //声明收件人地址
data //写正文
i am jack!!
. //正文结束标记
quit // 退出
这里写图片描述
2. 连接服务器SMTP端口

telnet 127.0.0.1 25
//110,143端口需要监听

---------------收信验证---------------
[root@localhost ~]# telnet mail.aa.com 110
user tom //收件人登录
pass 123 //邮箱密码
list //列表查看邮件
retr 1 //读取编号为1的邮件
quit //退出邮箱

vi /etc/postfix/main.cf
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 0.0.0.0/0 //允许接收所有网段发来的邮件,根据情况自行修改
relay_domains =$mydestination
在最后增加
smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
default_process_limit = 50
default_destination_concurrency_limit = 20
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_application_name = smtpd
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_client_restrictions = permit_sasl_authenticated
保存退出
service postfix restart

安装cyrus-sasl

# yum -y install cyrus-sasl
chkconfig saslauthd on

vi /etc/sysconfig/saslauthd
修改 /etc/sysconfig/saslauthd
# vim /etc/sysconfig/saslauthd
# Directory in which to place saslauthd’s listening socket, pid file, and so
# on. This directory must already exist.
SOCKETDIR=/var/run/saslauthd
# Mechanism to use when checking passwords. Run “saslauthd -v” to get a list
# of which mechanism your installation was compiled with the ablity to use.
MECH=shadow

修改 vi /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd

mech_list: plain login
测试 saslauthd
# service saslauthd restart
# testsaslauthd -u tom -p ‘tom’
0: OK “Success.”

openwebmail安装
# cd /etc/yum.repos.d
# wget -q http://openwebmail.org/openwebmail/download/redhat/rpm/release/openwebmail.repo
centos7安装openwebmail (yum install -y openwebmail 是centos6.5中的)
FUCK,FUCK,FUCK,搞了(4个小时,操了个dj),头发掉了一半
cd /opt
centos7中需要perl 和perl-suidperl 而且要配套
perl
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/perl-5.16.3-292.el7.x86_64.rpm
rpm -ivh --force --nodeps perl-5.16.3-292.el7.x86_64.rpm

perl-suidperl
wget http://mirror.centos.org/centos/6/os/x86_64/Packages/perl-suidperl-5.10.1-144.el6.x86_64.rpm
rpm -ivh --force --nodeps perl-suidperl-5.10.1-144.el6.x86_64.rpm
配置openwebmail

# vi /var/www/cgi-bin/openwebmail/etc/openwebmail.conf

domainnames cyl.cn
default_language zh_CN.GB2312
default_timeoffset +0800
default_iconset Cool3D.Chinese.Simplified
auth_module auth_unix.pl
mailspooldir /var/spool/mail
ow_cgidir /var/www/cgi-bin/openwebmail
ow_cgiurl /cgi-bin/openwebmail
ow_htmldir /var/www/data/openwebmail
ow_htmlurl /data/openwebmail
logfile /var/log/openwebmail.log

#vi /var/www/cgi-bin/openwebmail/etc/defaults/openwebmail.conf
domainnames cyl
smtpserver 192.168.0.2
#smtpserver 127.0.0.1 (这个如果是再阿里云上的话因为是自己对自己的测试,所以用127.0.0.1而不是用外网地址39.107.116.193)
authpop3_server 192.168.0.2
#authpop3_server 192.168.0.2 (这个如果是再阿里云上的话因为是自己对自己的测试,所以用127.0.0.1而不是用外网地址39.107.116.193)
smtpport 25
smtpauth no

# cd /var/www/cgi-bin/openwebmail
yum install -y libperl*
yum install perl-Socket -y
yum install perl-Digest -y
yum install perl-Digest-MD5
yum install perl-Text-Iconv.x86_64
/唯一能说的就是慢慢找错吧 -----------------------两天才弄好所有错
# ./openwebmail-tool.pl --init
perl-suidperl-5.8.8 (通过CentOS的yum功能直接安装,安装版本 5.8.8这个是和perl配套的,如果和perl版本不一致可能导致初始化openwebmail时找不到libperl.so,openwebmail的辅助软件,必须要有);
#service httpd restart

打开浏览器

输入192.168.45.128/webmail (或者 mail.test.com/webmail,这个我只在128本机上通过,猜测其他机子连接应该修改其dns)
系统提示:
‘/var/www/cgi-bin/openwebmail/.openwebmail.pl’ must setuid to root

原来刚才执行perl /var/www/cgi-bin/openwebmail/misc/tools/wrapsuid/wrapsuid.pl /var/www/cgi-bin/openwebmail/后,所有openwebmail*.pl文件的s位没有了。所以
chmod 4755 /var/www/cgi-bin/openwebmail/openwebmail*.pl
访问: http://localhost/cgi-bin/openwebmail/openwebmail.pl
OK,可以看到登陆框了,说明openwebmail已经安装成功了,下面就是来结合sendmail了。
这里写图片描述
这里写图片描述
这里写图片描述

这里写图片描述

收信的时候
这里写图片描述

这里写图片描述

这里写图片描述
可能出现问题:

如果出现了下面的问题 Couldn’t create File /var/log/openwebmail.log! (Permission denied)

在网上找到解决办法了:如下方面 运行:

#touch /var/log/openwebmail.log

#chcon -u system_u /var/log/openwebmail.log
#chcon -t httpd_sys_script_rw_t /var/log/openwebmail.log

#chcon -t httpd_unconfined_script_exec_t /var/www/cgi-bin/openwebmail/openwebmail*

我还遇到了一个问题,记不清了。大概是不能在/var/spool下面创建mai文件夹,解决方法是用root权限自己建立一个

全文借鉴网站
https://blog.csdn.net/kirao/article/details/3519683
中间遇到的
Requires: perl-suidperl
Removing: 3:perl-suidperl-5.8.1-92.i386 (installed)
perl-suidperl = 3:5.8.1-92
Obsoleted By: 4:perl-5.16.3-292.el7.x86_64 (base)

rpm包
强制卸载:rpm -e –nodeps xxxxxx.rpm

强制安装:rpm -ivh –nodeps xxxxxx.rpm

openwebmail的配置
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed19fa950d100b92235c438014628c8c52358fc51984642c101a39fec57c7e574ecec57e6402b8492ae8f72b73300820b58cc8ff109be4866d72c8713b2e47da0658c418dfdc3021d656924de8df0e97bde74395b9d2a28216049616522adfa3d004&p=c639d41086cc42ad589fc7710f488d&newp=cb759a46d4c308e90be296365b51bb231610db2151d6d0116b82c825d7331b001c3bbfb423251407d6c67d600aa84356effb35703d0923a3dda5c91d9fb4c57479c161&user=baidu&fm=sc&query=linux%D6%D0%B5%C4+openwebmail&qid=c24970630001f5a8&p1=3

找错最好文章
https://www.cnblogs.com/wwufengg/articles/openwebmail-error.html

  1. 填写发件人

MAIL FROM:test@cnblogs.info
回车

  1. 填写收件人

RCPT TO:contact@cnblogs.com
回车

  1. 撰写邮件内容

5.1 开始写邮件内容

DATA
回车

5.2 输入标题

Subject: test message
回车

5.3 输入内容,并以.号结束

test body
.
如果显示下面的信息,说明邮件进入发送队列

250 2.0.0 Ok: queued as 88D6D32A94

四、授权其他服务器通过该邮件服务器发送邮件

未授权的情况下,如果在其他服务器telnet这台服务器,在输入收件人之后会出现Relay access denied错误

RCPT TO:contact@cnblogs.com
554 5.7.1 contact@cnblogs.com: Relay access denied
解决方法:

vi /etc/postfix/main.cf,:264将光标移至第264行,在mynetworks的值之后加这台的服务器IP地址,比如:

mynetworks = 127.0.0.0/8 58.33.14.124
service postfix restart之后,问题解决。

五、其他问题

收到上面telnet发出的邮件时,你会发现收件人信息显示的是undisclosed recipients,解决方法是在DATA命令之后,输入Subject之前,分别输入:

FROM:test@cnblogs.info
TO:contact@cnblogs.com