Sending Emails through Django is not working in Nov 2022 - python

I have tried all the advice and guides already available but it is not working at any cost. Please refer below to know more.
Activated the 2-factor authentification required.
2-factor authentication activated
I generated the App password and used it correctly in the Django settings.py file. You can see there is an app password in the above image.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "xyz"
EMAIL_HOST_PASSWORD = "abc"`
And use the following code to send the email.
subject = "Email verification"
text = "Hi, This is the link to verify you registration email with our site {0}. Please paste the link to verify your account {0}/auth/verify/{1}/"
message = text.format(self.request.get_host(), self.token)
email_from = settings.EMAIL_HOST_USER
recipient_list = [self.request.data['email'],]
send_mail(subject, message, email_from, recipient_list)`
But still, I am getting the below error.
Error form smtp: smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials l9-20020a17090a150900b0020d48bc6661sm10003685pja.31 - gsmtp')
I have tried enabling less secure apps but it shows like this:
No change possible for less secure apps
I have also tried this link: https://accounts.google.com/DisplayUnlockCaptcha
enter image description here

Related

"Relay access denied" in django app using namecheap private email

So I have a Django app that sends confirmation emails to users who want to register their account. It looks something like this
views.py
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 with settings.py looking like this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_FROM_USER = 'verification#ianisdo.xyz'
EMAIL_HOST_PASSWORD = '[redacted]'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
The problem is that, when I try to send a email when the user creates an account, I get this error
SMTPRecipientsRefused at /register/
{'ianis.donica#gmail.com': (554, b'5.7.1 <ianis.donica#gmail.com>: Relay access denied')}
What I know for sure is:
that it's not a problem with the private namecheap email, from which
I can still send emails just not from my website.
I also know the problem is not due to gmail not liking my email, as
the same error when the email is sent to a yahoo.com domain.
I also know that the issue is not with the settings.py not connecting
to the views.py
I also know that all the details are entered correctly
From my knowledge and talking to namecheap support, the issue is most likely caused by the header of the email but to my knowledge I can't seem to find anything wrong with it.
If someone is able to help me out, I would really appreciate it. I have had issues sending emails for about 9 days now
I think you need to add a value for EMAIL_HOST_USER in your settings.py. Without it, Django won't attempt to authenticate. Here's the relevant bit from the docs:
EMAIL_HOST_USER
Default: '' (Empty string)
Username to use for the SMTP server defined in EMAIL_HOST. If empty, Django won’t attempt authentication.
The username should match the password setting you're already providing (i.e., a valid namecheap private email account).

Django Email sending SMTP Error, cant send a mail

I am trying to send email within my Django project:
# settings.py
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
def notify_user(email_address, subject='Subject', message='Some message!') -> None:
send_mail(
subject,
message,
settings.EMAIL_HOST_USER,
[email_address],
fail_silently=False
)
As I know I need to disable this option (Gmail Security), but Gmail don't let me do it.
As a result I got this error:
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f14-20020a056512360e00b00492bbf7db8asm320697lfs.85 - gsmtp')
What can I do? How to disable that option?
Google disabled/discontinued the Less Secure App feature as of May 30th, 2022.
As recommended by #MeL, you could use an alternative but you can still use Gmail to send emails using Django. You can read this blog Mail setup on django using Gmail to help with guiding the setup if you're unfamiliar with setting up Google App Passwords.
In addition to that, you'll need to do one last thing to activate the app password to be used by Django. You should visit the site https://accounts.google.com/DisplayUnlockCaptcha, select continue and you're all set.

Django Reset PW emails not sending on AWS

Difficult to phrase this question and what I've done as I don't know where the error is occuring.
I'm using Django hosted on AWS Elastic Beanstalk and SES to send emails. (Only for password reset) However the password reset emails don't seem to be sending.
When I try to send an email as below however it works.
send_mail('Test', 'Email content', 'email#email.com',['email#email.com',])
Also locally the password reset email sends (its put in spam but that's a different issue)
My email settings are:
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = 'email#email.com' # email id
EMAIL_HOST_PASSWORD = '****' #password
EMAIL_PORT = 587
# EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'ACCESSKEY'
AWS_SECRET_ACCESS_KEY = 'SECRETKEY'
AWS_SES_REGION_NAME = 'eu-west-1'
AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com'
DEFAULT_FROM_EMAIL= 'email#email.com'
If it's relevant, anywhere I've written email#email.com is actually the same email.
Any help would be much appreciated.
Thanks

Django and AWS Simple Email Service [duplicate]

This question already has answers here:
Amazon SES SMTP with Django
(8 answers)
Closed 10 months ago.
I'm attempting to get a django site up and running and I'm trying to enable django's standard password reset service.
My site is hosted by AWS EC2, so I figured I would use AWS SES for my email service. However, I can't get the smtp connection to work. Any idea on how to solve for the following error:
Exception Type: SMTPSenderRefused
Exception Value: (530, b'Authentication required', 'example#example.com')
I've looked at the following, but I'd prefer to stay with django's built-in email back-ends if possible:
https://github.com/django-ses/django-ses
https://github.com/azavea/django-amazon-ses
Amazon SES SMTP with Django
Email Settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_PORT = 587 # (I've also tried 25, 465, & 2587)
EMAIL_HOST_USER = ''
EMAIL_PASSWORD = ''
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'example#example.com' # but my real email address
My EMAIL_HOST_USER and EMAIL_PASSWORD are set to my SMTP credentials I obtained by following the instructions at: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
Also, I was sure to verify my DEFAULT_FROM_EMAIL and my domain following these instructions: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html
And DKIM is enabled and my nameservers have been updated.
Any help would really be appreciated.
UPDATE:
I was able to get the django email send working with the django-ses 3rd party library, but I'm still not able to figure out why the standard backend doesn't work via SMTP.
I think there is definitely something wrong with my code as I went so far as to switch email clients from AWS SES to Zoho mail and I'm still receiving the same 530 Authentication required error.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'support#example.com'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'support#example.com'
EMAIL_PASSWORD = ''
If it is asking for Authentication, please double-check the following:
Your SMTP settings
The email that you're trying to send mails from belongs to the verified domain. e.g. if you have verified example.com but you try sending emails from something#gmail.com, it is going to ask for gmail auth credentials.

How to implement e-mail verification on sign-up, but not for social accounts in Django?

I am using Django-allauth for my authentication purposes. I currently have these settings:
ACCOUNT_EMAIL_REQUIRED=True
ACCOUNT_EMAIL_VERIFICATION = ("mandatory")
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'me#gmail.com'
EMAIL_HOST_PASSWORD = '********'
This works nicely. However, I also give my users the option to use their social accounts (Facebook and Twitter) to login. If they are logging in for the first time, then they are asked for an email and the system waits for verification. I do not want this. If the user logs in using a social account, he/she should not need to verify their emails.
So how can I change that? Once again, I do want an email verification, but ONLY if the user signs up using the sign up form, if they use a social account, they are directly logged in. How do I do this?
Thanks!
SOCIALACCOUNT_EMAIL_VERIFICATION = "none"
There is this setting available.

Categories