I'm looking for a simple way to programmatically send emails from a Linode Ubuntu server (not bulk or spamming, simple iOT type notifications). I have a dockerized postfix/dovecot system up and running, but I don't know how to use that from outside the container. I've looked into sendmail but that seems like duplication since I already have a configured SMTP server. My question is what can I install on my Ubuntu server that will allow me to send simple emails from the command line (script) that uses my existing SMTP server in my docker container?
2 Answers
This is similar to having a Jenkins container which must send emails, 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 ofdocker0
.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
.
But:
There are two changes need to be made on Postfix to expose it to Docker containers on one host.
- Exposing Postfix to the docker network, that is, Postfix must be configured to bind to localhost as well as the docker network.
- 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 toinet_iterfaces
.For the second requirement, the whole docker network as well as
localhost
should be added tomynetworks
.

- 1,262,500
- 529
- 4,410
- 5,250
-
Interesting. Might be something useful there, but it is the inverse of my problem - my postfix installation is the docker container, and I want to send from the host machine itself. I found a solution, see below. – svenyonson Jan 27 '19 at 16:55
-
1Thank you, this is what fixed the problem I had that brought me here — couldn't send emails out from a container. I also had to remove `loopback-only` from the `inet_interfaces` parameter. – Corwin Newall Jan 28 '20 at 04:15
For this problem, the easiest solution I have found is nodemailer
, as my application that needs to send the emails is a node.js application, and I connect to it as you would from an email client.

- 1,815
- 1
- 21
- 30