You see, in my DJango program I give an option to the user to send a message that will be sent to an email.
Code in settings.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'perritos'
Code in forms.py:
class mail (forms.Form): subject = forms.CharField (required = True) content = forms.CharField (max_length = 999, widget = forms.Textarea)
Code in views.py:
class contacto(View):
def get(self,request):
form=correo()
return render(request,'email.html',{'forma':form})
def post(self,request):
form=correo(request.POST)
if form.is_valid():
datos=form.cleaned_data
email=EmailMessage('title','body', to=['[email protected]'])
email.send()
return HttpResponseRedirect('/')
return render(request,'email.html',{'forma':form})
The email account the message went to is [email protected] . The user with whom I did the test had a different email name. What do I do to indicate the email of the person who sent the message?
Edit:
I'm going to prove that the email header indicates who sent the message. To do this, first remove the variable "subject" of the object in the forms.py.
class correo(forms.Form):
contenido=forms.CharField(max_length=999, widget=forms.Textarea)
And then I modify the code in views.py:
class contacto(View):
def get(self,request):
form=correo()
return render(request,'email.html',{'forma':form})
def post(self,request):
form=correo(request.POST)
if form.is_valid():
cuerpo="Mensaje de "+str(User.objects.get(first_name='Sara'))
datos=form.cleaned_data
email=EmailMessage(cuerpo,'body', to=['[email protected]'])
email.send()
return HttpResponseRedirect('/')
return render(request,'email.html',{'forma':form})
I check that the header of the message is what I put in the body variable in the email (and although I put the first_name as an argument, what I see in the header is the username). Ovbidly what I want is to be sent on behalf of the user who has the session open, not the user that I decide.