首页 > Linux > 配置exim4 本地或远程

配置exim4 本地或远程

2010年2月23日 发表评论 阅读评论

本地做MAIL服务器

经过多方查阅资料,终于将exim4的配置搞定了,结果才发现,原来是这么简单。下面我就把我配置的过程写出来,与大家分享。
1)在命令行输入dpkg-reconfigure exim4-config来进行配置。
2)邮件系统配置的常见模式:(互联网站;直接通过 SMTP 发送或接收信件)
3)系统邮件名称:a18zhizao.cn(发件人,收到信件就是这个样子:www@a18zhizao.cn)
4)要监听的入站 SMTP 连接的 IP 地址:(空白,就是什么都不写)
5)其它可接受的邮件目的地址:( localhost.localdomain:vps25.vz25.ramhost.us:localhost)
注:我的主机名是vps25.vz25.ramhost.us
6)中转(relay)邮件的域名: (空白)
7)为这些主机进行邮件转发:(空白)
8)保持最小 DNS 查询量吗(按需拔号 Dial-on-Demand)? (否)
9)将配置文档分拆成小文件吗?(否)

配置完毕。然后,我用mail了一封邮件到qq邮箱。具体如下:

$mail lvs
Subject:Test
This is a test mail.       #(.代表结束输入!)
Cc:a18ccms@gmail.com



远程做MAIL服务器(收信速度会慢些,好处就不用说了。)

# dpkg-reconfigure exim4-config

说明:[]表示通过光标选择该项,{}表示输入的内容
1. [mail sent by smarthost; no local mail]
2. system mail name: {mail.a18zhizao.cn}
3. IP-address to listen on for incoming SMTP connections: 127.0.0.1
4. Other destinations for which mail is accepted: {null}
5. visible domain name for local users: {mail.a18zhizao.cn}
6. IP address or host name of the outgoing smarthost: {smtp.gmail.com::587}
7. Keep number of DNS-queries minial(Dial-on-Demand)? : [No]
8. split configuration into small files?: [No]
因为上述的步骤只是更新了/etc/exim4.conf.conf,因此还需要执行

# update-exim4.conf

这样才能更新exim4实际用到的配置文件。

二、添加gmail账号和密码

# chown root:Debian-exim /etc/exim4/passwd.client

添加如下行, 注意修改用户名和密码

# vim  /etc/exim4/passwd.client

gmail-smtp.l.google.com:yourAccountName@gmail.com:y0uRpaSsw0RD
*.google.com:yourAccountName@gmail.com:y0uRpaSsw0RD
smtp.gmail.com:yourAccountName@gmail.com:y0uRpaSsw0RD

执行

# chown root:Debian-exim /etc/exim4/passwd.client

编辑 /etc/exim4/conf.d/transport/30_exim4-config_remote_smtp_smarthost

# vi /etc/exim4/conf.d/transport/30_exim4-config_remote_smtp_smarthost
#在 "hosts_try_auth..." 之前增加
port=587

将发送者邮件地址添加到 /etc/exim4/email-addresses

# echo 'linuxuser@LocalHost: username@gmail.com' >> /etc/exim4/email-addresses

最后,执行

# update-exim4.conf

三、重启

# /etc/init.d/exim4 restart

邮件发送日志

# tail /var/log/exim4/mainlog

网络又发现的参考文章,比较详细:

debian下用exim4发送邮件
方式一:直接使用SMTP发信

#dpkg-reconfigure exim4-config
(1)将配置文档拆分成小文件么? 否
(2)选择第一项,使用SMTP直接发信
(3)系统邮件名称,随意;(这里我写了admin@ip)
(4)要监听的入站 SMTP 连接的 IP 地址:
127.0.0.1 (这样,就只有本机可以利用exim4)
* (大家都可以用)
192.168.0.0/24,仅该IP范围可以用

(5)其它可接受的邮件目的地址:(empty)
(6)为这些主机进行邮件转发:(empty)
(7)保持最小 DNS 查询量吗(按需拔号 Dial-on-Demand)? 否

方式二:使用smarthost发信
(1)将配置文档拆分成小文件么? 否
(2)选择“用smarthost发信;无本地邮件”
(3)系统邮件名称,随意;(这里我写了admin@ip)
(4)要监听的入站 SMTP 连接的 IP 地址:
127.0.0.1 (这样,就只有本机可以利用exim4)
* (大家都可以用)
192.168.0.0/24,仅该IP范围可以用

(5)其它可接受的邮件目的地址:(empty)
(6)为这些主机进行邮件转发:(empty)
(7)负责处理从本机寄出的邮件的机器(smarthost):真正用来外发邮件的smtp地址(我这里用smtp.gmail.com)
(8)保持最小 DNS 查询量吗(按需拔号 Dial-on-Demand)? 否

