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

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).

Related

Sending Emails through Django is not working in Nov 2022

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

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.

Using Outlook SMTP server to send email through a contact form but unable to send as I need the server to be able to accept multiple "From" addresses

Currently using Django as the chosen framework for my project and I have implemented a contact form and my main goal is for users to complete the contact form and the admin of the site (me) get an email, which shows me the details of their enquiry.
I am trying to use the Outlook SMTP server and these are my current settings in settings.py:
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '<my_emailAddress>'
EMAIL_HOST_PASSWORD = os.environ.get('OUTLOOK_PASSWORD')
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = '<my_emailAddress>'
However, whenever I complete the form and send the request to the server I am receiving the following error code:
(554, b'5.2.252 SendAsDenied; <my_emailAddress> not allowed to send as <inputtedForm_emailAddress>; STOREDRV.Submission.Exception:
SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.
I am looking for some help surrounding this issue - it would be greatly appreciated.
Recently I had same issue with Outlook.
Looks like Outlook is not accepting anymore sending emails from unsecure apps.
Your next alternative is to use gmail but first enable unsecure apps in gmail setting.
Everything should be the same, change only smtp to
​EMAIL_HOST=smtp.gmail.com

Django mail_admins vs send_mail

I can get my django application to properly report server errors by email using the obvious settings :
DEBUG = False
ADMINS = (('name','email'),)
MANAGERS = (('name','email'),)
EMAIL_HOST = 'ip address'
EMAIL_HOST_USER = 'user'
EMAIL_PORT = 25
EMAIL_HOST_PASSWORD = 'pwd'
SERVER_EMAIL = 'server-email'
Yet for some reason, when I try to use send_email() it returns either Authentication errors if I try without TLS, or "You don't have permission to send as this sender" if I do use TLS. I can't understand why error reporting works, and send_email doesn't... it uses the same settings by default.
Any suggestions ?
There are two settings which affect the 'from' email address for different types of emails.
SERVER_EMAIL - used for sending error emails to ADMINS and MANAGERS.
DEFAULT_FROM_EMAIL - used for sending regular emails
It looks as if you have already set SERVER_EMAIL, so make sure you have set DEFAULT_FROM_EMAIL as well.

Django Email settings without using From Address

Here is my settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "mygmail#gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
EMAIL_PORT = 587
and view.py code
From='someemail#mail.com'
send_mail(Subject,Message, From, [To])
every time when a mail is sent it shows mygmail#gmail.com ,but i need it show someemail#mail.com ,how to fix this??
Did you add your "someemail#mail.com" to your gmail account?
Google have a procedure to allow their SMTP to send email as an other, they require you can find a code they send to this given email.
Otherwise it's normal.
(Security way to avoid malicious users tu use google's SMTP to send email as "barack.obama#whitehouse.gov")
Google only allows you to send email from addresses you have registered on your Gmail account. So someemail#mail.com will not work unless that's a registered gmail account. Though I suspect you are trying to use it as mask from some user in your app to send mail as themselves.
An alternate way to do it is to use something other than Gmail (such as Mailgun, Sendgrid, etc). For best practices, send the email from a legitimate registered email address, but utilize the reply-to field for custom emails.

Categories