Docker django + host posfix gives `Bad destination mailbox address: Address not recognized by gateway`

I'm trying to persuade containerized django to send emails using the postfix installation on my host, and I'm told Bad destination mailbox address: Address not recognized by gateway.

(For more detail, the django application I'm running is docker-zulip.)

When I trigger a password-recovery email, I expect the email to be sent, but my server log in the container (/var/log/zulip/server.log) reports:

2025-09-01 22:47:00.280 ERR  [zulip.send_email] Error sending password_reset email to ['User <user@gmail.com>'
]: {'User <user@gmail.com>': (550, b'5.1.1 Bad destination mailbox address: Address not recognized by gateway.
')}

I'm aware of the SO post How do you configure Django to send mail through Postfix?, and I'm using its prescribed settings. But that post doesn't focus on docker setups, so it looks as if something further is required.

Can the host send emails? Yes

I can send emails from the host easily enough using echo "BODY" | mailx -r test@example.com -s "SUBJECT" user@gmail.com.

Is postfix configured to recognize traffic from the container? I think so

I've configured /etc/postfix/main.cf in a way that I think supports my docker container (running on 172.18.0.100):

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.18.0.0/24
inet_interfaces =  172.17.0.1 172.18.0.1 172.23.0.1

...and I've run systemctl restart postfix.

I don't see any activity in the host logs /var/log/mail.{err,log}, though.

I've found the solution: in the email settings, instead of specifying localhost for the email host, you should specify the address of your host as it appears on the container's network.

In this instance, I can get this address executing commands on the host (not container):

docker network ls | grep -oP '\w*zulip\w*' # => zulip_default
docker network inspect zulip_default | grep Subnet # => "Subnet": "172.18.0.0/24"
ip a | grep 172.18.0. # => inet 172.18.0.1/24 brd ...

So from my container's point of view, the address of my host (where postfix is running) is 172.18.0.1.

So I update my docker-zulip's docker-compose.yml file to hold

SETTINGS_EMAIL_HOST = '172.18.0.1'
SETTINGS_EMAIL_PORT = 25
SETTINGS_EMAIL_HOST_USER = ''
SETTINGS_EMAIL_USE_TLS = False

Now the next time I start up the container, I can get zulip to successfully send my password-recovery email.

Back to Top