Michał Tołkacz
Michał Tołkacz

Reputation: 131

OSError: [Errno 99] Address not available - sending e-mail from django docker app with gmail smtp

I'm trying to send an e-mail from my django application in docker and I'm getting following error:

OSError at /accounts/mail/
[Errno 99] Address not available
Request Method: GET
Request URL:    https://localhost:8000/accounts/mail/
Django Version: 2.2.5
Exception Type: OSError
Exception Value:    
[Errno 99] Address not available

Web server log:

web_1            | OSError: [Errno 99] Address not available
web_1            | [17/Sep/2019 19:21:35] "GET /accounts/mail/ HTTP/1.1" 500 108369

My environment: Ubuntu 18.04 Docker Django + Gunicorn Postfix

I have no problem to send an e-mail outside docker, locally. I suppose that there might be a problem with smtp port inside docker, but I don't know how to fix that. I tried with postfix - same error.

View code from django:

from django.core.mail import EmailMessage  # didn't work also with send_mail

def email(request):
    mail_subject = 'Activate your account'
    message = 'test'
    to_email = 'mail@mail'
    email = EmailMessage(
        mail_subject, message, to=[to_email]
    )
    email.send()
    return redirect('index')

Part of my docker-compose file.

version: '3.3'

services:
  web:
    build: ./app
    command: python manage.py runsslserver 0.0.0.0:8000
...
    ports:
      - 8000:8000
      - 587:587
      - 25:25
    env_file: .envdev
...
  postfix:
    image: juanluisbaptiste/postfix:latest
    expose:
    - "25"
    env_file:
      - .envpostfix
    restart: always
    volumes:
     - "/etc/localtime:/etc/localtime:ro"
...

Docker .env file:

EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_USE_TLS=True
EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_USER=***
EMAIL_HOST_PASSWORD=***
EMAIL_PORT=587

My postfix settings:

SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=***
SMTP_PASSWORD=***
SERVER_HOSTNAME=127.0.0.1

Upvotes: 4

Views: 2837

Answers (1)

Michał Tołkacz
Michał Tołkacz

Reputation: 131

Problem solved.

I've created MAIL variables in django settings instead of keeping only in docker .env file.

Upvotes: 1

Related Questions