What am I unable to send email using Django? - python

I have turned on the 'allow less secure apps' on my google account.
But when I submit the password reset form, it goes to the password_reset_done.html and DOESN'T show any error. But the mail is not sending. It's neither in my sentbox nor in the inbox of the email it's supposed to send to.
This is what my urls.py looks like :
from django.contrib.auth import views as auth_views
path('password_reset/',
auth_views.PasswordResetView.as_view(template_name="my_app/password_reset.html"),
name="password_reset"),
path('password_reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name="my_app/password_reset_done.html"),
name="password_reset_done"),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name="my_app/password_reset_confirm.html"),
name="password_reset_confirm" ),
path('reset_password/',
auth_views.PasswordResetCompleteView.as_view(template_name="my_app/password_reset_complete.html"),
name="password_reset_complete" ),
And here is the settings.py file:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'my_g_mail_id_here#domain.com'
EMAIL_HOST_PASSWORD = '**********'
I have literally read every other question and tried everything. Nothing is working for me.
I am dying here to find out a solution, please help.

Don't use Gmail for this. It's not designed for programmatic email sending.
Instead, use something like SendGrid or Mailgun. Both have inexpensive or free starter plans.

I found the solution. I was not registering user's email ID's through the Djangos default registration form.
Tweaked it a little to add the email field in the form and then the users started recieving the email.
Nothing wrong in the conf file.

Related

Django getting SMTP Server URL as URL in the password reset email

I made a Django application with custom users.
I followed the docs to create my password reset pages and forms. I found this similar question and tweaked my configuration to use the right domain name. My SITE_ID is set to 1, and I'm sure the Site with pk=1 is configured to be my domain.
When I try to get a password reset email, the subject contains the correct URL for the site I am requesting the password reset for. (Subject is "Password reset for myMainDomain.com")
The problem is the link inside the mail: the auto-generated link points to a CNAME of my mailserver, for some unknown reason. Django shouldn't even know that, since the only place I put the mailserver's domain is in the mail settings in setting.py
My users.urls.py:
urlpatterns = [path('accounts/', include('django.contrib.auth.urls')),
path('', views.UserListView.as_view(), name='lista-user'),
path('accounts/signup/', views.Iscriviti.as_view(), name='signup'),
path('<int:pk>', views.Profilo.as_view(), name='profilo'),
path('accounts/edit/<int:pk>', views.Modifica.as_view(), name='modifica-utente')]
My template password_reset_email.html:
Here is your password-reset link:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
Relevant part of my settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.myMailServerdomain.it'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'MYUSER'
EMAIL_HOST_PASSWORD = 'MYPASS'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = False
DEFAULT_FROM_EMAIL = 'noreply#myMailServerdomain.it'
SITE_ID = 1 # -> This is the site I configured on Django, with my_site.domain = 'myMainDomain.com' and my_site.name = 'My site's name'
The email I get when I try a password reset:
Here is your password-reset link:
http://tracking.mymailserverdomain.it/f3ag34/Go6CyRfZ=0D
tracking.mymailserverdomain.it is a CNAME of my domain mymailserverdomain.it which I pointed to track.mail.mymailserverdomain.it. I tried to send an empty mail with {{domain}} inside to see if my SMTP server used that for some reason, but it doesn't and just puts {{domain}} in the mail.
How can I investigate this behavior?

SMTP Authentication Error with Django on Heroku

