CentOS安装 Postfix+Dovecot

1.安装好postfix

#vi /etc/postfix/main.cf

INTERNET HOST AND DOMAIN NAMES

#

The myhostname parameter specifies the internet hostname of this

mail system. The default is to use the fully-qualified domain name

from gethostname(). $myhostname is used as a default value for many

other configuration parameters.

#

#myhostname = host.domain.tld

#myhostname = virtual.domain.tld
myhostname = mail.xxxx.com ← 追加

The mydomain parameter specifies the local internet domain name.

The default is to use $myhostname minus the first component.

$mydomain is used as a default value for many other configuration

parameters.

#

#mydomain = domain.tld
mydomain = xxxx.com ← 追加(域名指定)

SENDING MAIL

#

The myorigin parameter specifies the domain that locally-posted

mail appears to come from. The default is to append $myhostname,

which is fine for small sites. If you run a domain with multiple

machines, you should (1) change this to $mydomain and (2) set up

a domain-wide alias database that aliases each user to

user@that.users.mailhost.

#

For the sake of consistency between sender and recipient addresses,

myorigin also specifies the default domain name that is appended

to recipient addresses that have no @domain part.

#

#myorigin = $myhostname

#myorigin = $mydomain
myorigin = $mydomain ← 追加

The inet_interfaces parameter specifies the network interface

addresses that this mail system receives mail on. By default,

the software claims all active interfaces on the machine. The

parameter also controls delivery of mail to user@[ip.address].

#

See also the proxy_interfaces parameter, for network addresses that

are forwarded to us via a proxy or network address translator.

#

Note: you need to stop/start Postfix when this parameter changes.

#

#inet_interfaces = all

#inet_interfaces = $myhostname

#inet_interfaces = $myhostname, localhost
inet_interfaces = localhost

inet_interfaces = all ← 变更(可以收外网邮件)

The mydestination parameter specifies the list of domains that this# machine considers itself the final destination for.

#

These domains are routed to the delivery agent specified with the

local_transport parameter setting. By default, that is the UNIX

compatible delivery agent that lookups all recipients in /etc/passwd

and /etc/aliases or their equivalent.

#

The default is $myhostname + localhost.$mydomain. On a mail domain

gateway, you should also include $mydomain.

#

Do not specify the names of virtual domains - those domains are

specified elsewhere (see VIRTUAL_README).

#

Do not specify the names of domains that this machine is backup MX

host for. Specify those names via the relay_domains settings for

the SMTP server, or use permit_mx_backup if you are lazy (see

STANDARD_CONFIGURATION_README).

#

The local machine is always the final destination for mail addressed

to user@[the.net.work.address] of an interface that the mail system

receives mail on (see the inet_interfaces parameter).

#

Specify a list of host or domain names, /file/name or type:table

patterns, separated by commas and/or whitespace. A /file/name

pattern is replaced by its contents; a type:table is matched when

a name matches a lookup key (the right-hand side is ignored).

Continue long lines by starting the next line with whitespace.

#

See also below, section “REJECTING MAIL FOR UNKNOWN LOCAL USERS”.

#
mydestination = $myhostname, localhost.$mydomain, localhost

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ← 变更

#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,

mail.$mydomain, www.$mydomain, ftp.$mydomain

DELIVERY TO MAILBOX

#

The home_mailbox parameter specifies the optional pathname of a

mailbox file relative to a user’s home directory. The default

mailbox file is /var/spool/mail/user or /var/mail/user. Specify

“Maildir/“ for qmail-style delivery (the / is required).

#

#home_mailbox = Mailbox

#home_mailbox = Maildir/
home_mailbox = Maildir/ ← 追加(邮件箱Maildir形式)

SHOW SOFTWARE VERSION OR NOT

#

The smtpd_banner parameter specifies the text that follows the 220

code in the SMTP server’s greeting banner. Some people like to see

the mail version advertised. By default, Postfix shows no version.

#

You MUST specify $myhostname at the start of the text. That is an

RFC requirement. Postfix itself does not care.

#

#smtpd_banner = $myhostname ESMTP $mail_name

#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
smtpd_banner = $myhostname ESMTP unknown ← 追加(隐藏邮件服务器所使用的软件名)

下面到结尾都是追加的(SMTP-Auth设定)
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination

message_size_limit = 10485760 ← 追加(限制邮件大小10MB=1010241024)

2.SMTP-Auth设定
[root@centos ~]# /etc/rc.d/init.d/saslauthd start ← saslauthd启动
saslauthd 启动中: [ OK ]

[root@centos ~]# chkconfig saslauthd on ← saslauthd自动启动设定

[root@centos ~]# chkconfig –list saslauthd ← saslauthd自动启动确认
saslauthd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos ~]# vi /usr/lib/sasl2/smtpd.conf ← 修改SMTP-Auth认证文件
pwcheck_method: saslauthd

pwcheck_method: auxprop ← 变更