还要修改几个配置文件:
(1)/etc/exim4/passwd.client,smtp的帐号密码设置,加入:

stu.xjtu.edu.cn:flowaters@stu.xjtu.edu.cn:password
gmail-smtp.l.google.com:admin@vixp.net:password

(2)/etc/email-addresses,系统邮箱地址,加入:
user: admin@ip
配置完以后运行update-exim4.conf,然后重启 exim4 服务即可。

web服务中使用exim4发信的注意事项:
1. 运行服务的用户名在mail组中,才有mail权限
2. 发邮件的日志在/var/mail/username/下

还有一篇原理介绍:Exim4邮件服务器的工作原理
由于搜索了一下,网络上没有其他出处,防止消失,复制过来:

Exim4被设计成能高效地、不间断地在Internet上运行,而且能处理各种混合邮件。Exim4是如何投递邮件和接收邮件的呢?有何安全机制能确保邮件的安全呢?这节将简单介绍Exim4的工作原理。
Exim4 处理的每封邮件都以一个16位字符的名称标识,该标识由三部份组成,以"-"号分隔,如:1GS3YU-0000zG-Nd。这些字符是经过base编码 的,第一部份的原始信息是接收邮件的时间,第二部份的原始信息是接收邮件的进程id,第三部份的信息与配置文件中localhost_number的设置 相关。标识名与时间和进程相关,有效保证了标识名的唯一性。
Exim4通过基于TCP/IP的SMTP协议从其它主机收取邮件。 Exim4接收邮件后,会把邮件分成两个文件保存在spool目录,在我的机器上就是/var/spool/exim4/input/目录。这两个文件的 命名规则是邮件标识后加-D和-H。以-D结尾的文件保存着邮件正文的信息,以-H结尾的文件保存邮件的头信息。在一些操作系统上,同一目录下包含太多的 文件会造成性能问题,所以Exim4也可通过设置split_spool_directory选项,在input目录下再创建62个子目录来保存邮件信 息。这62个子目录以单个字母和数字命名(26个小写字母+26个大写字母+10个数字共62个数字和字母)。这样的设置,使邮件的排队只在子目录下进 行,而不是一个目录所有邮件。所以即使在邮件量不大的情况下也能效提升邮件系统的性能。
接收的邮件会停留在spool目录直到邮件被传递到接收者或被管理员删除。如果邮件不能被传递,则邮件会被打上"frozen"标记,并且不会再尝试投递 该邮件。但管理员可以设置 ignore_bounce_errors_after和timeout_frozen_after选项,指定一定时间后解除邮件的"frozen"状 态。
当Exim4处理邮件时会把处理过程的日志信息保存在/var/log/exim4目录下。其中mainlog日志记录了大量的信息,从中我们可以跟踪 Exim4处理邮件的过程。在一个大负载的服务器上,生成大量的日志信息会影响服务器的性能,所以我们也可以用 no_message_logs参数禁止日志功能。
router和transport是Exim4传递邮件过程中两个重要的环节, router负责处理邮件地址,并把邮件传递给不同的transport作进一步处理,transport负责把spool中的邮件传递到目的地,完成投 递过程。有两种的transport,一种是本地transport,它的目的地是文件或者本机管道(pipe);另一种transport是远程 transport,它的目的地是远程主机。如果一封邮件有多个接收者,则它可能会通过多个transport进行投递。
router和transport都有不同的driver,实现不同的功能。
router drivers:
* accept,
* dnslookup,
* ipliteral,
* iplookup,
* manualroute,
* queryprogram,
* redirect,
transport drivers:
* appendfile,
* autoreply,
* lmtp,
* pipe
* smtp
安装与配置
用以下命令安装exim4邮件系统:
debian:#~ apt-get install exim4 exim4-config exim4-daemon-light
下载完成后会自动运行配置向导,我们也可运行dpkg-reconfigure exim4-confg命令配置Exim4服务器。exim4的配置文件位于/etc/exim4目录下,update-exim4.conf.conf 文件的配置内容由上面的命令产生,经常改变的命令都集中在这里,而exim4.conf.template文件是Exim4的主配置文件,里面通过 DEBCONFsomethingDEBCONF的方式引用update-exim4.conf.conf文档的something内容。
exim4.conf.template是Exim4的配置文件,它以分段的格式组织各配置参数。一般的配置参数放在配置文件的最开头,其它所有的可选配置参数都以"beging+段名"的形式作为段的开始标识。下面是一些可选段:
* ACL,设置SMTP邮件的ACL(访问控制列表)。
* authenticators,设置验证模式。
* routers,设置不同的router,处理不同类型的邮件地址,并确定如何发送邮件。
* transports,设置不同的transport,把信息传递到不同的目的地。
* retry,设置重试的规则,当邮件不能马上投递时,我们可设置一些重试规则进行再次投递。
* rewrite,定义邮件地址的重写规则。
* local_scan,使用local_scan()函数的功能。要用LOCAL_SCAN_HAS_OPTIONS=yes选项开启。
exim4的配置文件有两种方式,一种是集中式的,所有的配置都集中在exim4.conf.template文件中,一种是分散式的,配置文件按功能不同分散成很多个文件存放在conf.d目录下。一般建议新手使用集中式。
下面是一个update-exim4.conf.conf的示例,它可通过debconf程序自动生成,生成后我们可用文本编辑器来修改它。这个示例把exim4配置成通过网上的smarthost来发送Internet邮件。

