Django reset password sender and receiver inverted - python

In Django my project I add the "Frorget password" link.
In my urls.py:
path('accounts/', include('django.contrib.auth.urls')),
in my settings.py:
# Email setting
EMAIL_USE_TLS = True
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_PORT = 587
EMAIL_FROM = 'account#test.io'
EMAIL_HOST_USER = 'YUIJKHKBKBD7879'
EMAIL_HOST_PASSWORD = 'By6786ghgkgk//89jkjlnCkVt'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
but when I go to my page /accounts/password_reset/ and insert the email for what I whant to change the password es. mario.rossi#gmail.com, when I click The Submit button, system return:
SMTPDataError at /accounts/password_reset/
(554, b'Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1: mario.rossi#gmail.com, webmaster#localhost')
But mario.rossi#gmail.com is not the sender, it's the receiver! Why django reset password does seem to use EMAIL_FROM from settings.py for send email From?

Related

Reset password email from django doesn't appear in my mailbox

Scenario:
In my django application, admin should create users using 'username' and 'email', 'password' will be generated auto .Welcome email will be sent to users with redirect link to reset password.
Issue:
after entering email for reset password the app should send email with link to confirm reset password. THE PROBLEM is the email it never show in user "inbox" but i can find it in app "outbox mails"
How can i make it appear in user mailbox?
Any Help or idea?
I'm using:
-Python3
-Django4
-enable two factor authentication(2FA) for Google account
Settings.py:
#SMTP Config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "webapp#app.com"
DEFAULT_FROM_EMAIL = "MyApp <no-reply#app.com>"
EMAIL_HOST_PASSWORD = "******"
APP/URLS:
urlpatterns = [
path('',views.login_user, name ='login'), #login fonction path
path ('user_logout',views.user_logout, name = 'logout'), #logout function path
path('dashboard/', views.dashboard, name='dashboard'),# dashoard view path
path ('daily',views.DailyView, name = 'dailydashboard'),#daily view depath
path ('monthly',views.MonthlyView, name = 'Monthlydashboard'),#monthly view path
path ('yearly',views.YearlyView, name = 'Yearlydashboard'), #yearly view path
path("reset_password/", auth_views.PasswordResetView.as_view(template_name="password/password_reset.html"), name="reset_password"),
path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name="password/password_reset_done.html"), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="password/password_reset_confirm.html"), name='password_reset_confirm'),
path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='password/password_reset_complete.html'), name='password_reset_complete'),
]

SMTP error sending an email to my second email account

I am trying to send an email to my second email account using GMail's SMTP.
This is the error
SMTPConnectError at /register/
(451, b'Request action aborted on MFE proxy, SMTP server is not available.')
This is in my settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my-gmail#gmail.com'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
This is my views.py file
email = EmailMessage("Subject Here",
"Testing hello world", "",
["therealemaluser96#gmail.com"]
)
You must activate 2-Step-Verification in your gmail and use app passwords instead of your general email's password.
This link helps you:
https://support.google.com/accounts/answer/185833?hl=en

django docs email is not activated after click on email confirmation link in Django mail backend

