I previously had the following SETTINGS:
{{{
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_HOST_USER = 'my_user'
EMAIL_HOST_PASS = 'my_pass
EMAIL_PORT = 587
EMAIL_USE_TLS = True
}}}
and the following view:
{{{
def index(request):
""" Return landing page with contact form """
if request.method == 'GET':
# Init a blank form
form = ContactForm()
else:
# Grab data from POST, validate, and attempt to send mail.
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, [my_to_email])
except BadHeaderError:
# Fail on invalid header.
return HttpResponse('Invalid Header found.')
# After sending, redirect to /success
return redirect('success/')
# return blank form
return render(request, 'index.html', {'form': form})
}}}
Which are all correct (I'm using gsuite so I use smtp-relay.gmail.com
instead of smtp.gmail.com).
I spent hours working on it, even tried sendgrid, was getting
Unauthenticated Senders Not Allowed, and contacted their support and none
of us were able to get it to work.
Finally I tried explicitly setting auth_user and auth_pass in send_mail(),
and it finally worked.
After importing setting variables:
{{{
send_mail(subject, message, from_email, ['my_to_address'],
auth_user=EMAIL_HOST_USER,
auth_password=EMAIL_HOST_PASS)
}}}
This makes it seem like it is not falling back to the settings variables.
I attempted to diagnose the issue, but was unable to. I hope someone else
can solve where it needs to be set.
--
Ticket URL: <https://code.djangoproject.com/ticket/28922>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Simon Charette):
Hello there, I unfortunately cannot reproduce your issue against Django
2.0 and we've got
[https://github.com/django/django/blob/6fd6d8383f48ea2fe4e058725fa30529a083e9a5/tests/mail/tests.py#L1265-L1287
unit tests asserting settings fallbacks] work correctly.
Unless you're able to provide a reduced test case to help us reproduce
your issue I'm afraid we'll have to close this ticket as ''worksforme''.
--
Ticket URL: <https://code.djangoproject.com/ticket/28922#comment:1>
* status: new => closed
* resolution: => worksforme
--
Ticket URL: <https://code.djangoproject.com/ticket/28922#comment:2>
Comment (by Zach Bresser):
Can we reopen this? At least in my case it was not falling back to the
settings variables.
I am working on getting a test case together for Simon. I work full time
so I have been unable to do it today.
--
Ticket URL: <https://code.djangoproject.com/ticket/28922#comment:3>
Comment (by Simon Charette):
Zach, feel free to re-open the ticket if you can provide a simplified
reproduction test case.
--
Ticket URL: <https://code.djangoproject.com/ticket/28922#comment:4>