Django Mailer not finding server - python

I'm trying to send an email through Django's wrapper.
Here are my relevant settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The Email, which I am trying to isolate in the most basic form of a view:
from django.core.mail import send_mail
def index(request):
subject = 'Subject'
message = 'message'
from_email = settings.DEFAULT_FROM_EMAIL
send_mail(subject, message, from_email, ['email#example.com'])
return render(request, "index.html")
All of the emails and password are legitimate. When I execute the code, I get thrown an error message:
SMTPAuthenticationError at /....*Link to sign into my account*
Please log in via your web browser and\n5.7.14 then try again
I did that but continue to get the same message. The password I give in the app is correct. Is there anything I need to configure in my gmail account?

Change EMAIL_HOST = 'smtp.gmail.com ' toEMAIL_HOST = 'smtp.gmail.com' and I bet your problem will disappear :)
EDIT #1
You are running into authentication issues as EMAIL_USE_TLS is True and Gmail only requires TLS connections for SMTP on port 587. Change to EMAIL_PORT = 587 and you should bypass the issue.
EDIT #2
The error you see can be fixed through your Gmail settings. See -
Django SMTPAuthenticationError

Related

530, b'5.7.0 Authentication Required Error when using gmail to send emails through django

Im having problems with sending emails through gmail in Django. I have set up a app password and yet I cant seem to send emails through Django. My settings.py look like this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_FROM_USER = 'ianis.donica#gmail.com'
EMAIL_HOST_PASSWORD = 'my app password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
To my best of knowledge it isn't a gmail specific issue, as I had experienced the same problems across yahoo mail and Sendgrid, the function that's responsible for sending the email looks like this
def send_activation_email(user, request):
current_site = get_current_site(request)
email_subject = "Activation Email"
context = {"user": user,
"domain": current_site,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': generate_token.make_token(user)
}
email_body = render_to_string('email/activate.html',context)
email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_FROM_USER, to=[user.email])
email.send()
and the full error message is this
SMTPSenderRefused at /register/
(530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError g9-20020a170906394900b00872a726783dsm9975622eje.217 - gsmtp', 'ianis.donica#gmail.com')
What I tried was changing to yahoo and SendGrid mail but the same issues occurred there, just with different names. I also tried changing some details but that shouldn't be the problem? Yet I cant seem to send an email anywhere. If anyone can help me I would really appreciate it
I also have IMAP enabled
The problem was with me using EMAIL_FROM_USER instead of EMAIL_HOST_USER, so I would need to change the code to this
settings.py
...
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'ianis.donica#gmail.com'
EMAIL_HOST_PASSWORD = 'my app password'
...
views.py
...
email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_HOST_USER, to=[user.email])
...
This is because without EMAIL_HOST_USER, Django won't try to authenticate

Using Django to send Gmail

This is very much potentially a duplicate question, but none of the other obvious duplicates have resolved the issue for me:
This is an inherited project.
My settings.py includes:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_username#gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'my_username#gmail.com'
DEFAULT_FEEDBACK_EMAIL = 'my_username#gmail.com'
SERVER_EMAIL = 'my_username#gmail.com'
ACCOUNT_EMAIL_VERIFICATION = 'none'
The code that I'm trying to run is:
subject = 'Subject'
template = get_template('accounts/email-templates/email-activation.html').render(Context(ctx))
email = EmailMessage(subject, template, to=[send_to])
email.content_subtype = "html"
try:
email.send()
My error when trying repeatedly with python manage.py shell is:
gaierror: [Errno 8] nodename nor servname provided, or not known
My dns seems fine, sudo killall -HUP mDNSResponder and dscacheutil -flushcache have been run without success, but I'm hardly an expert on dns settings.
My hosts file is:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Advice appreciated!
You might need to generate an app password in Gmail or change the settings to allow less secure access. I don't think Gmail will allow you to just use your regular password in - what Google would consider - an insecure manner.
I had similar problem working with Gmail to send mails.
Try only the following codes to send emails. Hope it helps:
In settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "sender_email#gmail.com"
EMAIL_HOST_PASSWORD = "email_password"
EMAIL_PORT = 0
EMAIL_USE_TLS = True
Codes to send emails:
# Imports for sending emails:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, send_mail
emailFrom = [settings.EMAIL_HOST_USER]
emailTo = email_here
text_content = 'Your content here'
email_subject = "Subject here"
msg = EmailMultiAlternatives(email_subject, text_content, emailFrom, [emailTo], )
msg.send()
Also change the settings in the 'sender gmail account' to create a passage for the mail to be send from the sender to the receiver. Hope this helps.

Authentication failure using Zoho smtp with Django

I am trying to send email using my django app. But after setting Zoho account and adding necessary lines in settings.py I am still not able to send email and it keep giving SMTPAuthenticationError (535, b'Authentication Failed').
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'administrator#technovate-iiitnr.org'
EMAIL_HOST_PASSWORD = 'mypass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
#views.py
html = render_to_string('email/code_email.html',{'code':code})
send_mail('Your Code',
'Hello',
'administrator#technovate-iiitnr.org',
['example#gmail.com'],
html_message=html
)
return render(request,'index.html')
One of the reason could be 2-factor authentication being used in the zoho account.
This prevents you from using account password, instead you need to generate application specific password and use the same.
Read more here

HTML based Email sending not working in Django with SMTP

This is what I ran in the django python shell
from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'Test<br>break', 'sender#mysite.com',['receiver#gmail.com'])
email.content_subtype = "html"
email.send()
email.send() returned 1 in python shell, When I checked my sender email I have not received any mail
I am not sure what that 1 means
Here is my settings.py file mail settings
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'sender#mysite.com'
EMAIL_HOST_PASSWORD = 'mypass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
So email.send() returned 1 meanings it's a successful mail.
The mail went to Spam box in Gmail
When I wrote actual content that I want to send it came to my inbox.

in send mail from is not working in django

i am trying to send the mail in django. mail is going properly but mail is going by EMAIL_HOST_USER. Want to send the mail using from i.e. from some other email address.
settings.py
EMAIL_HOST ='smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'you#everycrave.me'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_USE_TLS = True
in view:
text="hi this is test mail"
send_mail('Codeville Signup', text.decode(), 'gaurav#everycrave.me', ['manish#everycrave.me', 'jagat#everycrave.me'], fail_silently=False)
i want to send the mail from "gaurav#everycrave.me" but mail is getting sent by "you#everycrave.me"
How can i overcome this problem. And i dont want to change EMAIL_HOST_USER mail address.
Guide me through this
You can refer EmailBackend for sending email through multiple SMTP in Django this question or
in your view you have to write this code, from where you are sending the email.
from django.core.mail import get_connection, send_mail
from django.core.mail.message import EmailMessage
#TODO: Insert clever settings mechanism
my_host = 'smtp.gmail.com'
my_port = 587
my_username = 'your email address'
my_password = 'password'
my_use_tls = True
connection = get_connection(host=my_host,
port=my_port,
username=my_username,
password=my_password,
user_tls=my_use_tls)
EmailMessage('Test subject', 'test message', 'from_email', ['to'], connection = connection).send(fail_silently=False)
Check this.

Categories

Resources