如何使用 dockerized 邮件服务器从 Ubuntu 服务器发送邮件

How to send mail from Ubuntu server using dockerized mail server

我正在寻找一种简单的方法来以编程方式从 Linode Ubuntu 服务器发送电子邮件(不是群发或垃圾邮件,简单的 iOT 类型通知)。我有一个 docker 化的 postfix/dovecot 系统和 运行,但我不知道如何从容器外部使用它。我查看了 sendmail,但这似乎是重复的,因为我已经配置了 SMTP 服务器。我的问题是我可以在我的 Ubuntu 服务器上安装什么,使我能够从使用我 docker 容器中现有 SMTP 服务器的命令行(脚本)发送简单的电子邮件?

这类似于必须发送电子邮件的 Jenkins 容器,as described here:

For containerized Jenkins system, mail server can also be configured in same Manage Jenkins page, E-mail Notification section.
The only difference is the IP/hostname provided to SMTP server option. Instead of providing the known SMTP server’s IP and host, one should use the IP of docker0.

For corporate network, you may have to use an SMTP relay server instead. For those cases, you can configure SMTP communication by setting up Postfix.

After installing, update /etc/postfix/main.cf with correct relay information: myhostname, myorigin, mydestination, relayhost, alias_maps, alias_database.

但是:

需要对 Postfix 进行两项更改才能将其暴露给一台主机上的 Docker 个容器。

  1. Exposing Postfix to the docker network, that is, Postfix must be configured to bind to localhost as well as the docker network.
  2. Accepting all incoming connections which come from any Docker containers.

Docker bridge (docker0) acts a a bridge between your ethernet port and docker containers so that data can go back and forth.
We achieve the first requirement by adding the IP of docker0 to inet_iterfaces.

For the second requirement, the whole docker network as well as localhost should be added to mynetworks.

对于这个问题,我找到的最简单的解决方案是 nodemailer,因为我需要发送电子邮件的应用程序是 node.js 应用程序,我连接到它就像您从电子邮件客户端。