Problem in Django’s send_email function

Asked

Viewed 331 times

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.

1 answer

3


It seems that you are trying to get the user who completed the form to send an email to you. But then things won’t work out that way. 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 it filled in the form in the body of the message received through the application.

Remember that not all email platforms/servers accept you define a different sender than the one used in SMTP authentication (at least, every time I tried this, it never worked).

Maybe it’s just a matter of business rule. I, in your case, would only create a standard message, identifying from which application it is, and would send the message normally, even if the recipient and sender were the same person, and would put in the body of the email the value filled by the user there in the 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 }}

Accretion: You may also want to use the form to reply messages to the user so that the communication (email response) is stacked. A good idea is to keep sending the email "to yourself", but adding the user’s email form as CC (with copy)

  • Thank you very much! cleared up a lot! Problem solved :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.