M
M
MalekBV2019-09-23 08:20:12
Django
MalekBV, 2019-09-23 08:20:12

Django 2 email send error?

Hello, I ran into a problem, sending a letter to the mail (through a form) always worked, but today when I also wanted to send a letter to the mail through the form, this error appeared:

UnicodeEncodeError at /
'ascii' codec can't encode characters in position 7-8 : ordinal not in range(128)

Here is my forms.py

# -*- coding: utf-8 -*- 
from django import forms

class ContactForm(forms.Form):
    name = forms.CharField(label="Имя", max_length = 20)
    email = forms.EmailField(label="Email")
    phone = forms.CharField(label="Телефон", max_length = 20)
    message = forms.CharField(label="Сообщение", widget = forms.Textarea, required = False, max_length = 100)

Views.py
# -*- coding: utf-8 -*-
from .forms import ContactForm
from django.core import mail
from django.conf import settings

def index(request):
    categories = Category.objects.all()
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            sender_name = form.cleaned_data['name']
            sender_email = form.cleaned_data['email']
            sender_phone = form.cleaned_data['phone']
            message = """НОВАЯ ЗАЯВКА\n\nИмя: {0}\nПочта: {1}\nТелефон: {2}\n\nСообщение:\n\n{3}""".format(sender_name, sender_email, sender_phone, form.cleaned_data['message'])

            with mail.get_connection() as connection:
                mail.EmailMessage(
                    'ЗАЯВКА (peno.decor)', message, '[email protected]', ['[email protected]'], connection=connection,
                ).send()

            return render(request, 'main/thank.html', {'name': sender_name})

    else:
        form = ContactForm()

    return render(request, 'main/home.html', {'categories': categories, 'form': form})

The template is normal, it displays the form. Sending always worked and suddenly now there is an error
Help please
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.mail.ru'
EMAIL_PORT = 2525
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "somepass"
EMAIL_USE_TLS = True

SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question