开始按http://semi-legitimate.com/blog/item/how-to-rewrite-outgoing-address-in-postfix 博客中的方法进行设置,是可以替换,但是webmail上面可替换。
下面引用该博客的
Sometimes I find myself configuring an internal Linux machine to be able to send emails for alerts or from a
particular application. Since this will not be a primary mail server, I just want to rewrite the outgoing
address to be something that make sense with the proper domain for the users. Here are the quick steps to accomplish this: vi /etc/postfix/main.cf Modify the "mydomain" variable to your email domain mydomain = example.com Make sure to uncomment the proper network interface. I'm usually lazy and just do all. inet_interfaces = all Now at the bottom of this file you need to specify the generic map file for rewriting the address. smtp_generic_maps = hash:/etc/postfix/generic Save and exit main.cf. Now we need to edit /etc/postfix/generic vi /etc/postfix/generic You need to specify the mapping of the original address to the one you want. Since in this case I just
want to rewrite everything I usually add the following two lines and it seems to catch everything. root@example.com no-reply@example.com @example.com no-reply@example.com Save and exit the file. Now we need to create the postfix db. postmap /etc/postfix/generic This will create the /etc/postfix/generic.db hash file. Now just restart postfix and test. /etc/init.d/postfix restart EDIT: umm..so I was doing this on an old version of Red Hat/Postfix 2.0 and my own directions
didn't work. (embarrassing...) Turns out you have to use a different file. Instead of using smtp_generic_maps,
use sender_canonical_maps like so: sender_canonical_maps = hash:/etc/postfix/canonical Then /etc/postfix/canonical is setup just like the generic file example above. Use the postmap /etc/postfix/canonical Restart postfix and test.
再次google. 找到http://itdept.blog.51cto.com/1034307/1104223
在此很感谢itdept,因为你的此细而帮助了我。我自认写不到你那么仔细。
开始我也一样,准备开始使用正则。来达到目的。但是最后没有最后了。
1.用header_checker检查头文件,用正则匹配替换发件人地址From 如将user01@test.com发件地址替换成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否则出现正则是对的,而匹配出的地址格式显示是错误的 Service postfix reload 以上方法只配置了From:部分,而To:部分如何写正则,真的不好写。
EDIT: umm..so I was doing this on an old version of Red Hat/Postfix 2.0 and my own directions didn't work. (embarrassing...) Turns out you have to use a different file. Instead of using smtp_generic_maps,
use sender_canonical_maps like so:
2.第二种方法则是postfix的smtp_generic_maps参数设置。类似于sendmail的地址伪装功能,可以将本地网域的邮件地址改写成internet上合法的邮件
域名地址。smtp_generic_maps 只作用于外发的需要SMTP的邮件,本地域的内邮件收发,地址是不会修改的。smtp_generic_maps 如将user01@domain.local转换改写成user01@domain.com邮件网关网域地址 文件设置如下 配置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生产hash数据文件 postmap /etc/postfix/my_generic_maps service postfix reload 该参数会修改掉邮件header的路由、From:、To:相关邮件地址信息 它作用范围,只会修改掉需要发送出去到别台邮件服务器的邮件地址相关信息,local邮件不影响。
3.还可以使用邮件地址规范改写参数canonical_maps实现邮件地址改写:
Canonical_maps的作用区域local与smtp所有邮件,可以用来规范邮件地址,
Firstname.Lastname 风格的地址以及清除无效的域。
缺省postfix是不进行规范地址改写的,你可以通过指定canonical_maps参数的值来使其生效。如:
canonical_maps = hash:/etc/postfix/canonical
canonical_maps = hash:/etc/postfix/canonical
Vim canonical
wyjos@test.com jos.wang@test.com
@testmail.com @test.com
地址改写可以基于单个邮件地址,也可以基于整个域设定。
也可以分别为收件人和发件人地址分别指定不同的改写规范,这时参数sender_canonical_maps和recipient_canonical_maps的优先级比canonical_maps高。如:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
也可以分别为收件人和发件人地址分别指定不同的改写规范,这时参数sender_canonical_maps和recipient_canonical_maps的优先级比canonical_maps高。如:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
看到itdept的测试,不得不说,哎,可惜没有早看到你的博客,我测试了许久,也看到了这个问题。最终在main.cf文件中添加
local_header_rewrite_clients = static:all
来处理了。
注意:基于以上测试时我发现,用webmail发送的邮件,对方收到的邮件显示地址为改写后的地址(改写成功),但 我用OUTLOOK2007发送的邮件对方收到的邮件显示地址
并没有被改写。查看header头文件路由Delivered-To:路由信息已被修改但是 From:与To:部分的地址没有被改。, 查阅postfix 官网有提到如下注意,而网上与postfix指南都没有这个说明,导致这个问题折腾了我很久。 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 设定控制的。默认只是改写 local_header_rewrite_clients (default: permit_inet_interfaces) permit_inet_interfaces只作用于 Append the domain name in $myorigin or $mydomain when the client IP address matches $inet_interfaces. 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表的邮件无论谁发送的都改写,只需在main.cf添加上,就可以了。 local_header_rewrite_clients = static:all
最终简化的结果为:
邮件头翻译操作步骤
1./etc/postfix/main.cf 添加 smtp_generic_maps = hash:/etc/postfix/generic sender_canonical_maps = hash:/etc/postfix/canonical local_header_rewrite_clients = static:all 2.新建 /etc/postfix/generic 文件,建好复制一份为canonical内容格式如下 需要被替换的邮箱地址 目的邮箱地址 root@example.com no-reply@example.com 3.postmap /etc/postfix/generic 4. postmap /etc/postfix/canonical 5.最好重启下postfix的服务service umail_postfix restart