5

I'm trying to send an e-mail from django app in docker on Ubuntu and I'm receiving following message:

Request Method: GET
Request URL:    https://localhost:8001/accounts/mail/
Django Version: 2.2.5
Exception Type: SMTPAuthenticationError
Exception Value:    
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials x76sm1225174ljb.81 - gsmtp')
Exception Location: /usr/local/lib/python3.7/smtplib.py in auth, line 642
Python Executable:  /usr/local/bin/python
Python Version: 3.7.4

There is no problem to send an e-mail outside docker.

I tried every step from Google troubleshooting steps. Currently I have 2-Step verification which works for local app but still doesn't for docker one.

I don't necessarily need Google SMTP (I have an account there), but what I what to achive is to send e-mail with activation link to user after registration for django application.

I tried without 2-factor auth - the same result. My docker-compose settings in web service:

services:
  web:
    build: ./app
    command: python manage.py runsslserver 0.0.0.0:8001
    stdin_open: true
    tty: true
    volumes:
      - ./app/:/usr/src/app/
      - /etc/localtime:/etc/localtime
      - /etc/timezone:/etc/timezone
    ports:
      - 8001:8001
      - 587:587
      - 25:25
      - 465:465

And code to send an e-mail (works outside the docker):

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

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example@example.com'  
EMAIL_HOST_PASSWORD = '***'  
EMAIL_PORT = 587
Michał Tołkacz
  • 111
  • 1
  • 2
  • 8
  • did you try with an account without 2-factor auth? that will narrow down the issue that can be happening. Also, make sure that the port that you are using is open from the docker outside. – NotSoShabby Sep 18 '19 at 14:52
  • Can you please post the code that you are trying to use to send the email? Please make sure to use dummy values for sensitive secrets. – 2ps Sep 18 '19 at 15:39
  • I've just added answers in main post to your questions – Michał Tołkacz Sep 18 '19 at 16:06
  • Thank you, I'll reply later tonight--I think the issue has to do with smtp and relay configuration for your django app inside gmail / gsuite. – 2ps Sep 18 '19 at 23:23

5 Answers5

2

After 30 May 2022 less secure apps solution is not working. because google stopped supporting less secure apps link

Solution

  • so you need to set up the App Password from setting.
  • This video shows how to set up App Password.
Ayush Rana
  • 31
  • 3
1

Problem solved! I've applied different code for sending e-mail.

import smtplib
import ssl

def email(request):
    port = settings.EMAIL_PORT
    smtp_server = settings.EMAIL_HOST
    sender_email = settings.EMAIL_HOST_USER
    password = settings.EMAIL_HOST_PASSWORD
    receiver_email = 'example@example.com'
    subject = 'Website registration'
    body = 'Activate your account.'
    message = 'Subject: {}\n\n{}'.format(subject, body)
    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.ehlo()  # Can be omitted
        server.starttls(context=context)
        server.ehlo()  # Can be omitted
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)
    return redirect('index')

I'm still wondering if this is the right way to pass variables from django settings. Am I doing it in elegant way and is it necessary? And is it the best way for smtp?

Michał Tołkacz
  • 111
  • 1
  • 2
  • 8
  • You have to check the email you are trying to send mail to. I received a critical security alert in my gmail that said some unauthorised program is trying to access my gmail. You will get the 'Yes,It was me ' option there. Click it and it should resolve the issue – Jdeep Jun 07 '20 at 13:21
0

Another potential solution is posted here.

I used Flask but the error was exactly the same. I made sure that my credentials were correct and the environment variables were not enclosed in quotes like this post suggested.

The short answer from the first post is to click this link while logged into the google account associated with the gmail you're sending from.

Google doesn't trust most programs to automatically login to your account, so they put the onus on the owner of the gmail to give "less secure apps" permission to access your gmail.

kidbilly
  • 670
  • 9
  • 9
0

I solved this problem by enabling less secure apps on the host Gmail account. Go Gmail security option and then: Allow less secure apps: ON

enter image description here

RiveN
  • 2,595
  • 11
  • 13
  • 26
Ridoy Roy
  • 11
  • 1
  • 5
0

Reason for this:

Less secure app access is no longer available.

Follow below steps:

  • Go to your google account and Active you 2-Steps Verification on Gmail.
  • Search App Password.
  • Choose other (Custom Name).
  • set a name Ex: Web App.
  • Copy password and paste in EMAIL_HOST_PASSWORD in Django setting Ex: EMAIL_HOST_PASSWORD = xxxxxxxxxx.