Sending Email from form

27 views
Skip to first unread message

Emmanuel klutse

unread,
Jun 28, 2019, 2:22:29 PM6/28/19
to django...@googlegroups.com
Hello Team. 
I’m new to programming and I’ve been studying Django for 6 months now. I just started my first project and i need help with handling sending emails from a user form.
I am using send_mail function to allow anyone who wants to contact me through my contact page. Its is working fine but I need a way for Django to allow me get an email from my application using the email address provided by the user in fill the form instead of the one I provided in settings.py.
Please help…. I have been on this for the past one week. 

I’m also available to assisting anyone who needs someone with my level on a project for free. 

Thank you
Emmanuel klutse

Charlotte Wood

unread,
Jun 28, 2019, 8:27:39 PM6/28/19
to django...@googlegroups.com
I need this too!!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60A5D9BF-5096-4824-90D2-7149E1ACC82E%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Sithembewena L. Dube

unread,
Jun 28, 2019, 10:28:25 PM6/28/19
to django...@googlegroups.com
I just finished a project where I did the same thing you require.

I used the Mailgun API.

The relevant documentation can be found here:
https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-api

Kind regards,
Lloyd


Sent with Shift

--

Emmanuel klutse

unread,
Jun 29, 2019, 12:25:52 AM6/29/19
to django...@googlegroups.com
Thank you Dube!!! Will check it out and update shortly. 

Sent from my iPhone

Sithembewena L. Dube

unread,
Jun 29, 2019, 1:28:06 PM6/29/19
to django...@googlegroups.com
You're welcome Emmanuel.

Fyi, I think using one email as the 'from' address and sending the user-provided one in the email body may be easier, as mail sent from different addresses yet from the same mail server IP address may be flagged as spam (Barracuda Networks is an example of such an enforcer).

My solution was as follows:

- Declare a Django Form that accepts the email address. Call the field whatever you like, e.g. 'email_address'
- In your view, if request.method == 'POST':
-    if form.is_valid():
-        email_address = form.cleaned_data['email_address']
- Build a message and include the email in its text
- Call send_mail(...) and use an email address you provided (as the 'from' address)


Kind regards,
Lloyd


Sent with Shift

Emmanuel klutse

unread,
Jun 29, 2019, 2:01:22 PM6/29/19
to django...@googlegroups.com
Thanks ones again, 
-How do I build a message and include the email in its text please. I can I get a snippet please. 
Thanks 

Sithembewena L. Dube

unread,
Jun 30, 2019, 2:26:05 PM6/30/19
to django...@googlegroups.com
Basically, you would need to build it in string form.

This could be done through a combination of string interpolation and concatenation.

Here is my quick hack below (I have not shown all the code).

There is a cleaner way to do this, e.g. using templates, but I wanted something that would "just work" for the time being.

first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
start_date = form.cleaned_data['start_date']
number = form.cleaned_data['number']
message = form.cleaned_data['message']
number_of_people = form.cleaned_data['number_of_people']
if is_type == 'admin':
sender = form.cleaned_data['email']
return 'Booking request for "{}" tour starting '.format(tour) + str(start_date) + ' from {} {}.'.format(first_name, last_name) + \
'\n\nNumber of people: {}'.format(number_of_people) + \
'\n\nThe message is as follows: \n\n"' + message + \
'"\n\nSent by: {} {}'.format(first_name, last_name) + '\n' + sender + '\n' + number
elif is_type == 'client':
return 'Dear ' + first_name + ' ' + last_name + ',\n\n' +\
'Your booking request for our "{}" Tour has been received.'.format(tour) + \
'\n\nNumber of people: {}'.format(number_of_people) + \
'\n\nThe message is as follows: \n\n"' + message + \
'"\n\nWe will respond as soon as possible.' +\

Kind regards,
Lloyd


Sent with Shift
Reply all
Reply to author
Forward
0 new messages