# /etc/exim4/update-exim4.conf.conf
#
# Edit this file and /etc/mailname by hand and execute update-exim4.conf
# yourself or use 'dpkg-reconfigure exim4-config'
#
# Please note that this is _not_ a dpkg-conffile and that automatic changes
# to this file might happen. The code handling this will honor your local
# changes, so this is usually fine, but will break local schemes that mess
# around with multiple versions of the file.
#
# update-exim4.conf uses this file to determine variable values to replace
# the DEBCONFsomethingDEBCONF strings in the configuration template files.
#
# Most settings found in here do have corresponding questions in the
# Debconf configuration, but not all of them.
#
# This is a Debian specific file
dc_eximconfig_configtype='smarthost'           #用smarthost发信;通过SMTP或fetchmail接收信件
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1'                #只接收本机的SMTP请求,只在本机使用Exim4
dc_readhost=''
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.21cn.com'                   #远程的SMTP主机,我们通过它来发送邮件
CFILEMODE='644'
dc_use_split_config='false'                    #不使用分散方式的配置文件
dc_hide_mailname='false'
dc_mailname_in_oh='true'

编辑/etc/mailname文档,该文档记录你所发邮件的域名后缀。我的mailname只有一条记录,内容如下:
debian
运行update-exim4.conf命令可以测试/etc/exim4下的配置文件有没有语法错误,如果正常则会生成一个有效简洁的配置文件备份,保存在/var/lib/exim4/config.autogenerated文件中,方便我们查看。
用以下命令还可测试Exim4的配置有否出错:

debian:#~ exim4 -bV
Exim version 4.63 #1 built 23-Aug-2006 17:21:47
Copyright (c) University of Cambridge 2006
Berkeley DB: Sleepycat Software: Berkeley DB 4.3.29: (September 6, 2005)
Support for: crypteq iconv() IPv6 GnuTLS move_frozen_messages
Lookups: lsearch wildlsearch nwildlsearch iplsearch cdb dbm dbmnz dsearch nis nis0 passwd
Authenticators: cram_md5 plaintext
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore autoreply lmtp pipe smtp
Fixed never_users: 0
Size of off_t: 8
Configuration file is /var/lib/exim4/config.autogenerated

启动exim4

ebian:/etc/init.d# ./exim4 restart
Stopping MTA for restart: exim4_listener.
Restarting MTA: exim4.

本地邮件发送测试:

debian:~# exim4 -bt fax
R: system_aliases for fax@debian
R: userforward for fax@debian
R: procmail for fax@debian
R: maildrop for fax@debian
R: local_user for fax@debian
fax@debian
router = local_user, transport = maildir_home

外部邮件发送测试:

debian:#~ exim4 -bt jims.yang@gmail.com
R: smarthost for jims.yang@gmail.com
jims.yang@gmail.com
router = smarthost, transport = remote_smtp_smarthost
host smtp.cdn.21cn.com [202.104.32.230]

测试通过后,我们就可正式发送邮件啦。为了清楚显示邮件的传送过程,我们使用了-v参数。fax是我系统上的一个用户,主机名为debian,所以fax用户的本机邮件地址就是fax@debian。

debian:~# exim4 -v fax@debian
from:root
to:fax@debian
subject:local mail test
test will be ok.
.
LOG: MAIN
<= yjnet@21cn.com U=root P=local S=318
debian:~# delivering 1GQemL-0001eD-Pq
R: system_aliases for fax@debian
R: userforward for fax@debian
R: procmail for fax@debian
R: maildrop for fax@debian
R: local_user for fax@debian
T: maildir_home for fax@debian
LOG: MAIN
=> fax <fax@debian> R=local_user T=maildir_home
LOG: MAIN
Completed

