Unable to send emails in production while not having problems in development.

Hello guys, this thing is killing me. I have a django app deployed to a Google Cloud instance that has been sending emails without trouble via gmail in the past. I think it was due to a change in the settings file structure that the mailing stopped working. At the beginning I though it was a gmail security related issue, but after trying another email, I discovered it’s a general issue.

My settings file are split like so:

```
# base.py (production)

EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_HOST_IMAP = os.getenv('EMAIL_HOST_IMAP')
EMAIL_PORT = os.getenv('EMAIL_PORT')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_HOST_USER_MED = os.getenv('EMAIL_HOST_USER_MED')
EMAIL_HOST_PSSWD_MED = os.getenv('EMAIL_HOST_PSSWD_MED')
EMAIL_USE_TLS = True
```

```
# dev.py (development)    
from inventario.settings.base import *

DEBUG = True
SECRET_KEY = 'xxx'

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.abc.de'
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'xxx'
EMAIL_HOST_PASSWORD = 'xxx'
EMAIL_HOST_USER_MED = 'xxx'
EMAIL_HOST_PSSWD_MED = 'xxx'
EMAIL_HOST_IMAP = 'imap.abc.de'
EMAIL_PORT_IMAP = '587'
```

The wsgi application is running with the productions settings with no problem, and I’ve checked via the django shell that the following env vars are properly acquired by django:

# etc/profile.d/djangoenvs.sh
export SECRET_KEY='skey'
export EMAIL_HOST_USER_MED='xxx'
export EMAIL_HOST_PSSWD_MED='xxx'
export EMAIL_HOST='smtp.abc.de'
export EMAIL_PORT='587'
export EMAIL_HOST_USER='xxx'
export EMAIL_HOST_PASSWORD='xxx'
export EMAIL_HOST_IMAP='imap.abc.de'

I have two different functions sending emails, as shown here: https://del.dog/emefubabig. The first function doesn’t even send the email, but the second one just fails at copying the email to the Sent folder after successfully sending the email.

The errors I’m getting are these:

[2020-06-22 10:35:39,762: ERROR/ForkPoolWorker-1] Task gestion.tasks.solicitud_proforma[c253a118-7421-4786-b5bd-7c$
Traceback (most recent call last):
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/celery/app/trace.py", line 385, in trace_ta$
    R = retval = fun(*args, **kwargs)
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/celery/app/trace.py", line 648, in __protec$
    return self.run(*args, **kwargs)
  File "/home/jesuspm92/webapps/inventario/gestion/tasks.py", line 59, in solicitud_proforma
    subject,
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/django/core/mail/__init__.py", line 60, in $
    return mail.send()
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/django/core/mail/message.py", line 291, in $
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 10$
    new_conn_created = self.open()
  File "/home/jesuspm92/webapps/inventario/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 70$
    self.connection.login(self.username, self.password)
  File "/usr/lib/python3.6/smtplib.py", line 697, in login
    "SMTP AUTH extension not supported by server.")
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.

File "/home/jesuspm92/webapps/inventario/medicina/views.py" in Pizarra
  69.             email(pedido)

File "/home/jesuspm92/webapps/inventario/medicina/functions.py" in email
  74.     imap = imaplib.IMAP4(settings.EMAIL_HOST_IMAP)

File "/usr/lib/python3.6/imaplib.py" in __init__
  198.         self.open(host, port)

File "/usr/lib/python3.6/imaplib.py" in open
  299.         self.sock = self._create_socket()

File "/usr/lib/python3.6/imaplib.py" in _create_socket
  289.         return socket.create_connection((host, self.port))

File "/usr/lib/python3.6/socket.py" in create_connection
  724.         raise err

File "/usr/lib/python3.6/socket.py" in create_connection
  713.             sock.connect(sa)

Exception Type: ConnectionRefusedError at /medicina/
Exception Value: [Errno 111] Connection refused

As I told you, mails are properly sent in development. Also I have no errors when using django shell.

Can someone please enlighten me? What is wrong?

This problem makes the app unusable, since it relies in emailing.

Thanks a lot for your time!

Jesús.

It seems you a different configuration between prod and dev environments. I don’t know why your production server does not support authentication. An other solution which could work would be removing your credentials in production env.

Yes, it looks like that, but I have no idea what is missing, the settings variables are properly asigned and read, so it must be something else related to auth. I hope some django-developer-angelofsalvation appears around here and elighten me…this is driving me crazy.

Thanks!

But, if smtp authentification is not available on your production server, try to remove your credentials in your conf files.

Hi ngle, which credentials should I remove? How am I supposed to login then?
Also, authentication is indeed available in the production server, since I am able to send successfully mails from the django shell.

Hi.

I said this option, because it was recommended in another topic, with successfull.
I don’t know what you have this error only in production environment, but it appears issue is not on Django’s side. Also, a view to your maillog could be usefull.

EDIT / Out of Subjet : thanks, all smtp servers don’t require authentification :slight_smile: