Problem in Django send_email function

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 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.

    
asked by anonymous 01.03.2018 / 17:19

1 answer

3

It looks like you're trying to get the user who filled out the form to send you an email. But in case things will not work that well. At least until today, when I needed to make a form where the client sent an email to me, I always put the email that the same filled in the form in the body of the message received through the application.

Please note that not all email platforms / servers support you to set a different sender than the one used for SMTP authentication (at least every time I tried this, it never worked).

Maybe it's just a matter of business rule. I would, in your case, only create a standard message, identifying which application it is, and would send the message normally, even though the recipient and sender are the same person, and would put the user-filled value there in the body of the email form.

For example:

FROM: [email protected]
TO:   [email protected]

Subject: Mensagem recebido a aplicação XXX - Assunto

BODY: O usuário de nome {{ nome }} envio a seguinte mensagem através do formulário a aplicação XXX.   

 bla bla bla bla bla

 Para responder esse usuário, utilize o e-mail {{ email_que_foi_preenchido_no_formulario }}

Addition : You may also want to use the form to respond to messages to the user, so that communication (e-mail response) is stacked. A good idea is to continue sending the email "to yourself", however adding the form's user email as CC (With copy)

    
01.03.2018 / 17:32