Django 从 docker 容器发送邮件如何公开端口

Django send mail from docker container how to expose port

没有 docker 容器邮件发送工作正常。我想我实际上打开了 SMTP 端口。这是我的 docker-compose 文件:

version: "3.9"

services:
  db:
    <some postgres setup>
  api:
    build: .
    command: python manage.py runserver 0.0.0.0:8000 --settings=api.settings.production
    volumes:
      - .:/code
    ports:
      - "8000:8000"
      - "587:587"
      - "25:25"
    expose:
      - 587
      - 25
    env_file:
      - ./.env.dev
    depends_on:
      - db
    entrypoint: /entrypoint.sh
  mail:
    build: .
    command: python manage.py qcluster --settings=api.settings.production
    depends_on:
      - api

无论是用django q作为异步任务发送还是直接用django.core.mail.send_mail

发送邮件都不起作用

这是我的邮件 settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'mail'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

我得到一个 OSError OSError: [Errno 99] Cannot assign requested address

问题是端口被我的wifi屏蔽了。