I have email verification working in Django, it sucessfully sends an email to inbox when I click the submit button at the bottom of the signup page, but the only problem is that it does not yet have the function where it sends an email to the address of the user who just signed up, instead, it only sends the email to the email address hard coded in the views .py file .
Signup page:
[![Signup page][1]][1]
views.py
from django.core.mail import EmailMessage
email = EmailMessage(
'Test', #email subject
'Hello,'
' This is the Robo Depot team. We wanted to let you know your
account is confirmed.'
' Thank You for joining the Robo Depot userbase.', #email body
'*********', #not sure what this does
['******', '******',
'***********'], #recipients
reply_to=['hignuff#gmail.com'],
headers={'Message-ID': 'foo'},
)
email.send(fail_silently=False)
Settings.py
# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER = '**********'
EMAIL_HOST_PASSWORD = '*****'
register.html
<button id="signup-btn" type="submit" class="mt-3 btn btn-
primary" EmailMessage/cart/view.py">
Submit
</button>
I don't even know where to begin here. I think I need a get function to pull the value from the email address form field, but I have no clue how I would even get that. Anyone have any idea?
Where is the form data sent to?
that would be the action attribute in the <form> element.
say it's /api/register
you can set up a handler there to get the email address from the form and then call the emailing function with the address as a parameter.
Related
I am trying to create a contact form in Django that actually sends emails for real. Can i put all the email configs in the views.py file itself? I want to do that because i want only the legitimate owners of emails to actually send emails. I do not people to send me emails using their friends email.
Yes obviously you can, but make sure you have your email credentials stored in your settings.py file safely
What is ideal is to save your email credentials as environment variables
In your settings.py File
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "from#example.com"
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# If you are using any other smtp host.
# Search documentation for other smtp host name and port number
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = "from#example.com"
EMAIL_HOST_PASSWORD = "SUPER_SECRET_PASSWORD"
In your view that you want to use to send email
views.py
from django.core.mail import EmailMessage
def email_send_view(request):
if request.method == "POST":
# Get email information via post request
to_email = request.POST.get("to", "")
subject = request.POST.get("subject", "")
message = request.POST.get("message", "")
if to_email and message:
email = EmailMessage(subject=subject,body=body,to=[to])
email.send()
# Complete your view
# ...
return redirect("REDIRECT_VIEW")
If you want to send html template or image using email Email Template via your django app
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from YourProject.settings import EMAIL_HOST_USER
def email_send_view(request):
if request.method == "POST":
# Get email information via post request
to_email = request.POST.get("to", "")
subject = request.POST.get("subject", "")
message = request.POST.get("message", "")
if to_email and message:
# Gets HTML from template
# Make sure in this case you have
# html template saved in your template directory
html_message = render_to_string('template.html',
{'context': 'Special Message'})
# Creates HTML Email
email = EmailMultiAlternatives(subject,
from_email=EMAIL_HOST_USER,
to=[to])
# Send Email
email.attach_alternative(html_message, "text/html")
email.send()
# Complete your view
# ...
return redirect("REDIRECT_VIEW")
I am working in a project where I need to send the email to the user who is filling the Email in the form. I am able to send email by using my Gmail Account details but while using outlook.365. This is the image with error that I am getting.
My requirement is :
Once users come in the registration form and fill the details as Name, Email, Mobile
Whatever Email is put there in the form, send the email with link to create new password.
Here is my code:
settings.py:
# Using outlook365:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'support#academic.com'
EMAIL_HOST = 'smtp.outlook.office365.com' # (also tried : smtp.office365.com and
outlook.office365.com)
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
# Using my gmail account:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'admin#gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
views.py:
subject = "Academic - Create New Password"
message = "Hi %s! Please create your new Password here : http://127.0.0.1:8000/api/create-password" % firstname
send_mail(
subject, # Subject of the email
message, # Body or Message of the email
'support#academic.com', # from#gmail.com (admin#gmail.com for gmail account)
[email], # to#gmail.com # email that is filled in the form
)
Please guide me that what extra setting or configuration I need to send email using Outlook365.
Please put this in settings.py file.
SERVER_EMAIL = EMAIL_HOST_USER
Your settings look correct. Use EMAIL_HOST = "smtp.office365.com". Verify that you are using the correct credentials. Check that your outlook account is set up properly to send emails from support#academic.com.
I am writing django application and trying to send email using it I have "Access to low security app" in gmail enabled and django setting are given below which I think are right. but I am still getting error as mentioned it title. I dont know the problem but I know I am not getting logged in to send email.
Edit 1: I have made two changes first in settings.py and second in views.py in views.py I replaced email entered by user to mine(syedfaizan824#gmail.com) and settings.py DEFAULT_EMAIL_FROM is changed form testing#example.com to syedfaizan824#gmail.com
searched on internet and find out that gmail does not allow low security app to login by default but I turned it off in setting of gmail. find out that my email backend was wrong so made it right. fail silently is False.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = 'syedfaizan824#gmail.com'
EMAIL_HOST_USER = 'syedfaizan824#gmail.com'
EMAIL_HOST_PASSWORD = '******'
views.py
def post(self, request, *args, **kwargs):
form = contact(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
phone = form.cleaned_data['phone']
organization = form.cleaned_data['organization']
message = form.cleaned_data['message']
ref_code = form.cleaned_data['ref_code']
plan = form.cleaned_data['plan']
message = message + ref_code
send_mail(
'from website' + name + " " + organization,
message,
'syedfaizan824#gmail.com',
['syedfaizan824#gmail.com'],
fail_silently=False,
)
print("sent")
else:
#print('something is wrong with forms!')
return render(request, self.template_name, self.context)
Error message is ConnectionRefusedError WinError[10061]. and statement of error is : No connection could be made because the target machine actively refused it. which means I am not getting logged in.
change
EMAL_HOST = 'smtp.gmail.com'
to
EMAIL_HOST = 'smtp.gmail.com'
You are missing the 'I' character in the word 'EMAIL'
I am trying to send email from the user who is logged in to an account which I have given bilalkhangood4#gmail.com. But I have typed it manually. There could be more accounts means more users from which I want to send. But how could I send email from just those users who are logged in? Basically it is an APP where a user will click on withdraw button to request and send mail to official admin account.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'bilalkhangood4#gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_SSL = False
views.py
from django.contrib.auth.models import User
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
'bilalkhangood6#gmail.com', #FROM
['bilalkhangood4#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')
use request.user.email to access the email address of logged in user.
update your code with following:
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
request.user.email, #FROM
['bilalkhangood6#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')
im trying to make a contact form in django 1.3, python 2.6.
Whats the reason of following error?
error:
SMTPRecipientsRefused at /contact/
{'test#test.megiteam.pl': (553, '5.7.1 <randomacc#hotmail.com>: Sender address
rejected: not owned by user test#test.megiteam.pl')}
my settings.py:
EMAIL_HOST = 'test.megiteam.pl'
EMAIL_HOST_USER = 'test#test.megiteam.pl'
EMAIL_HOST_PASSWORD = '###'
DEFAULT_FROM_EMAIL = 'test#test.megiteam.pl'
SERVER_EMAIL = 'test#test.megiteam.pl'
EMAIL_USE_TLS = True
edit: If any1 else was following djangobook, this is the part causing it:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('email', 'noreply#example.com'), #get rid of 'email'
['siteowner#example.com'],
The explanation is in the error message. Your email host is rejecting the email because of the sender address randomacc#hotmail.com that you have taken from the contact form.
Instead, you should use your own email address as the sender address. You can use the reply_to option so that replies go to your user.
email = EmailMessage(
'Subject',
'Body goes here',
'test#test.megiteam.pl',
['to#example.com',],
reply_to='randomacc#hotmail.com',
)
email.send()
On Django 1.7 and earlier, there isn't a reply_to argument, but you can manually set a Reply-To header:
email = EmailMessage(
'Subject',
'Body goes here',
'test#test.megiteam.pl',
['to#example.com',],
headers = {'Reply-To': 'randomacc#hotmail.com'},
)
email.send()
Edit:
In the comments you asked how to include the sender's address in the message body. The message and from_email are just strings, so you can combine them however you want before you send the email.
Note that you shouldn't get the from_email argument from your cleaned_data. You know that the from_address should be test#test.megiteam.pl, so use that, or maybe import DEFAULT_FROM_EMAIL from your settings.
Note that if you create a message using EmailMessage as in my example above, and set the reply to header, then your email client should do the right thing when you hit the reply button. The example below uses send_mail to keep it similar to the code you linked to.
from django.conf import settings
...
if form.is_valid():
cd = form.cleaned_data
message = cd['message']
# construct the message body from the form's cleaned data
body = """\
from: %s
message: %s""" % (cd['email'], cd['message'])
send_mail(
cd['subject'],
body,
settings.DEFAULT_FROM_EMAIL, # use your email address, not the one from the form
['test#test.megiteam.pl'],
)