大网数据广告活动
服务器运维问答

Self-destructing recipient address on Postfix (rewrite recipient address)

· 52CE 我爱测

I'd like to implement self-destructing e-mail address rules on my Postfix mail server. The idea is to have public address like this: [email omitted] where 0220 in the address means "February 2020". If Postfix receives e-mail for …

问题原文

原帖链接 · bizwiz · CC BY-SA 4.0

I'd like to implement self-destructing e-mail address rules on my Postfix mail server. The idea is to have public address like this:

[email omitted]

where 0220 in the address means "February 2020". If Postfix receives e-mail for this address before 3/1/2020 it will be delivered to some non-public e-mail (e.g. [email omitted]) but after this date the e-mail is discarded (/dev/null).

Such system would allow me to have infinite number of temporary addresses.

Postfix allows regular expressions to match recipient address but how to do that conditional rewriting based on time?

已采纳解答

原帖链接 · anx · CC BY-SA 4.0

Make any suitable lookup table dynamic by adding a sufficiently dynamic lookup method, such as SQLite. If postfix itself delivers the mail (no dovecot etc), the alias table seems appropriate. I will demonstrate for local mail (analogous configuration possible for virtual).

You may keep your existing (e.g. hash:) lookup(s) and just add the new method:

alias_maps = hash:/etc/aliases sqlite:/etc/postfix/date-dependant-aliases.cf

You might need to install support for the new lookup type. I chose SQLite, because I do not need to setup a database if my query does not use one.

# /etc/postfix/date-dependant-aliases.cf
dbpath = /dev/null
expansion_limit = 1
query = WITH T (prio, forward_addr, condition) AS ( VALUES (1, '[email omitted]', '%s' >= ('john' || substr(strftime('%%Y%%m', 'now'),3))), (2, 'devnull', 1=1)) SELECT forward_addr FROM T WHERE '%s' LIKE 'john____' AND condition ORDER BY prio ASC LIMIT 1;

Explanation of the query (note local delivery agent inserts lowercase key for %s, other % need quoting):

WITH T (prio, forward_addr, condition)                    -- sub-query
AS ( VALUES                                               -- with these 2 rows:
(1, '[email omitted]',                              -- 1: real address,
'%s' >= ('john' || substr(strftime('%%Y%%m', 'now'),3))), -- if >= current months alias
    -- SQLite doesnt have 2-digit dates in strftime
(2, 'devnull', 1=1))                                      -- 2: discard, always
SELECT forward_addr FROM T                                -- postfix only needs the target
WHERE '%s' LIKE 'john____'                                -- require matching template
    -- no REGEXP support; just match 4 arbitrary chararacters
AND condition ORDER BY prio ASC LIMIT 1;                  -- select best match from sub-query

You should verify the query suits your needs using postmap:

postmap -q john$(date +'%y%m' -d '+1 year') sqlite:/etc/postfix/date-dependant-aliases.cf
[email omitted]
postmap -q john$(date +'%y%m') sqlite:/etc/postfix/date-dependant-aliases.cf
[email omitted]
postmap -q john$(date +'%y%m' -d '-31 days') sqlite:/etc/postfix/date-dependant-aliases.cf
devnull
postmap -q john1801 sqlite:/etc/postfix/date-dependant-aliases.cf
devnull

Note the propagate_unmatched_extensions parameter (see man 5 postconf and man 5 aliases), which changes behavior in case of address extensions (+foo) not previously matched.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。