Good afternoon!
I have the following code in views.py:
def email(request):
if request.method=='POST':
form = ContactForm(request.POST)
form.email = request.POST.get('email', '')
form.subject = request.POST.get('subject', '')
form.message = request.POST.get('message', '')
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['email']
cc_myself = form.cleaned_data['cc_myself']
recipients = ['[email protected]']
if cc_myself:
recipients.append(email)
send_mail(subject, message, sender, recipients, fail_silently=False)
return HttpResponseRedirect('thanks')
else:
form.errors
else:
form = ContactForm()
return render(request, 'main.html', {'form': form})
and in Settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '***'
The function is working and I receive the email as set. The problem is that the email is being sent to the same place it is received ( to_email and from_email being equal). The sender variable fills the to_email parameter of the send_email function, but for some reason I'm sending and receiving to the same e-mail. I think it does not make much sense for the user to be able to send an email from their own address to another via a form of a website without authentication but anyway, I also do not know how to manipulate this to_email parameter > to know who sent that message to the recipient email.