Yevhen
Yevhen

Reputation: 13

Django - Email is not sent but its object is created in DB

When I try to send email via django, I notice that object of email is created and all fields (email, title, body) are in it, but the actual email is not sent. When I check celery logs, I see the following message:

SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted.
Learn more `at\n5.7.8  https://support.google.com/mail/?p=BadCredentials` 

but I'm 100% sure that I use correct credentials and my mailbox isn't secured with two factor authentication

Code in

settings.py

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER = '*******@gmail.com' # my email, I'm sure it's correct
EMAIL_HOST_PASSWORD = '********' # my password, I'm sure it's correct

Code in

views.py

(contact form where I gather needed info - email, title, body)

class ContactUs(CreateView):
    template_name = 'my_profile.html'
    queryset = Contact.objects.all()
    fields = ('email', 'title', 'body')
    success_url = reverse_lazy('index')

    def form_valid(self, form):
        response = super().form_valid(form)

        message = form.cleaned_data.get('body')
        subject = form.cleaned_data.get('title')
        email_from = form.cleaned_data.get('email')
        recipient_list = [settings.EMAIL_HOST_USER, ]

        send_email_async.delay(subject, message, email_from, recipient_list)

        return response

Code in

tasks.py

(for celery)

@shared_task()
def send_email_async(subject, message, email_from, recipient_list):
    send_mail(subject, message, email_from, recipient_list, fail_silently=False)

but it doesn't really matter if it's celery or not - email is not sent itself but I see the object of this email in my Postgres DB

Django version 2.2.10

Upvotes: 0

Views: 216

Answers (1)

user2390182
user2390182

Reputation: 73498

You have set the console backend which just logs emails to the console:

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

You need to use the smtp backend:

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

Upvotes: 1

Related Questions