SMTPAuthenticationError on Heroku - python

I am trying to request a reset password from a system deployed on heroku but i get this error.
SMTPAuthenticationError at /password-reset/
(534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs\n5.7.14 i1cE5v2I-3UUFLl6jQMnBfbcvZuRiGo_q9k1T5h2XgMGxtKYIJ0sIPE9Jr6zW1X78xQM3\n5.7.14 Py7mrOXSPkPG01upxjbZRi68aLrKIbvG_4uHrli9l7ZVmFnr8CxRNb9JUDGVcm8M>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 k188sm1479373qkd.98 - gsmtp')
I have set all these settings and they seem to be okay but i still get the same error. Kindly help
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

Your application is probably fine, but the issue is with Google, that is preventing your login as it is a potentially suspicious activity. You may have received a mail from Google telling you that a suspicious activity has happened.
To solve this problem, you have to change your Google's security settings and create an app password so you can login from your application:
Using a browser, login to your Gmail account
Go to "Manage your account"
Go to "Security"
In "Signing to Google" section, click on "App passwords"
Enter your password
In "Select the app and device you want to generate the app password for.", select Other, name it anyway you want and click "Generate"
Use this password in Django instead of your regular password.

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

How did Django send_mail work without EMAIL_HOST_PASSWORD?

I tested Django send_mail() function which I needed for a project. I had the EMAIL_HOST set to the SMTP server provider my company uses, EMAIL_PORT to 587, EMAIL_USE_TLS to True. Both EMAIL_HOST_USER and EMAIL_HOST_PASSWORD was set to "" (empty string).
Then I used the send_mail as:
from djanog.core import mail
mail.send_mail(
"Django mail sample",
"Body",
"myemail#example.com",
["myemail#example.com"]
)
But calling the view actually sent the email to my mail from my mail, but I didn't provide any password at all. I also set recipient_list to my colleague's mail, it worked. Then I tried to change the from_email to my colleague's mail it didn't work.
I have my mail setup in Thunderbird in my system.
So how did Django sent the mail without my email's password? Was it because of my setup in Thunderbird? What happened exactly?
Update:
My colleague tried the same in his system, which doesn't have any mail configured. When running the result was an SMTPRecipientsRefused.
I used both my Phone's network as Hotspot and my company's Hotspot both worked. But in my colleague's system nothing worked.
When setting EMAIL_USER_HOST and EMAIL_HOST_PASSWORD in my colleague's system it worked.

Securely send email from custom gsuit domain in python Django

I have two gsuit emails :
admin#mydomain.com -- this is the main admin
user#mydomain.com
I want to securely send email from user#mydomain.com using python3.7
I created app_password for user#mydomain.com and tried the following code in Django:
settings.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user#mydomain.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxx'
EMAIL_PORT = 587
main function:
message = render_to_string('app1/email.html', {
'fn': ip_dict['first_name'], 'ln': ip_dict['last_name']
})
send_mail(
'hello world',
'',
'user#mydomain.com',
[ip_dict['email']],
html_message=message,
)
I get error :
SMTPAuthenticationError at /url/
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials v5sm4332617otk.64 - gsmtp')
With app_password the level of security is low.
With google gmail API https://developers.google.com/gmail/api/quickstart/python the app opens a browser and makes me physically sign in. Is there any other secure way to send an email automatically from gmail .
Thanks
Detailed solution in a Jupyter notebook hosted on Gitlab:
Link

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

How do I configure django settings to send email to Ibm lotus Notes

I have an html page with a form where I collect some data like mail_sender, mesage_to_send, subject etc.
When a user click on a button that information is submitted to a views.py function:
from django.core.mail import send_mail
def register_new_request(request):
email_person = request.POST.get('email_person', False)
send_mail('Test', 'Here is the message.', email_person,['tosomeone#example.com'], fail_silently=False)
return HttpResponseRedirect("/")
How i configure in my settings.py to send email to lotus notes via that function?
After some research i found that are some variables that need to be configured like :
EMAIL_USE_TLS = True
EMAIL_HOST = ''
EMAIL_PORT = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
Can tell me someone how exactly should look that variables? Thanks !
django.core.email sends SMTP mail. There is absolutely nothing special about sending SMTP email to Lotus Notes users. It looks exactly like sending email to any other email user on the Internet.
If your real question is about sending to a Lotus Notes user from within that user's internal network, presumably by submitting your SMTP message directly to a Lotus Domino server, then you need to talk to the administrator(s) responsible for the servers and find out (a) if SMTP is enabled on at least one internal Domino server, (b) what host and port to use to connect via SMTP to the Domino server, (c) is SSL/TLS supported or required for the SMTP connection to the Domino server (d) whether authentication is required, and if yes what user name and password your code should use.

Categories

Resources