3.设置Maildir形式的邮件箱
[root@centos ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
[root@centos ~]# chmod -R 700 /etc/skel/Maildir/

4.如果系统默认启动了sendmail或者其他邮件软件 把它停了 并取消自动启动 我的情况是sendmail
[root@centos ~]# /etc/rc.d/init.d/sendmail stop
[root@centos ~]# chkconfig sendmail off
[root@centos ~]# chkconfig –list sendmail
sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

5.邮件服务器切换
[root@centos ~]# alternatives –config mta ← 邮件服务器切换 (会出现下面)

choice command

*+ 1 /usr/sbin/sendmail.sendmail
2 /usr/sbin/sendmail.postfix

:2 ← 选择Postfix的编号 按enter

6.启动Postfix
[root@centos ~]# /etc/rc.d/init.d/postfix start
Starting postfix: [ OK ]
[root@centos ~]# chkconfig postfix on
[root@centos ~]# chkconfig –list postfix
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off

7.安装dovecot (centos用yum可以安装 Ubuntu应该是apt-get)
[root@centos ~]# vi /etc/dovecot.conf ← 修改Dovecot设定文件

Protocols we want to be serving:

imap imaps pop3 pop3s

#protocols = imap imaps
protocols = imap imaps pop3 pop3s ← 追加

Location for users’ mailboxes. This is the same as the old default_mail_env

setting. The default is empty, which means that Dovecot tries to find the

mailboxes automatically. This won’t work if the user doesn’t have any mail

yet, so you should explicitly tell Dovecot the full location.

#

If you’re using mbox, giving a path to the INBOX file (eg. /var/mail/%u)

isn’t enough. You’ll also need to tell Dovecot where the other mailboxes are

and where Dovecot can place its index files. This is called the “root mail

directory”, and it must be the first path given in the mail_location setting.

#

There are a few special variables you can use, eg.:

#

%u - username

%n - user part in user@domain, same as %u if there’s no domain

%d - domain part in user@domain, empty if there’s no domain

%h - home directory

#

See doc/variables.txt for full list. Some examples:

#

mail_location = maildir:~/Maildir

mail_location = mbox:~/mail:INBOX=/var/mail/%u

mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n

#

http://wiki.dovecot.org/MailLocation

#

#mail_location =
mail_location = maildir:~/Maildir ← 追加

Default MAIL environment to use when it’s not set. By leaving this empty

dovecot tries to do some automatic detection as described in

doc/mail-storages.txt. There’s a few special variables you can use:

#

%u - username

%n - user part in user@domain, same as %u if there’s no domain

%d - domain part in user@domain, empty if user there’s no domain

%h - home directory

#

You can also limit a width of string by giving the number of max. characters

after the ‘%’ character. For example %1u gives the first character of

username. Some examples:

#

default_mail_env = maildir:/var/mail/%1u/%u/Maildir

default_mail_env = mbox:~/mail/:INBOX=/var/mail/%u

default_mail_env = mbox:/var/mail/%d/%n/:INDEX=/var/indexes/%d/%n

#

#default_mail_env =
default_mail_env = maildir:~/Maildir ← 追加

‘:’ separated list of directories under which chrooting is allowed for mail

processes (ie. /var/mail will allow chrooting to /var/mail/foo/bar too).

This setting doesn’t affect login_chroot or auth_chroot variables.

WARNING: Never add directories here which local users can modify, that

may lead to root exploit. Usually this should be done only if you don’t

allow shell access for users. See doc/configuration.txt for more information.

#valid_chroot_dirs =
valid_chroot_dirs = /home ← 追加※用OpenSSH进行Chroot的情况下才设

[root@centos ~]# /etc/rc.d/init.d/dovecot start
Dovecot Imap启动中: [ OK ]

[root@centos ~]# chkconfig dovecot on
[root@centos ~]# chkconfig –list dovecot
dovecot 0:off 1:off 2:on 3:on 4:on 5:on 6:off

8.邮件用户追加(以centos为例)
[root@centos ~]# useradd -s /sbin/nologin centos ← 用户追加

[root@centos ~]# passwd centos ← 密码设定
Changing password for user centos.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

如果想把SMTP-Auth用的用户名和密码弄的跟系统用户名和密码不同的话 才用到下面4步
[root@centos ~]# echo “密码” | saslpasswd2 -p -u 域名 -c centos
[root@centos ~]# sasldblistusers2 ← SMTP-Auth用用户名密码确认
centos@域名: userPassword
[root@centos ~]# chgrp postfix /etc/sasldb2 ← 把/etc/sasldb2的所有group变成postfix
想删除SMTP-Auth用用户名密码确认
[root@centos ~]# saslpasswd2 -d centos -u 域名

 

附:fenzsoft下面的postfix.main.cf

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = fenzsoft.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost.fenzsoft.com, AY130828163919423534Z, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

 
安裝POP3與IMAP套件才能收信(你也可以只安裝一種),預設Server版當初有選安裝mail服務的話,這兩個都會幫你裝到好。
$ sudo apt-get install dovecot-pop3d
$ sudo apt-get install dovecot-imapd

$ sudo vi /etc/dovecot/dovecot.conf

#使用兩種方式可以收信
protocols = imap imaps pop3 pop3s

#支援的格式
mail_location = mbox:~/mail:INBOX=/var/mail/%u
mail_privileged_group = mail

測試POP3與IMAP是否可行
pop3出現以下文字代表正常
$ telnet localhost pop3
Trying ::1…
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
+OK Dovecot ready.

imap出現以下文字代表正常
$ telnet localhost imap
Trying ::1…
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.

  • OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE STARTTLS AUTH=PLAIN] Dovecot ready.

請用Outlook設定就可以收信囉
版主發現每次登入都會有一封信件,內容如下:
This text is part of the internal format of your mail folder, and is not
a real message. It is created automatically by the mail system software.
If deleted, important folder data will be lost, and it will be re-created
with the data reset to initial values.

編輯$sudo chmod 777 /var/spool/mail/john
之後,就不會出現此一問題囉

坚持原创技术分享,您的支持将鼓励我继续创作!