将所有本地邮件发送到单个外部电子邮件

将所有本地邮件发送到单个外部电子邮件

我正在尝试使用 Google 的 SMTP 服务器将所有本地邮件发送到单个外部电子邮件。

使用 Google 的 SMTP 服务器可以工作,因为我可以使用来自 mailutils 的邮件将邮件发送到外部地址。

将本地邮件重定向到外部电子邮件不起作用。当从我的帐户测试发送邮件到 root 时,使用:

echo "Body" | mail -s "Test Postfix To Root" root
  • 我从来没有收到消息
  • /var/log/mail.err 中没有出现任何新内容
  • 这出现在 /var/log/mail.log 中
Sep  4 18:48:06 desktop1204test postfix/pickup[5535]: C9326EE26: uid=1000 from=
Sep  4 18:48:06 desktop1204test postfix/cleanup[5702]: C9326EE26: message-id=
Sep  4 18:48:06 desktop1204test postfix/qmgr[5534]: C9326EE26: from=, size=401, nrcpt=1 (queue active)
Sep  4 18:48:06 desktop1204test postfix/local[5704]: C9326EE26: to=, orig_to=, relay=local, delay=0.03, delays=0.02/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Sep  4 18:48:06 desktop1204test postfix/qmgr[5534]: C9326EE26: removed

我的/etc/postfix/main.cf

inet_interfaces = loopback-only
mynetworks = loopback-only
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
virtual_alias_domains = localhost.localdomain
virtual_alias_maps = hash:/etc/postfix/virtual
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

我的etc/aliases

# See man 5 aliases for format
postmaster:    root

我的/etc/postfix/virtual:

@localhost.localdomain        [email protected]

我怎样才能让它工作?我不希望任何本地邮件到达本地,它们都应该发送至[email protected].


我尝试了下面评论中建议的解决方案:放入.但没有工作:*: [email protected]/etc/aliases

test@desktop1204test:~$ sudo newaliases
test@desktop1204test:~$ cat /etc/aliases
# See man 5 aliases for format
postmaster:    root
*: [email protected]
test@desktop1204test:~$ sudo newaliases 
test@desktop1204test:~$ sudo service postfix restart
 * Stopping Postfix Mail Transport Agent postfix                                                     [ OK ] 
 * Starting Postfix Mail Transport Agent postfix                                                     [ OK ] 
test@desktop1204test:~$ echo "Body" | mail -s "Test Postfix To Root $(date)" root
test@desktop1204test:~$ tail /var/log/mail.err 
test@desktop1204test:~$ tail /var/log/mail.log 
......
Sep  4 22:46:12 desktop1204test postfix/master[7224]: daemon started -- version 2.9.6, configuration /etc/postfix
Sep  4 22:46:23 desktop1204test postfix/pickup[7227]: 859AFF6A8: uid=1000 from=<test>
Sep  4 22:46:23 desktop1204test postfix/cleanup[7235]: 859AFF6A8: message-id=<[email protected]>
Sep  4 22:46:23 desktop1204test postfix/qmgr[7228]: 859AFF6A8: from=<[email protected]>, size=431, nrcpt=1 (queue active)
Sep  4 22:46:23 desktop1204test postfix/local[7237]: 859AFF6A8: to=<[email protected]>, orig_to=<root@desktop1204test>, relay=local, delay=0.02, delays=0.02/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Sep  4 22:46:23 desktop1204test postfix/qmgr[7228]: 859AFF6A8: removed
test@desktop1204test:~$

答案1

(这是在 AskUbuntu 上回答的帕西·索米宁,非常感谢他!)

这可以通过虚拟正则表达式(/etc/postfix/virtual-regexp)来完成

/.+@.+/ [email protected]

然后在main.cf中:

virtual_maps = hash:/etc/postfix/virtual, regexp:/etc/postfix/virtual-regexp

和地图文件:

postmap /etc/postfix/virtual-regexp

这应该可以解决所有本地邮件的问题(否则您必须指定虚拟地址)

答案2

对所有根邮件使用一封电子邮件转发:
echo "your_email_address" >> /etc/aliases && newaliases并使用以下命令进行测试:
echo "body" | mail -s "subject" root

现在,所有发送到 root 的应用程序都将发送到您的电子邮件。更多信息请点击这里:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-16-04#step-3-测试 smtp 服务器

相关内容