Django not sending error messages to email - python

I used to be able to do this, not sure why it seems to no longer work. I want Django to email me whenever an error occurs like 500 server error, which would be useful since the email usually includes the detailed error description.
Here's one error I'm letting persist for now shown in chrome's terminal as I try and get this feature to work.
here's what I have in my settings file
ADMINS = ['<my_email>']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = SERVER_EMAIL = '<server_email>'
EMAIL_HOST_PASSWORD = "xxxxxx" #
EMAIL_USE_TLS = True
so even though this is an error it doesn't send any emails to my mail.
I can however send emails from the shell using
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'<server_email>',
['<my_email>'],
fail_silently=False,
)
from the same server

ADMINS settings need to be a list of tuples with name and email in each tuple.
Example : ADMINS = [('John', 'john#example.com'), ('Mary', 'mary#example.com')]
Reference : https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-ADMINS

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.

Django Rest Framework Send verification email

Is there any way to send a verification email. when an user registers through drf (Django Rest framework). I have this code in my User model:
from django.core.mail import send_mail
from config import settings
def email_user(self, subject, message, from_email=settings.DEFAULT_FROM_EMAIL, **kwargs):
send_mail(subject, message, from_email, [self.email], fail_silently=False, **kwargs)
and in my settings.py:
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = '`132312123'
EMAIL_HOST_USER = 'mimic#gmail.com'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
But email not being sent, also I want to verification email service, so user will be activated when they click on the link. How to accomplish these?
If you look here:
Console backend
Instead of sending out real emails the console backend just writes the
emails that would be sent to the standard output. By default, the
console backend writes to stdout. You can use a different stream-like
object by providing the stream keyword argument when constructing the
connection.
Also:
This backend is not intended for use in production – it is provided as
a convenience that can be used during development.
Try this one instead:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

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

send_mail is clearly sending email but no email is showing up in inbox

I have these email settings in my settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email#myemail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
and am using this function to send email to recipients.
def send_email(subject, body, recipients, agent_email, bcc=[], attachments=[]):
recipient_list = []
if isinstance(recipients, (str, unicode,)):
recipient_list.append(recipients)
else:
recipient_list = recipients
recipient_list = recipient_list + bcc
send_mail(subject, body, settings.EMAIL_FROM, recipient_list)
while it looks quite clear when I go to the django-admin site that the email was indeed sent and no error messages show in the log files whatsoever, when I go to check the email that it was sent to, nothing shows in the inbox. I would expect to see the email there, especially given that it shows as sent in django-admin. Have I misunderstood something about how email is sent from the system?
EDIT
I also checked my spam folder and added
EMAIL_FROM = 'email#myemail.com'
because I noticed it wasn't there before. Same results, though. Email appears sent according to django-admin but no email in my inbox.
Try to add the following in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Categories

Resources