I am trying to send emails from my django app using the the gmail smtp servers. The emails are being sent when I run the application on my local server. But I'm getting an SMTP Authentication Error while using it on heroku.
Traceback - link
settings.py -
# Email configuration.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '***************#gmail.com'
EMAIL_HOST_PASSWORD = '************'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '******************#gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
And I have rechecked the account password, also I have enabled the access of less secure apps from my google account. But still I am getting this error.
Disabling CAPTCHA for clients
If you are not using 2-factor authentication and you have verified the credentials in your Python source are correct, follow these steps:
Login to gmail in your browser
Navigate to the DisplayUnclockCaptcha page.
Click the continue button, and you will see the message
Account access enabled Please try signing in to your Google account again from your new device or application.
Run your Python script - your login attempt should be successful.
Try using sendgrid, because gmail smtp has some problem. I too was unable to do using gmail smtp. But it worked perfectly using sendgrid. And this is also free for basic use.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'username'
EMAIL_HOST_PASSWORD = 'userpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'asdf#example.com'
Change from mail(#gmail.com) domain to your company domain .then it will work. we can't use from mail domain #gmail.com, DEFAULT_FROM_EMAIL = '******************#yourcompanydomain.com'

Django registration email not sending

I've been trying to get the django-registration-redux account activation email to send to newly registered users.
I've gotten all non-email related parts to work, such as loggin in/out and actually registering the user! When i register, it automatically logs my in as that user. But i never get the activation email.
I've tried various different things to try get this to work, I've followed some tutorials on setting whole thing up but the emails still dont work.
heres some of the code setup, im using registration templates that i downloaded online.
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'synths',
)
# user reg settings
REGISTRATION_OPEN = True
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login/'
# i tried including this line but still nothing
# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# email
# first i tried setting up the debbuging server with this CMD line
# python -m smtpd -n -c DebuggingServer localhost:1025
# i dont know if it worked!, i got no errors but the cursor just
# sat there blinking at me! i was expecting some output to tell me
# the server had started
# these were the settings i used for that
EMAIL_HOST = '127.0.0.1'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
# then i tried using my own address and smtp.live.com
EMAIL_HOST = 'smtp.live.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'myemailaddress#hotmail.com'
EMAIL_HOST_PASSWORD = '123123abcabc'
# still nothing
am i missing any important settings here?
urls.py
# included amongst my other urls
(r'^accounts/', include('registration.backends.simple.urls')),
seems all in order with the tutorials and documentation. like i said, registration works perfectly bar the emails.
one thing ive noticed is that you probably shouldn't have auto loggin = True if you want a user to activate their accounts, but commenting that line out didnt change anything, i still got logged in automatically after registering. Seems like a minor aside but maybe this has something to do with the emails not working?
i dunno, im lost with it. Either im missing some settings, the code doesnt work, python smtpd doesnt work, or my smtp.live.com settings are wrong!
any insigths greatly appreciated!
EDIT: when trying the 'reset password' email function i get this error
SMTPException at /accounts/password/reset/
SMTP AUTH extension not supported by server.
Request Method: POST
Request URL: http://localhost:8000/accounts/password/reset/
Django Version: 1.7.6
Exception Type: SMTPException
Exception Value: SMTP AUTH extension not supported by server.
Exception Location: C:\Python34\lib\smtplib.py in login, line 613
Python Executable: C:\Python34\python.exe
Python Version: 3.4.3
EDIT 2: using these settings i get the the password/reset/done page but recieve no actual email
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = '127.0.0.1'
EMAIL_PORT = 1025
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
will only display the email on the console.
Instead you should use
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Moreover it is more convenient to use a existing smtp server like gmail
For that you need to add these to your django settings file
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST ='smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'youruser#gmail.com'
EMAIL_HOST_PASSWORD = 'gmail app password' #This is not your gmail password.
EMAIL_USE_TLS = True
More help on the password can be found here
You may want to try adding a DEFAULT_FROM_EMAIL setting and setting these settings:
EMAIL_USE_TLS = True
EMAIL_USE_SSL = True
This will allow Django to use secure email-sending.
Check your urls.py file, and make sure you are using the hmac not the simple
urlpatterns = [
#...
url(r'^accounts/', include('registration.backends.hmac.urls')),
]
Also, in your setting.py, INSTALLED_APPS, make sure that the 'registration' is before django.contrib.auth.
INSTALLED_APPS = [
#.....
'registration',
'django.contrib.auth',
#...
]
I know this is an old question, but I thought it would help anybody else looking for the answer. You have setup your urlconf to use the one step registration. Below is a snippet from their docs -
This backend’s workflow is deliberately as simple as possible:
A user signs up by filling out a registration form.
The user’s account is created and is active immediately, with no intermediate confirmation or activation step.
The new user is logged in immediately.
If you want to see the emails in the console, use the following urlconf instead -
url(r'^account/', include('registration.backends.default.urls')),
Hope that helps.

Sending mail in Django 1.5 with Gmail smtp

I try to send mail with Gmail smtp in my Django App.
I followed some instructions to configure my setting.py file.
Here is my code snippet in setting.py file.
EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myusername#gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
then I tried these commands below to send mail to my own gmail account:
python manage.py shell
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Mail Test', 'This is a test', to=['myusername#gmail.com'])
>>> email.send()
it returned 1 to me, it should be successful.
However, I can't find the testing mail in my inbox in my gmail.
Are there anything which I missed to set?
I run my Django site on localhost in Debug mode. And the Django's version is 1.5.
Thanks in advance! Any suggestion is appreciated.
UPDTED:
I changed the following code
EMAIL_BACKEND = django.core.mail.backends.dummy.EmailBackend
to
EMAIL_BACKEND = django.core.mail.backends.smtp.EmailBackend
And, I have succeed to send the mail.
You email backend is set to dummy. Change it to smtp
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Documentation
Dummy backend just prints to the console, and does nothing

Pinax Email Verification not working

I have installed pinax on my local and I try to sign-up on it. but the email verification is not working, so I look over the internet on how to config about the email verification but failed.
I try also the forgot password but it requires the email address to be confirmed.
I also try to put this code in settings.py
EMAIL_HOST = '...'
EMAIL_PORT = ..
EMAIL_HOST_USER = '...'
EMAIL_HOST_PASSWORD = '..'
EMAIL_SUBJECT_PREFIX = '..'
EMAIL_USE_TLS = True
EMAIL_ADMIN = '...'
but still not working.
Does anyone have an idea on why my pinax's email verification is not working?
Did you check your spam folder?
Also some apps tell Django to silently ignore EMail sending errors.
Try this quick example in the shell you get from python manage.py shell and see if it shows you any errors.

Categories

Resources