0

I'm trying to send an email through Django's wrapper.

Here are my relevant settings.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_USE_TLS = True

EMAIL_HOST = 'smtp.gmail.com'

EMAIL_HOST_USER = 'myemail@gmail.com'

EMAIL_HOST_PASSWORD = 'mypassword'

EMAIL_PORT = 587

DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

The Email, which I am trying to isolate in the most basic form of a view:

from django.core.mail import send_mail

def index(request):

    subject = 'Subject'
    message = 'message'
    from_email = settings.DEFAULT_FROM_EMAIL

    send_mail(subject, message, from_email, ['email@example.com'])

    return render(request, "index.html")

All of the emails and password are legitimate. When I execute the code, I get thrown an error message:

SMTPAuthenticationError at /....*Link to sign into my account*
Please log in via your web browser and\n5.7.14 then try again

I did that but continue to get the same message. The password I give in the app is correct. Is there anything I need to configure in my gmail account?

Patrick Connors
  • 5,677
  • 6
  • 27
  • 54
  • 1
    The return render(...) line is incorrectly indented as displayed and won't be returned as part of the index function. Is this the same indentation you have in your file? – Ian Price Jan 18 '16 at 04:58
  • The return render line was int the correct place in my file, just a typo on the question. – Patrick Connors Jan 18 '16 at 05:01
  • 1
    In my settings I have to set EMAIL_PORT = 587 in order to send through Gmail w/ TLS; the ports available from Gmail as I understand are 25 for no-encryption, 465 for SSL, 587 for TLS – Ian Price Jan 18 '16 at 05:03
  • Any luck? If so, I'll update my answer so that it clearly answers the question as asked now – Ian Price Jan 18 '16 at 05:12
  • That helps! I updated my question as I'm running into another issue. – Patrick Connors Jan 18 '16 at 05:15
  • Probably best to ask a brand new question; your StackOverflow questions may be viewed by thousands of users years down the line :) – Ian Price Jan 18 '16 at 05:16
  • 1
    But... Take a look here - http://stackoverflow.com/questions/26697565/django-smtpauthenticationerror – Ian Price Jan 18 '16 at 05:16
  • That ties things up. Thanks for your help! I'll clean up by question a bit to make it clearer. – Patrick Connors Jan 18 '16 at 05:20
  • No problem; blast out some emails and please mark any comments that you found particularly useful as helpful/upvoted. Have fun! – Ian Price Jan 18 '16 at 05:22

1 Answers1

1

Change EMAIL_HOST = 'smtp.gmail.com ' toEMAIL_HOST = 'smtp.gmail.com' and I bet your problem will disappear :)

EDIT #1 You are running into authentication issues as EMAIL_USE_TLS is True and Gmail only requires TLS connections for SMTP on port 587. Change to EMAIL_PORT = 587 and you should bypass the issue.

EDIT #2 The error you see can be fixed through your Gmail settings. See - Django SMTPAuthenticationError

Community
  • 1
  • 1
Ian Price
  • 7,416
  • 2
  • 23
  • 34
  • Sorry for wasting you time with the typo. That got rid of the error message, but takes me back to a problem I was having earlier before the typo. When I try to send the email the browser just reloads forever. @Ian Price – Patrick Connors Jan 18 '16 at 04:49
  • 1
    `send_mail(subject, message, from_email, ['email@example.com'])` would typically only be called in a view or triggered by a method on a model or form. If you post a new question with the full code where the `send_mail` function is called, we can help you out. And if I helped you out, please mark as complete. Thanks! – Ian Price Jan 18 '16 at 04:52
  • I just had to wait a second before it would allow me to accept the answer. The function is called in a view that is functional without the mail-related code.@Ian Price – Patrick Connors Jan 18 '16 at 04:54