0

I am setting email form at my django-python based webpage. I have seen this cool video nicely explaining how setup django and how to incorporate gmail as the smtp server.

The form has three fields: Full Name; Email Address; and Message.

Everything works fine, but gmail smtp says that I (EMAIL_HOST_USER) am the sender of the message and not the person who fulfilled the form, i.e. I am missing fulfilled email address with the default youtube video implementation.

This thread and this thread say, we can not change the sender address. I have added email address of the person asking via form (i.e variable message_email) into recipients. This asegurar, you will not lose the email address and the asking person will get a feedback that you are processing his/her question.

There are two other dynamic variables which we need to get. 1.) message_name (Full name of the asking person) 2.) message (i.e. the plain text from the form with \r\n marks at the end of each paragraph with quite good python print() output). See:

message = 'Dear website developers,\r\nI would like to ask when the website will be online available.\r\nKind regards,\r\nJohn Doe'

print(message)
Dear website developers,
I would like to ask when the website will be online available.
Kind regards,
John Doe

@Maina Kamau suggested to get the dynamic concent into the email message with legible content via hmtl formating, as described here. Here is a useful thread describing how to substitute parts of text in python. The modified nad working code now appears as follows:

from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags

html_message = render_to_string(
        'email.html',
        {'name': message_name,'email': message_email, 'message': message}
)
plain_message = strip_tags(html_message)
html_message = html_message.replace("\r\n", "<br>")

send_mail(
        'message from ' + message_name, # email_subject
        plain_message, # message
        message_email, # from email
        ['recepient1@example.com', message_email], # To Email
        html_message=html_message, # html formated message
)

The email.html (from render_to_string command), incorporating the dynamic content into the email message, can appear e.g. as follows:

<p style="text-align: justify">Dear {{ name }},
   <br>We have recieved your message and will respond as soon as possible.
   <br>This is an automatically generated message.
   <br>If you want to provide some additional information to us you can easily respond at this email.
   <br>Yours sincerely,
   <br>Helpdesk
</p>
<p style="text-align: justify"><br></p>
<p style="text-align: justify"><i>Your initial message details are as follows:</i>
   <br><b>Full Name:</b> {{ name }}
   <br><b>Email Address:</b> {{ email }}
   <br><b>Message:</b>
   <br>{{ message }}
</p>

Many thanks to @Maina Kamau for the comments, I would not make it working without them!!!

Best, Rene

1 Answers1

0

I believe as far as changing the sender address that cannot be changed, this is because the sender email is the one configured with all the requisite credentials to perform the sending. As far as formatting your email is concerned, that can be achieved by adding some HTML syntax in your email body. The top voted answer on this thread shows how to achieve this.

  • Thanks Maina, I have updated the original post based on your recomendation. But still, there stays one issue in the last command row of the email.html file. Please, any idea? – René Labounek May 02 '20 at 04:08
  • `newvar = message.split('\r\n')`, this can't be done in the template file, this has to be done in the python file as the thread you pointed out describes. – Maina Kamau May 03 '20 at 06:25
  • Many thanks Maina, I have sovled how to incorporate `.replace` into the views python file. I have updated the original question text into the form of a text solving the problem. I hope, it can help some other future django users sending gmails from their webpage. – René Labounek May 05 '20 at 07:14