I have separate django project and one python script file. The script file will not depends on project(that was available in different path).
The script file will generate some data and need to send mail to corresponding user. In this case i am trying to use django mail:-
from django.core.mail import EmailMessage, SMTPConnection
I enabled the django environment in my python script file and added the sys.path to my django project settings(So i can able to use django mail).
# setting a django environment
os.environ['DJANGO_SETTINGS_MODULE'] = "settings"
sys.path = sys.path + [PROJECT_PATH]
And the mail config code added in my script:-
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxx#gmail.com'
EMAIL_HOST_PASSWORD = 'xxx'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
After all this mail not working properly. Because the above parameters not in settings file(The actual problem is if i added this "EMAIL_USE_TLS" parameter itself in settings means it works properly other parameters taking from my script file.
My question is why "EMAIL_USE_TLS" this parameter is not taking from my script file like other parameters?
or
Is mandatory one "EMAIL_USE_TLS" this needs to specify in settings file?
If i not specified EMAIL_USE_TLS = True in settings means got below error:-
SMTP AUTH extension not supported by server
Please anyone advise on this.
Related
I am newbie!
I want to create a django application that sends mail. it worked well on localhost, but when I deployed it, it stopped working because the host doesn't support smtp. He suggested using sendgrid, and I'm stuck here...
views.py
error:
The error message in the image that you have attached here is showing 403 forbidden which means the server understands the request but refuses to authorize it. Check whether from email address is used when you are verifying your send address. read docs in this link https://docs.sendgrid.com/ui/sending-email/sender-verification
and check all your settings for sendgrid is correct or not.
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
I have a Docker container where I am trying to send an email via Django.
I have a separate email server on another domain that I want to use for this purpose. I have other applications connect with it with no problem.
My Django production email setup looks like this (I intend to replace username and password with Kubernetes secrets in the future, but for testing I am just putting them inside the file):
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.mydomain.io'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "<username>"
EMAIL_HOST_PASSWORD = "<password>"
In my module, I have the following code:
from rest_framework.views import APIView
from django.core.mail import send_mail
class MailView(APIView):
def post(self, request):
subject = request.data.get("subject")
message = request.data.get("message")
sender = request.data.get("sender")
receipients = request.data.get("receipients")
send_mail(subject,message,sender,receipients,fail_silently=False,)
... more
This works locally, however when I try to run it inside the container, I get the following error:
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.
Do I need to install some sort of SMTP relay or server to my Docker container?
My Docker container is based on the python:3.7 container and I am not currently installing any SMTP extensions or anything to it.
EMAIL_USE_TLS needs to be set to True
EMAIL_USE_TLS = True
This is my settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = 'xx#xx.com' # email id
EMAIL_HOST_PASSWORD = 'sdjlfkjdskjfdsjkjfkds' #password
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'xx#xx.com'
I am sending password reset email
It works fine on localhost but on production server as sender I get webmaster#localhost
What do I do?
This is my urls.py
url(r'^password_reset/$', auth_views.PasswordResetView.as_view(
template_name='accounts/password_reset.html',
email_template_name = 'accounts/email/password_reset.html',
html_email_template_name = 'accounts/email/password_reset.html',
subject_template_name = 'accounts/email/password_reset_subject.html'),
First I want to explain why you get specifically webmaster#localhost email.
Explanation
django.contrib.auth.forms.PasswordResetForm.save() function uses None as a from email address, webmaster#localhost is used by default, so you should just need to add DEFAULT_FROM_EMAIL property to your settings.py.
Solution
Connect to your production enviroment and run the django shell with python manage.py shell.
Now run the following commands.
# Check your enviroment, should say something like production.
import os
os.environ.get('DJANGO_SETTINGS_MODULE')
# Check if the DEFAULT_FROM_EMAIL property is set.
from django.conf import settings
settings.DEFAULT_FROM_EMAIL
With os.environ.get('DJANGO_SETTINGS_MODULE') you will get the settings file you are using for production, so if settings.DEFAULT_FROM_EMAIL isn't set just edit that file and add it with your email.
Recomendations
Use another email backend (mandrill, sendgrid, etc...) to ensure that your mails will reach the inbox and not the spam folder.
For this you can use django-anymail package.
References
How to create a password reset view in Django - simpleisbetterthancomplex.com
I'm developing a django app locally and trying to configure it to use the Amazone SES service to send emails. I've installed django-ses and added this to my settings.py:
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_SES_REGION_NAME = 'us-west-2'
AWS_SES_REGION_ENDPOINT = 'email.us-west-2.amazonaws.com'
AWS_ACCESS_KEY_ID = '...'
AWS_SECRET_ACCESS_KEY = '...'
Unfortunately, mail.get_connection() returns that it's still using django.core.mail.backends.console.EmailBackend; both in the shell and when the development server is running.
It behaved the same when I was attempting to go the normal smtp configuration route with django.core.mail.backends.smtp.EmailBackend too...
Any ideas as to why it's not making the switch?
According to the django docs the default value for EMAIL_BACKEND is django.core.mail.backends.smtp.EmailBackend, not django.core.mail.backends.console.EmailBackend, so it has probably been set again later in the settings file.
You can also print the value of EMAIL_BACKEND to make sure if the problem is in the function or the variable.
from django.conf import settings
print(settings.EMAIL_BACKEND)
I know that this question was asked a lot o times, but I have a little different situation. I am deploying my django app with implemented django-registration-redux on heroku. The registration works fine on local machine, but it gives smtpauthenticationerror 534 on heroku.
I ALLOWED less secure apps on my google account, but the error persist.
I clicked on DisplayUnlockCaptcha the button Continue- still doesn't work.
Here is the relevant part of settings.py:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'domain <example#gmail.com>'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
It seems like google banned the usage of it's accounts for automatic email-sending. The best choice in this case is just to use another service or create an email service by yourself