在 Debian 上使用 fetchmail 从远程服务器检索电子邮件
在此页
- 1 条初步说明
- 2 安装fetchmail
- 3 配置fetchmail
- 3.1 使用全局配置文件将 fetchmail 作为守护进程运行
- 3.2 使用每用户配置文件并通过 Cron 运行 fetchmail
Fetchmail 是一个用于从远程服务器检索电子邮件的程序。假设您在五个不同的服务器上有五个电子邮件帐户。当然,您不想连接到他们每个人来获取您的电子邮件。这就是 fetchmail 发挥作用的地方。如果你在 Linux 服务器上有一个用户帐户,你可以让 fetchmail 从远程服务器下载电子邮件并将它们放入一个邮箱(你的 Linux 用户的那个),从那里你可以用你的电子邮件客户端(例如 Thunderbird 或外表)。
或者假设您在一家不进行垃圾邮件和病毒过滤的提供商处拥有一个电子邮件帐户。在这种情况下,您可以使用 fetchmail 将邮件下载到您自己的服务器,并在使用电子邮件客户端下载邮件之前通过垃圾邮件和病毒过滤器(例如 SpamAssassin 和 ClamAV)将它们通过管道传输。
1 初步说明
您需要一台 Linux 服务器,系统用户可以接收电子邮件,这意味着必须在系统上安装 MTA,例如 Postfix 或 Sendmail。否则,fetchmail 将无法工作,因为它会尝试将下载的电子邮件传递给 MTA(Postfix、Sendmail 等),然后 MTA 将邮件传递到用户邮箱(您可以将系统配置为包含垃圾邮件和病毒在此过程中进行扫描,例如使用 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、用户名 [电子邮件保护](是的,在这种情况下用户名是电子邮件地址)、密码保密。
- 第二个帐户:服务器 mail.otherprovider.tld,协议 POP3,用户名 ftimme,密码 verysecurepassword。
直到有一个电子邮件帐户:
- 服务器 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 "" 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 postmaster:给出最后的邮件收件人的名字(默认:运行 fetchmail 的用户,如果由 root 用户运行则为 \postmaster\)。
- 设置无退回邮件:将错误邮件直接发送到本地邮局局长(根据上面的“邮局局长”全局选项)。
然后我们有服务器和用户选项选项。这些一起进入以 poll 开头的行;如果每个轮询行都有相同的选项,我们也可以在以默认值开头的部分中的轮询行之前指定它们:(例如我们示例中的超时、反垃圾邮件和 batchlimit)。
- 超时:服务器不活动超时(以秒为单位)(默认 300)。
- 反垃圾邮件:指定哪些 SMTP 返回被解释为垃圾邮件策略块。
- batchlimit:指定在有意断开和重建连接之前发送到 SMTP 侦听器的最大消息数(默认为 0,表示无限制)。
投票行是不言自明的;如您所见,fetchmail 从两个 falkos 外部电子邮件帐户中检索电子邮件并将它们放入一个帐户中。
您会注意到轮询行有不同的结尾(例如 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 现在应该下载电子邮件并将它们放入 falkos 和 tills 邮箱(使用 MTA)。它将在每个设定的守护程序秒数内重复此操作。
3.2 使用每个用户的配置文件并通过 Cron 运行 fetchmail
我们可以使用每个用户的配置文件,而不是使用第 3.1 章中所示的全局配置文件。这些必须具有名称 .fetchmailrc 并且必须位于用户主目录中。
我们现在想为用户 falko 创建这样一个文件。确保您以 falko 身份登录,而不是 root!然后我们这样做:
cd ~/
vi .fetchmailrcset postmaster falko set bouncemail poll pop.someprovider.tld protocol POP3 user "" there with password "secret" poll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" fetchall
该文件看起来与第 3.1 章中的文件 /etc/fetchmailrc 非常相似,但是您会注意到我在这里不再使用短语 is falko(因为 .fetchmailrc 在 falkos homedir 中,fetchmail 知道邮件应该发送到 falko) .当然,你仍然可以在这里使用 is falko,所以文件也可以是这样的:
set postmaster falko set bouncemail poll pop.someprovider.tld protocol POP3 user "" 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
- 获取邮件:http://fetchmail.berlios.de