R
R
Ramplin2018-06-10 18:45:28
Django
Ramplin, 2018-06-10 18:45:28

How to send a letter from any addressee in the form?

Tell me, please, how can I send letters from any addressee from the form?
settings:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '***'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'Python ecommerce  <***>'
BASE_URL = '127.0.0.1:8010'

forms.py:
class ContactForm(forms.Form):
    subject = forms.CharField(widget=forms.TextInput(
            attrs={
                "class": "form-control",
                "placeholder": "ФИО"
            }
        )
    )
    sender = forms.EmailField(widget=forms.EmailInput(
            attrs={
                "class": "form-control",
                "placeholder": "Email"
            }
        )
    )
    message = forms.CharField(widget=forms.Textarea(
            attrs={
                'class': 'form-control',
                "placeholder": "Оставьте свои данные для связи"
            }
        )
    )
    copy = forms.BooleanField(required = False)

views.py:
def contactView(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        
        if form.is_valid():
            subject = form.cleaned_data['subject']
            sender = form.cleaned_data['sender']
            message = form.cleaned_data['message']
            copy = form.cleaned_data['copy']

            recipients = ['***']
           
            if copy:
                recipients.append(sender)
            try:
                send_mail(subject, message, sender, recipients)
            except BadHeaderError:  # Защита от уязвимости
                return HttpResponse('Invalid header found')
           
            return render(request, 'thanks/thanks.html')
    else:
     
        form = ContactForm()
   
    return render(request, 'Rest/contact.html', {'form': form})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-06-10
@Ramplin

No way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question