Подскажите, пожалуйста, как можно отправлять письма от любого адресата с формы?
настройки:
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})