跳转至内容

fdm

来自 ArchWiki

fdm (fetch and deliver mail,抓取和投递邮件),是一个简单的用于投递和过滤邮件的程序。与其他同类应用程序相比,它与 Mutt 非常专注的设计原则有相似之处。

安装

安装 fdm 包。

配置

fdm 可以通过配置文件进行配置。默认是 ~/.fdm.conf,如果不存在,则为 /etc/fdm.conf

有关详细信息,请参阅手册页 fdm(1)fdm.conf(5) 以及 /usr/share/doc/fdm-git/MANUAL

mbox

Alpine 使用 mbox 格式,所以我们需要设置一些我们将要编辑的文件

$ cd
$ mkdir mail
$ touch mail/INBOX .fdm.conf 
$ chmod 600 .fdm.conf mail/INBOX

maildir

Mutt 偏好首字母大写的邮件目录,并且能够使用 maildir 格式。如果你打算使用 Mutt,请进行以下设置

$ cd
$ touch .fdm.conf; chmod 600 .fdm.conf
$ mkdir -p Mail/INBOX/{new,cur,tmp}

配置示例

编辑 .fdm.conf,并添加你的电子邮件账户和基本过滤规则。使用 mbox 或 maildir,但不能同时使用。

.fdm.conf
## Accounts and rules for:
#> foo@example.com
#> bar@gmail.com
## Last edit 21-Dec-09
 
# Catch-all action (mbox):
action "inbox" mbox "%h/mail/INBOX"
# Catch-all action (maildir):
# action "inbox" maildir "%h/Mail/INBOX"
 
account "foo" imaps server "imap.example.com"
	user "foo@example.com" pass "supersecret"
 
account "bar" imaps server "imap.gmail.com"
	user "bar@gmail.com" pass "evenmoresecret"
 
# Match all mail and deliver using the 'inbox' action.
match all action "inbox"

这将从列出的账户收集邮件,并将其投递到我们创建的 INBOX 文件夹。请参阅 fdm(1) 手册页,了解如何连接到其他类型的邮件服务器(例如 POP3)的细节。

提示 你也可以在 .netrc 文件中指定你的登录名和/或密码。

密码管理

电子邮件账户的密码可以明文存储在配置文件中,或者通过 shell 命令从密码管理器或密钥环请求,格式为 $(cmd ...)

使用密码管理器

以下是一个使用 pass 的示例

account "foo" imaps server "imap.example.com"
    user "foo@example.com" pass $(pass foo-email-entry | head -1)

OAuth2 认证,gmail

fdm 可以使用 GMail 所需的 OAuth2 访问令牌,但它缺乏续订和/或授权 OAuth2 凭据的能力。一个全面的解决方案是使用 oama 工具,它为 IMAP/SMTP 客户端提供了续订和授权 OAuth2 凭据的功能。初始设置后,访问令牌的续订会在后台自动进行,用户无需感知。

安装 oama-binAUR 包并配置 fdm

account "foo" imaps server "imap.gmail.com"
    user "foo@gmail.com"
    pass $(oama access foo@gmail.com)
    oauthbearer

通过 cron 运行

虽然上述示例对于交互式使用效果很好,但从 cron 作业运行它们会更复杂一些,因为调用的一些程序可能期望特定的环境。可以通过使用包装脚本和导出所需的环境变量来解决这个问题。

以下是这类脚本的一些技巧

...
# for pass
export PASSWORD_STORE_DIR=~/.local/var/password-store
...
# when using gnome's keyring
# secret-tool can't live without these two envvars
# so we fake them for the case this script runs in a cron job
export DISPLAY=${DISPLAY:-:0}
export DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/$UID/bus}
...

测试

一旦你对设置满意,请尝试手动运行 fdm 来收集你的邮件。

$ fdm -kv fetch

这样可以在发生错误时,将邮件保留在服务器上不受干扰。查看输出,确保一切都按计划进行。打开你喜欢的邮件阅读器(MUA),并确保你能阅读刚刚投递的邮件。

扩展用法

附加过滤

如果你想让来自特定账户的邮件 go 到一个特定的邮箱,你可以将以下行添加到你的 fdm.conf 文件中。从上面的配置文件来看,如果你想将来自 bar@gmail.com 的邮件过滤到它自己的文件夹 bar-mail,你会在现有的 "action" 行下方添加以下内容

action bar-deliver mbox "%h/mail/bar-mail"

如果需要,将 mbox 改为 maildir,并确保路径正确。

要激活新的 action,请在现有的 "match" 行之前添加此内容

match account bar action bar-deliver

然后,所有发往 bar@gmail.com 的邮件都将被放入 bar-mail 邮件文件夹。

自动化

由于 fdm 不作为守护进程运行,所以定时抓取邮件的工作留给了 cronsystemd timers 等作业调度器。本节将展示这两种实现的方案。

Cron

每 15 分钟抓取和排序来自所有账户的邮件,并将所有匹配项的日志追加到本地文件

$ crontab -e
...
*/15 * * * * fdm fetch >> $HOME/[Mm]ail/fdm.log

systemd timer

设置本地用户的 fdm 服务来抓取和排序所有账户的邮件

${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/fdm.service
[Unit]

Description=Fetch mail using fdm
After=network.target network-online.target dbus.socket
Documentation=man:fdm(1)

[Service]
Type=oneshot
ExecStart=/usr/bin/fdm fetch

然后设置 timer 每 15 分钟运行一次服务

${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/fdm.timer
[Unit]
Description=Fetch mail using fdm

[Timer]
# Uncomment to run the service two minutes after booting
# OnBootSec=2m
OnUnitActiveSec=15m
Persistent=true

[Install]
WantedBy=timers.target

最后,启动/启用用户单元 fdm.timer

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.