I created the Django email backend for active the email during the registration. But in my case When I register the account and confirmation email is sent on my email and after click on the confirmation link then it's not activate the user's account and link is not redirected on login page.
setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your_mail#gmail.com'
EMAIL_HOST_PASSWORD = 'XXXXXXXXXX'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = "USERNAME"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = reverse_lazy('account_confirm_complete')
ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = reverse_lazy('account_confirm_complete')
urls.py
urlpatterns = [
url(r'^user_reg/registration/account-email-verification-sent/', email_view.null_view, name='account_email_verification_sent'),
url(r'^user_reg/registration/account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(), name='account_confirm_email'),
url(r'^user_reg/registration/complete/$', email_view.complete_view, name='account_confirm_complete'),
url(r'^user_reg/password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', email_view.null_view, name='password_reset_confirm'),
path('admin/', admin.site.urls),
path('user_reg/', include('users.urls', namespace="users")),
path('user_reg/registration/', include('rest_auth.registration.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And below the email message
Greeting from example.com!
You're receiving this e-mail because user usertest has given yours as an e-mail address to connect their account via API.
To confirm this is correct, go to http://127.0.0.1:8000/user_reg/registration/account-confirm-email/Mjg:1jSeJC:4btJkSnHSxYN7w5CITEPydcG9cA/
Thank you from example.com!
example.com
Where is the problem and how can I solve this?.
Please help me. Thank you!

Forgot password in django

I trying to do forgot password function in my django site.
My settings.py is:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email#gmail.com' #sample
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
my urls.py is:
url(r'^user/password/reset/$',
'django.contrib.auth.views.password_reset',
{'post_reset_redirect' : '/user/password/reset/done/'},
name="password_reset"),
url(r'^user/password/reset/done/$',
'django.contrib.auth.views.password_reset_done'),
url(r'^user/password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
{'post_reset_redirect' : '/user/password/done/'}),
url(r'^user/password/done/$',
'django.contrib.auth.views.password_reset_complete'),
I followed this site,the templates are shown there.
When I entered email id ,it shows that it mailed. But I am not getting any email.
I tried in console :
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Subject', 'Message', to=['example#example.com'])
>>> email.send()
1
and I got email..
Help me..
Please check your INBOX of 'email#gmail.com' email id, May be there is a new email regarding asking your permission to send email.

How to send email via Django?

In my settings.py, I have the following:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Host for sending e-mail.
EMAIL_HOST = 'localhost'
# Port for sending e-mail.
EMAIL_PORT = 1025
# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
My email code:
from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'World', to=['user#gmail.com'])
email.send()
Of course, if I setup a debugging server via python -m smtpd -n -c DebuggingServer localhost:1025, I can see the email in my terminal.
However, how do I actually send the email not to the debugging server but to user#gmail.com?
After reading your answers, let me get something straight:
Can't you use localhost(simple ubuntu pc) to send e-mails?
I thought in django 1.3 send_mail() is somewhat deprecated and EmailMessage.send() is used instead?
I use Gmail as my SMTP server for Django. Much easier than dealing with postfix or whatever other server. I'm not in the business of managing email servers.
In settings.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'me#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
NOTE: In 2016 Gmail is not allowing this anymore by default. You can either use an external service like Sendgrid, or you can follow this tutorial from Google to reduce security but allow this option: https://support.google.com/accounts/answer/6010255
Send the email to a real SMTP server. If you don't want to set up your own then you can find companies that will run one for you, such as Google themselves.
Create a project: django-admin.py startproject gmail
Edit settings.py with code below:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail#gmail.com'
EMAIL_HOST_PASSWORD = 'email_password'
EMAIL_PORT = 587
Run interactive mode: python manage.py shell
Import the EmailMessage module:
from django.core.mail import EmailMessage
Send the email:
email = EmailMessage('Subject', 'Body', to=['your#email.com'])
email.send()
For more informations, check send_mail and EmailMessage features in documents.
UPDATE for Gmail
Also if you have problems sending email via gmail remember to check this guides from google.
In your Google account settings, go to Security > Account permissions > Access for less secure apps and enable this option.
Also create an App specific password for your gmail after you've turned on 2-step-verification for it.
Then you should use app specific password in settings. So change the following line:
EMAIL_HOST_PASSWORD = 'your_email_app_specific_password'
Also if you're interested to send HTML email, check this out.
My site is hosted on Godaddy and I have a private email registered on the same.
These are the settings which worked for me:
In settings.py:
EMAIL_HOST = 'mail.domain.com'
EMAIL_HOST_USER = 'abc#domain.com'
EMAIL_HOST_PASSWORD = 'abcdef'
DEFAULT_FROM_EMAIL = 'abc#domain.com'
SERVER_EMAIL = 'abc#domain.com'
EMAIL_PORT = 25
EMAIL_USE_TLS = False
In shell:
from django.core.mail import EmailMessage
email = EmailMessage('Subject', 'Body', to=['def#domain.com'])
email.send()
Then I got "1" as the O/P i.e. Success. And I received the mail too. :)
What is the meaning of domain.com?
For Django version 1.7, if above solutions dont work then try the following
in settings.py add
#For email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'sender#gmail.com'
#Must generate specific password for your app in [gmail settings][1]
EMAIL_HOST_PASSWORD = 'app_specific_password'
EMAIL_PORT = 587
#This did the trick
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The last line did the trick for django 1.7
You need to use smtp as backend in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
If you use backend as console, you will receive output in console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
And also below settings in addition
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'urusername#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
If you are using gmail for this, setup 2-step verification and Application specific password and copy and paste that password in above EMAIL_HOST_PASSWORD value.
I found using SendGrid to be the easiest way to set up sending email with Django. Here's how it works:
Create a SendGrid account (and verify your email)
Add the following to your settings.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = '<your sendgrid username>'
EMAIL_HOST_PASSWORD = '<your sendgrid password>'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
And you're all set!
To send email:
from django.core.mail import send_mail
send_mail('<Your subject>', '<Your message>', 'from#example.com', ['to#example.com'])
If you want Django to email you whenever there's a 500 internal server error, add the following to your settings.py:
DEFAULT_FROM_EMAIL = 'your.email#example.com'
ADMINS = [('<Your name>', 'your.email#example.com')]
Sending email with SendGrid is free up to 12k emails per month.
I had actually done this from Django a while back. Open up a legitimate GMail account & enter the credentials here. Here's my code -
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
def sendmail(to, subject, text, attach=[], mtype='html'):
ok = True
gmail_user = settings.EMAIL_HOST_USER
gmail_pwd = settings.EMAIL_HOST_PASSWORD
msg = MIMEMultipart('alternative')
msg['From'] = gmail_user
msg['To'] = to
msg['Cc'] = 'you#gmail.com'
msg['Subject'] = subject
msg.attach(MIMEText(text, mtype))
for a in attach:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(a))
msg.attach(part)
try:
mailServer = smtplib.SMTP("smtp.gmail.com", 687)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, [to,msg['Cc']], msg.as_string())
mailServer.close()
except:
ok = False
return ok
You could use "Test Mail Server Tool" to test email sending on your machine or localhost. Google and Download "Test Mail Server Tool" and set it up.
Then in your settings.py:
EMAIL_BACKEND= 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
From shell:
from django.core.mail import send_mail
send_mail('subject','message','sender email',['receipient email'], fail_silently=False)
Late, but:
In addition to the DEFAULT_FROM_EMAIL fix others have mentioned, and allowing less-secure apps to access the account, I had to navigate to https://accounts.google.com/DisplayUnlockCaptcha while signed in as the account in question to get Django to finally authenticate.
I went to that URL through a SSH tunnel to the web server to make sure the IP address was the same; I'm not totally sure if that's necessary but it can't hurt. You can do that like so: ssh -D 8080 -fN <username>#<host>, then set your web browser to use localhost:8080 as a SOCKS proxy.
For SendGrid - Django Specifically:
SendGrid Django Docs here
Set these variables in
settings.py
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgrid_username'
EMAIL_HOST_PASSWORD = 'sendgrid_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
in views.py
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com', ['to#example.com'], fail_silently=False)
below formate worked for me
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True EMAIL_HOST = 'mail.xxxxxxx.xxx'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'support#xxxxx.xxx'
EMAIL_HOST_PASSWORD = 'xxxxxxx'
In settings.py configure the email as following
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
In most of the cases Gmail settings is missed out. Make sure that the less secure app access is turned on. You can also check the procedure here
Then send email within views
from django.core.mail import send_mail
def send(request):
send_mail(
‘Email Subject here’,
‘Email content’,
settings.EMAIL_HOST_USER,
[‘emailto#gmail.com’],
fail_silently=False)

Categories

Resources