出现Completed说明邮件已发送成功,切换到fax用户我们就可以收到新的邮件啦。下面我们来试试发送Internet。在正式测试之前,我们还要做一些配置。因为通过smtp.21cn.com发送邮件是要经过用户验证的。

    *
      把登录smtp.21cn.com的用户名和密码写入/etc/exim4/passwd.client文档。
      # password file used when the local exim is authenticating to a remote
      # host as a client.
      #
      # see passwd_client(5) for more documentation
      #
      # Example:
      ### target.mail.server.example:login:password
      smtp.cdn.21cn.com:yjnet:1234

smtp.cdn.21cn.com是真正的stmp服务器名,而smtp.21cn.com是别名。在这里一定要用真正的smtp服务器名,否则验证不通过。如何知道真正的smtp服务器名呢?很简单,用我们最常用的ping命令即可。
debian:~# ping smtp.21cn.com
PING smtp.cdn.21cn.com (202.104.32.230): 56 data bytes
yjnet是登录用户名,1234是登录密码。
*
把系统用户对应的外部邮件地址写入/etc/exim4/email-addresses文件。

      # This is /etc/email-addresses. It is part of the exim package
      #
      # This file contains email addresses to use for outgoing mail. Any local
      # part not in here will be qualified by the system domain as normal.
      #
      # It should contain lines of the form:
      #
      #user: someone@isp.com
      #otheruser: someoneelse@anotherisp.com
      root:yjnet@21cn.com

这里的外网邮件用户名yjnet要与passwd.client中的登录用户名yjnet一致。如果不一致,会造成个别mail client不能正常发送邮件。
*
exim4 默认是不使用明文的验证方式的,因为明文的密码很容易在网上被截取。但smtp.cdn.21cn.com是使用明文验证的,所以我们要打开exim4的 明文验证模式。修改/etc/exim4/exim4.conf.template,加入 AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS = 1配置参数。添加参数时要注意,一定要把该参数放到本配置小节的最开头,这样在后面才能有效引用该参数。

      ######################################################################
      #                   AUTHENTICATION CONFIGURATION                     #
      ######################################################################
      begin authenticators
      ....
      # Because AUTH PLAIN and AUTH LOGIN send the password in clear, we
      # only allow these mechanisms over encrypted connections by default.
      # You can set AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS to allow unencrypted
      # clear text password authentication on all connections.
      AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS = 1
      ....

配置完成要重启exim4服务器使配置生效。下面我们就可测试用smtp.21cn.com服务器发邮件啦。

debian:~# exim4 -v jims.yang@gmail.com
from:yjnet@21cn.com
to:jims.yang@gmail.com
subject:test from exim4
测试邮件正文。
.
LOG: MAIN
<= yjnet@21cn.com U=root P=local S=327
debian:~# delivering 1GQfnH-0001hE-PY
R: smarthost for jims.yang@gmail.com
T: remote_smtp_smarthost for jims.yang@gmail.com
Connecting to smtp.cdn.21cn.com [202.104.32.230]:25 ... connected
SMTP<< 220 21cn.com SMTP Server 5 of AIMC 2.9.5.2 (DELAYED).
SMTP>> EHLO debian
SMTP<< 250-21cn.com, helo (DELAYED)
         250-EXPN
         250-HELP
         250-8BITMIME
         250-AUTH=LOGIN PLAIN
         250-AUTH LOGIN PLAIN
         250 XTMD
SMTP>> AUTH PLAIN ****************
SMTP<< 504 Unknown authentication mechanism
LOG: MAIN
plain authenticator failed H=smtp.cdn.21cn.com [202.104.32.230] 504 Unknown authentication mechanism
SMTP>> AUTH LOGIN
SMTP<< 334 VXNlcm5hbWU6
SMTP>> ********
SMTP<< 334 UGFzc3dvcmQ6
SMTP>> ********
SMTP<< 235 OK Authenticated
SMTP>> MAIL FROM:<yjnet@21cn.com> AUTH=root@debian
SMTP<< 250 Ok
SMTP>> RCPT TO:<jims.yang@gmail.com>
SMTP<< 250 User <jims.yang@gmail.com> is not local, will forward to <64.233.167.114:25>.
SMTP>> DATA
SMTP<< 354 Start mail input; end with <CRLF>.<CRLF>
SMTP>> writing message and terminating "."
SMTP<< 250 Requested mail action okay, completed. Message-ID=<E1GQfnH-0001hE-PY@debian>
SMTP>> QUIT
LOG: MAIN
=> jims.yang@gmail.com R=smarthost T=remote_smtp_smarthost H=smtp.cdn.21cn.com [202.104.32.230]
LOG: MAIN
Completed

成功发送。

分类: Linux 标签: , ,
  1. 2010年6月1日18:25 | #1

    正需要exim4的配置方法

  2. 2010年6月2日18:55 | #2

    @ppg
    呵呵~~对你有用太好了~~

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。直接点击评论上方的回复实现此功能