本页内容
- 1 预备说明
- 2 安装 fetchmail
- 3 配置 fetchmail 3.1 使用全局配置文件以守护进程方式运行 fetchmail 3.2 使用每用户配置文件并通过 Cron 运行 fetchmail
- 4 链接
- 3.1 使用全局配置文件以守护进程方式运行 fetchmail
- 3.2 使用每用户配置文件并通过 Cron 运行 fetchmail
fetchmail 是一个用于从远程服务器检索邮件的程序。设想你在五台不同的服务器上拥有五个邮件账户。当然,你不会想逐个连接它们去收取邮件。这正是 fetchmail 发挥作用的地方。如果你在 Linux 服务器上拥有一个用户账户,就可以让 fetchmail 从远程服务器下载邮件,并把它们全部放进一个邮箱(即你的 Linux 用户邮箱),随后你可以用邮件客户端(例如 Thunderbird 或 Outlook)从该邮箱收取邮件。
或者设想你在某家不提供垃圾邮件和病毒过滤的服务商处有一个邮件账户。这种情况下,你可以用 fetchmail 把邮件下载到自己的服务器,并在用邮件客户端下载这些邮件之前,先让它们通过垃圾邮件和病毒过滤器(例如 SpamAssassin 和 ClamAV)。
1 预备说明
你需要一台 Linux 服务器,且其上有一个能够接收邮件的系统用户,这意味着系统上必须安装 MTA(例如 Postfix 或 Sendmail)。否则,fetchmail 将无法工作,因为它会试图把下载下来的邮件转交给 MTA(Postfix、Sendmail 等),再由 MTA 把邮件投递到用户的邮箱(你可以配置系统在此过程中加入垃圾邮件和病毒扫描,例如使用 amavisd-new 或 procmail,但本教程不涉及此内容)。
本教程中我使用一台 Debian 系统,其上存在两个名为 falko 和 till 的用户。
2 安装 fetchmail
要安装 fetchmail,我们只需运行
apt install fetchmail
3 配置 fetchmail
配置 fetchmail 有两种方式。我们可以让它配合一个全局配置文件以守护进程方式运行,也可以创建一个 cron 作业,让 fetchmail 配合每用户配置文件一起运行。本文将分别介绍这两种方法。
3.1 使用全局配置文件以守护进程方式运行 fetchmail
要让 fetchmail 以守护进程方式运行,我们必须编辑 /etc/default/fetchmail,并把 START_DAEMON 设置为 yes:
nano /etc/default/fetchmail
# This file will be used to declare some vars for fetchmail # # Uncomment the following if you dont want localized log messages # export LC_ALL=C # Declare here if we want to start fetchmail. 'yes' or 'no' START_DAEMON=yes
接下来我们必须创建配置文件 /etc/fetchmailrc,因为如果该文件不存在,fetchmail 守护进程将无法启动。在这个文件中,我们可以指定 fetchmail 守护进程的行为方式,以及 fetchmail 从外部邮件账户检索邮件所需了解的细节。
假设 falko 有两个邮件账户,我们要从中检索邮件:
- 第一个账户:服务器 pop.someprovider.tld,协议 POP3,用户名 [email protected](是的,本例中用户名就是个邮件地址),密码 secret。
- 第二个账户:服务器 mail.otherprovider.tld,协议 POP3,用户名 ftimme,密码 verysecurepassword。
till 有一个邮件账户:
- 服务器 mailin.tillsprovider.tld,协议 POP3,用户名 tbrehm,密码 iwonttellyou。
因此我们的 /etc/fetchmailrc 文件可以如下所示:
nano /etc/fetchmailrc
# /etc/fetchmailrc for system-wide daemon mode
# This file must be chmod 0600, owner fetchmail
set daemon 300 # Pool every 5 minutes
set syslog # log through syslog facility
set postmaster root
set no bouncemail # avoid loss on 4xx errors
# on the other hand, 5xx errors get
# more dangerous...
##########################################################################
# Hosts to pool
##########################################################################
# Defaults ===============================================================
# Set antispam to -1, since it is far safer to use that together with
# no bouncemail
defaults:
timeout 300
antispam -1
batchlimit 100
poll pop.someprovider.tld protocol POP3 user "
[email protected]
" there with password "secret" is falko here
poll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" is falko here fetchall
poll mailin.tillsprovider.tld protocol POP3 user "tbrehm" there with password "iwonttellyou" is till here keep
在文件开头有一些全局选项,例如 set daemon 300(表示 fetchmail 应每 300 秒检索一次邮件),它们控制程序的运行方式。上述选项的含义如下:
- set daemon:设置后台轮询间隔(以秒为单位)。
- set syslog:通过 syslog 记录错误日志。
- set postmaster:指定最后的邮件接收者名称(默认值:运行 fetchmail 的用户,若由 root 用户运行则为 "postmaster")。
- set no bouncemail:把错误邮件发送给本地 postmaster(依据上面的 "postmaster" 全局选项)。
接下来是服务器和用户选项。这些选项一起写在以 poll 开头的行中;如果某些选项对每个 poll 行都相同,我们也可以把它们放在 poll 行之前、以 defaults: 开头的一段中统一指定(例如本例中的 timeout、antispam 和 batchlimit)。
- timeout:服务器无活动超时时间(以秒为单位,默认 300)。
- antispam:指定哪些 SMTP 返回值被解释为垃圾邮件策略拦截。
- batchlimit:指定在连接被有意断开并重建之前,发送给 SMTP 监听程序的最大邮件数量(默认 0,表示无限制)。
poll 行的含义不言自明;如你所见,fetchmail 从 falko 的两个外部邮件账户检索邮件,并把它们放进同一个账户。
你会注意到 poll 行有不同的结尾(例如 nofetchall(默认)、fetchall、keep、nokeep)。其含义如下:
- nofetchall:仅检索新邮件(默认)。如果未指定其他选项(例如 fetchall、keep),即表示 nofetchall。
- fetchall:检索所有邮件,无论是否已读。
- keep:不从服务器删除已读邮件。
- nokeep:从服务器删除已读邮件。
要了解更多关于所有可用配置设置的信息,请查看
man fetchmail
/etc/fetchmailrc 必须具有 600 权限,并且必须归用户 fetchmail 所有,因此我们执行以下操作:
chmod 600 /etc/fetchmailrc
chown fetchmail /etc/fetchmailrc
最后,我们可以启动 fetchmail:
/etc/init.d/fetchmail start
现在 fetchmail 应该会下载邮件,并把它们放进 falko 和 till 的邮箱(通过 MTA)。它将每隔 set daemon 所设的秒数重复一次此操作。
3.2 使用每用户配置文件并通过 Cron 运行 fetchmail
除了像 3.1 节那样使用全局配置文件外,我们也可以使用每用户配置文件。这些文件必须命名为 .fetchmailrc,并且必须位于用户的 homedir 中。
现在我们要为用户 falko 创建这样一个文件。请确保你是以 falko 身份登录,而不是 root!然后我们执行以下操作:
cd ~/
vi .fetchmailrc
set postmaster falko set bouncemail poll pop.someprovider.tld protocol POP3 user " [email protected] " there with password "secret" poll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" fetchall
该文件与 3.1 节的 /etc/fetchmailrc 文件非常相似,但你会注意到,这里我不再使用 is falko 这个短语(因为 .fetchmailrc 位于 falko 的 homedir 中,fetchmail 就知道邮件应投递给 falko)。当然,你在这里仍然可以使用 is falko,因此该文件也可以如下所示:
set postmaster falko set bouncemail poll pop.someprovider.tld protocol POP3 user " [email protected] " there with password "secret" is falko here poll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" is falko here fetchall
要了解更多关于所有可用配置设置的信息,请查看
man fetchmail
.fetchmailrc 必须具有 600 权限,以便只有 falko 能够读取/写入它:
chmod 600 ~/.fetchmailrc
就这样。现在 falko 可以通过运行来启动检索过程
fetchmail
或者
fetchmail -v
它会显示正在发生的情况。
当然,falko 不想每隔几分钟就手动启动检索,因此我们为他创建一个 cron 作业。仍然以 falko 用户身份,我们运行
crontab -e
并创建一个类似这样的 cron 作业(它会每五分钟启动一次 fetchmail):
*/5 * * * * /usr/bin/fetchmail &> /dev/null
4 链接
- Debian: http://www.debian.org
- Fetchmail: http://fetchmail.berlios.de