1
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 location as it is received (to_email and from_email are being equal). The variable Sender fills the parameter to_email of function send_email, but for some reason I’m sending and receiving the same email. I think that it does not make much sense for the user to be able to send an email from his own address to another, through a form of a site without authentication but anyway, also do not know how to manipulate this parameter to_email to find out who sent that message to the recipient email.
Thank you very much! cleared up a lot! Problem solved :)
– Mikhael Araujo