Как исправить ошибку SMTPServerDisconnected('please run connect() first') в django 2

У меня есть следующая email-настройка в settings.py:

EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = os.environ.get('email_host_user')
EMAIL_HOST_PASSWORD = os.environ.get('email_host_password')
EMAIL_PORT = 587
EMAIL_USE_TLS = True

У меня есть следующая функция в файле views.py:

def post_share(request, post_id):
    # get post from id
    post = get_object_or_404(Post, id=post_id, status='published')
    sent = False
    if request.method == "POST":
        # form was send to save
        form = EmailPostForm(request.POST)
        if form.is_valid():
            # All field of the form have been checked
            cd = form.cleaned_data
            # Sending email
            post_url = request.build_absolute_uri(post.get_absolute_url())
            subject = '{}, ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
            message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments'])
            send_mail(subject, message, 'lilarro93@gmail.com', [cd['to']])
            sent = True
    else:
        form = EmailPostForm()
    return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent})

А если вызывается функция "send_mail", то у меня возникает следующая проблема:

терминал:

>>> send_mail("Django mail", "This mail was sent from django", 'server@gmail.com', ['another@gmail.com'], fail_silently=False)
...
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

Как я могу это исправить?

Вернуться на верх