由于工作需要,需要改寫用戶郵件header頭地址與郵件路由地址。整理筆錄
改寫郵件地址是postfix的核心,在收到郵件后就會(huì)進(jìn)行相應(yīng)地址修改,地址補(bǔ)充等工作,其流程如下:以下來自postfix官網(wǎng)說明
Postfix address rewriting overview
|
trivial-
rewrite(8)
(std form)
|
|
trivial-
rewrite(8)
(resolve)
|
|
|
|
|
|
smtpd(8)
|
>-
|
cleanup(8)
|
->
|
incoming
|
->
|
active
|
->
|
qmgr(8)
|
-<
|
smtp(8)
|
qmqpd(8)
|
lmtp(8)
|
pickup(8)
|
local(8)
|
|
^
|
|
|
|
|
|
bounces
forwarding
notices
|
|
deferRED
|
|
|
|
由于工作只需要修改郵件header地址,著重只測(cè)試了canonical_maps,smtp_generic_maps,header_checke部分修改郵件頭地址的方法
。
1.用header_checker檢查頭文件,用正則匹配替換發(fā)件人地址From
如將user01@test.com發(fā)件地址替換成user01@ct.com
Vim main.cf
添加如下開啟頭檢查,使用的是pcre方式。
header_checks = pcre:/etc/postfix/my_header_checks
建立my_header_checks文件
/^From:(.*)[<]([\w\.\-]+)\@test\.com[>]/i REPLACeFrom:$1<$2@ct.com>
記得每次修改完my_header_checks文件要重新加載postfix否則出現(xiàn)正則是對(duì)的,而匹配出的地址格式顯示是錯(cuò)誤的
Service postfix reload
以上方法只配置了From:部分,而To:部分如何寫正則,真的不好寫。
2.第二種方法則是postfix的smtp_generic_maps參數(shù)設(shè)置。類似于sendmail的地址偽裝功能,可以將本地網(wǎng)域的郵件地址改寫成internet上合法的郵件域名地址。smtp_generic_maps
只作用于外發(fā)的需要SMTP的郵件,本地域的內(nèi)郵件收發(fā),地址是不會(huì)修改的。smtp_generic_maps
如將user01@domain.local轉(zhuǎn)換改寫成user01@domain.com郵件網(wǎng)關(guān)網(wǎng)域地址
文件設(shè)置如下
配置postfix添加
Vim main.cf
smtp_generic_maps = hash:/etc/postfix/my_generic_maps
vim my_generic_maps
user01@domain.local user01@domain.com
@localdomain.local @hisisp.example
建立文件后需要postmap生產(chǎn)hash數(shù)據(jù)文件
postmap /etc/postfix/my_generic_maps
service postfix reload
該參數(shù)會(huì)修改掉郵件header的路由、From:、To:相關(guān)郵件地址信息
它作用范圍,只會(huì)修改掉需要發(fā)送出去到別臺(tái)郵件
服務(wù)器的郵件地址相關(guān)信息,local郵件不影響。
3.還可以使用郵件地址規(guī)范改寫參數(shù)canonical_maps實(shí)現(xiàn)郵件地址改寫:
Canonical_maps的作用區(qū)域local與smtp所有郵件,可以用來規(guī)范郵件地址,
Firstname.Lastname 風(fēng)格的地址以及清除無效的域。
缺省postfix是不進(jìn)行規(guī)范地址改寫的,你可以通過指定canonical_maps參數(shù)的值來使其生效。如:
canonical_maps = hash:/etc/postfix/canonical
Vim canonical
wyjos@test.com jos.wang@test.com
@testmail.com @test.com
地址改寫可以基于單個(gè)郵件地址,也可以基于整個(gè)域設(shè)定。
也可以分別為收件人和發(fā)件人地址分別指定不同的改寫規(guī)范,這時(shí)參數(shù)sender_canonical_maps和recipient_canonical_maps的優(yōu)先級(jí)比canonical_maps高。如:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
注意:基于以上測(cè)試時(shí)我發(fā)現(xiàn),用webmail發(fā)送的郵件,對(duì)方收到的郵件顯示地址為改寫后的地址(改寫成功),但我用OUTLOOK2007發(fā)送的郵件對(duì)方收到的郵件顯示地址并沒有被改寫。查看header頭文件路由Delivered-To:路由信息已被修改但是From:與To:部分的地址沒有被改。,
查閱postfix 官網(wǎng)有提到如下注意,而網(wǎng)上與postfix指南都沒有這個(gè)說明,導(dǎo)致這個(gè)問題折騰了我很久。
NOTE: Postfix versions 2.2 and later rewrite message headers from remote SMTP clients only if the client matches the local_header_rewrite_clients parameter, or if the remote_header_rewrite_domain configuration parameter specifies a non-empty value. To get the behavior before Postfix 2.2, specify "local_header_rewrite_clients = static:all".
郵件地址改寫作用范圍是受local_header_rewrite_clients 設(shè)定控制的。默認(rèn)只是改寫
local_header_rewrite_clients (default: permit_inet_inte-ces)
permit_inet_inte-ces只作用于
append the domain name in $
myorigin or $
mydomain when the client IP address matches $
inet_inte-ces. This is enabled by default.
我們可以自定義可以郵件地址改寫的作用范圍:
local_header_rewrite_clients = permit_mynetworks,
permit_sasl_authenticated permit_tls_clientcerts
check_address_map hash:/etc/postfix/pop-before-smtp
我想任何符合canonical表的郵件無論誰發(fā)送的都改寫,只需在main.cf添加上,就可以了。
local_header_rewrite_clients = static:all