如何在使用postfix的时候去掉或重写发件人的发件人地址?

目的

我试图configuration一个CentOS 7系统,以便所有发送到本地系统帐户的邮件都被redirect到一个备用的邮件地址。 例如,如果邮件发送到root我希望它被redirect到[email protected]

我有一个主要工作configuration使用后缀,通用地图和别名。 如果我正在使用的完整configuration将有所帮助,请询问我将修改问题以包含它。

如果有更好的办法来达到我的目标,请告诉我。 我宁愿坚持使用后缀,但只是因为它是CentOS上的默认MTA。

问题

我正在尝试将postfixconfiguration为智能主机,以便通过ZOHO帐户中继邮件。 不过,ZOHO 发件人的格式非常挑剔。

当我尝试发送带有如下所示的发件人地址的邮件时:

 [email protected] 

但是,当我尝试发送带有如下所示的发件人地址的邮件时,它将失败:

 [email protected] (Ryan) 

例子

以下是使用完全相同的后缀configuration发送的4个示例。

允许

我使用这个命令:

 mail -r "[email protected]" -s "Testing." root <<< "Test to root." 

…在maillog看到这个(我把信息删除了,并重新格式化,使其更具可读性):

 CD9FA29D20: from=<[email protected]> CD9FA29D20: to=<[email protected]>, orig_to=<root>, relay=smtp.zoho.com[74.201.154.90]:587, delay=2.4, delays=0/0.02/1.7/0.66, dsn=2.0.0, status=sent (250 Message received) 

允许

我使用这个命令:

 mail -r "Ryan <[email protected]>" -s "Testing." root <<< "Test to root." 

…在maillog看到这个(我把信息删除了,并重新格式化,使其更具可读性):

 936C929D20: from=<[email protected]> 936C929D20: to=<[email protected]>, orig_to=<root>, relay=smtp.zoho.com[74.201.154.90]:587, delay=2.3, delays=0/0.03/1.6/0.7, dsn=2.0.0, status=sent (250 Message received) 

中继拒绝

我使用这个命令:

 mail -r "[email protected] (Ryan)" -s "Testing." root <<< "Test to root." 

…在maillog看到这个(我已经删除了消息并重新格式化,使它更具可读性):

 D57C529D20: from=<[email protected]> D57C529D20: to=<[email protected]>, orig_to=<root>, relay=smtp.zoho.com[74.201.154.90]:587, delay=2, delays=0.01/0/1.4/0.64, dsn=5.0.0, status=bounced (host smtp.zoho.com[74.201.154.90] said: 553 Relaying disallowed as [email protected] (Ryan) (in reply to end of DATA command)) 

中继拒绝

我使用这个命令:

 mail -s "Testing." root <<< "Test to root." 

…在maillog看到这个(我把信息删除了,并重新格式化,使其更具可读性):

 9CFE629D20: from=<[email protected]> 9CFE629D20: to=<[email protected]>, orig_to=<root>, relay=smtp.zoho.com[74.201.154.90]:587, delay=2, delays=0/0.02/1.4/0.6, dsn=5.0.0, status=bounced (host smtp.zoho.com[74.201.154.90] said: 553 Relaying disallowed as [email protected] (Ryan) (in reply to end of DATA command)) 

这是我遇到麻烦的最后一个例子,因为默认情况下,如果没有明确包含地址,就会以ZOHO拒绝的方式写入地址。

重写发件人地址时是否可以有后缀条或重写一个人的名字? 这里有一个类似的问题,但它不能完成我想要的。 我不想依靠更改用户帐户设置,以确保我的邮件不会反弹。

邮件服务器需要一个有效的电子邮件地址并拒绝无效的邮件地址是相当普遍的。

  [email protected] (Ryan) 

不是有效的电子邮件地址。 维基百科条目有一些有用的指针,以及对相关RFC的引用。

在实践中,您可以使用以下电子邮件地址:

 [email protected] 

或者用< >括起来的任何显示名称,例如:

 Ryan <[email protected]> 

解决这个问题的关键是理解后缀cleanup过程如何处理丢失的头信息。 从清理手册页:

 The cleanup(8) daemon always performs the following transformations: o Insert missing message headers: (Resent-) From:, To:, Mes- sage-Id:, and Date:. 

一个旧的邮件列表文章给出了关于插入的From标题格式的提示:

 The defaults are: MAIL FROM address = UNIX login name FROM: header = UNIX login name (GECOS information) 

由于这是需要更改或重新格式化的标题信息,使用postfix的header_checks看起来就像是一个明显的解决scheme。 但是,它不起作用。 阅读header_checks手册页 ,在BUGS部分有另一个提示。

 Message headers added by the cleanup(8) daemon itself are excluded from inspection. Examples of such message headers are From:, To:, Mes- sage-ID:, Date:. 

有另一个邮件列表线程与其他人试图解决相同的问题,他们最终使用smtp_header_checks 。 对于smtp_header_checks没有太多的文档,但是,由于在通过SMTP传递邮件时应用了处理,所以可以用它来重写通过cleanup添加的From头。

下面是一个/etc/postfix/smtp_header_checksconfiguration的例子,它将把每个外出消息From (标题)地址改为LOCALHOST System <[email protected]>

 /^From:.*/ REPLACE From: LOCALHOST System <[email protected]> 

请注意,我不是一个正则expression式专家,所以你可能要testing自己的正则expression式,而不是依靠上面的那个。

添加smtp_header_checks ,必须将以下configuration选项添加到/etc/postfix/main.cf

 smtp_header_checks = pcre:/etc/postfix/smtp_header_checks 

然后重新加载postfixconfiguration(假设systemd )并发送testing消息:

 sudo systemctl reload postfix 
 sendmail [email protected] << EOF Subject: Test email to root. Testing. EOF