To know the originating user of an email message through DJango

0

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 result:

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.

    
asked by Miguel Alparez 23.05.2017 в 17:27
source

2 answers

0

You have a problem with this concept.

The email is simple, it has 3 protocols 2 to receive: IMAP and POP3 and another to send: SMTP

With the SMTP protocol it is possible to do what you ask since it only consists of 4 parameters that are strings: -Who sends -A Where it's aimed -Title -Body of the message

If you mount your own SMTP the parameter 'who sends it' is modifiable, but if you use a SMTP third party (in this case the google) will not let you modify that paramenter for identity theft issues.

EYE Because at least in my country (Spain), impersonating an identity is illegal. I do not know exactly if your application would apply. But be careful with this.

Therefore: If you set up a third-party SMTP, your mail will always be sent by what you have set in EMAIL_HOST_USER or, failing that, the account you have associated with the passwords of the SMTP service you are using.

I recommend that you put it in the body or in the title, type the 'user Pajaritos has left you a message' and that within the body, indicate the email account where to respond. (Bearing in mind that your service conditions should cover this case)

    
answered by 24.05.2017 в 11:47
0

I have already fixed what I wanted to see the user's name in the header.

You have to modify this 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(**request.user**)
            datos=form.cleaned_data
            email=EmailMessage(cuerpo,'body', to=['[email protected]'])
            email.send()
            return HttpResponseRedirect('/')
        return render(request,'email.html',{'forma':form})

So you know who sent the message.

    
answered by 24.05.2017 в 16:49