Gmail SMTPAuthenticationError when I attempt to send email via django - python

I'm using django-registration-redux and the forget password reset dialogue is working on my machine but it fails on the server. I get an SMTPAuthenticationError even though the local server is sending mails just fine. What could I be doing wrong?
This is the error I get:
(534, '5.7.14
<https://accounts.google.com/ContinueSignInsarp=1&scc=1&plt=AKgnsbtwe\n5.7.14 Dz3CKtLMnsuR5AJAalqHTpUCJLECZAJKqbimLhjZ3dzjnEOfMck4BnpTZdlLbVRnkocGVe\n5.7.14 ghl_iEoinCscpmC_RWk8CxJ8MP61MyYKtKljt8gnet-NUj53mRP5ohGJsok8V1z1SWXzGa\n5.7.14 MrBjYMYDHU9MbPFopv5jWzFDQS_4Jop03AACxQAcSD1jGG1uQqFXjULQ2RE-wMPrCcyDKW\n5.7.14 Vq08guBEY2LqcSBp9L3jauD-a9i4>
Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 ra1sm18942520pab.24 - gsmtp')

If you have activated two-step verification, you need to turn that off. Also ,you can try another thing. Go to https://www.google.com/settings/security/lesssecureapps and https://myaccount.google.com/security?pli=1#connectedapps, allow access for the less secure app. Google might consider your server's sign-in process as less secure sign-in technology, which makes your account more vulnerable. So allowing access might help you.

It is because Google blocks sign-in attempts from apps that do not use modern security standards (mentioned on their support page). You can, however, turn on/off this safety feature by going to the link below:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
Moreover, google blocks your IP when you try to send an email since its an unusual location, so, you can unblock in the next link
https://support.google.com/accounts/answer/6009563
and clicked in
https://accounts.google.com/b/0/DisplayUnlockCaptcha
More info…smtp mail

Related

How do I send myself an email using Python?

I know this has been answered before, but recently gmail updated the "Less secure apps" ToS or something. So with this update, is there a way to do this? (Sorry for short explanation + old duplicates)
Tried this tutorial and when trying to connect to my google acc, less secure apps option disabled
UPDATE: I'll be using chrome notifications, which is useful for what I'm doing. Thanks for trying to help! :D
You can or should be able to use the same code that you are using now. The easest solution would be to enable 2fa on your google account and create an apps password. Quick fix for SMTP username and password not accepted error
Once you have created the apps password you can use that in place of your standard gmail password in your code.
If that doesn't work you can switch to using Xoauth2 and authorize a user and send an access token instead. This method would probably require changes you your existing code.

Google disabled access to less secure apps

With google having disabled the access to enable access to less secure apps can anyone suggest ways in which I can send mails using flask-mail? It has to be disabled in the mail account I'm using to send mails using flask-mail.
If your problem is about google security you can use app password instead of your account password in MAIL_PASSWORD configuration, To do that you should enable 2-step verification in your Google Account and then use app password,
So go to your Google Account,
Security
Signing in to Google
Enable 2-step verification
App password
Make app password and use it as your MAIL_PASSWORD
In link below you can see other options
https://support.google.com/accounts/answer/6010255?hl=en&utm_source=google-account&utm_medium=profile-less-secure-apps-card
I had a similar problem using sendmail and ESMTP with Google Mail,
and the excellent 'sender_based_routing' patch from:
http://faculty.cs.niu.edu/~rickert/cf/
which allows sendmail to choose a different relay based on
email 'From:' headers; using my MS-365 corporate email selects
smtp.outlook365.com , while using my personal email selects
smtp.google.com.
Because I am out of mobile phone coverage while at home, I cannot
enable 2-Factor authentication or use an App Token, which requires
2-factor authentication and the ability to receive SMS; my
mobile phone package does not include SMS-over-WiFi, and
Google's 2-factor Auth SMS messages are not sent over Google Chat SMS.
The only workaround I have found is to open a new Google WorkSpaces
email account, then you can enable the SMTP relay feature, and
sending email with sendmail + ESMTP then works again.
Google has thus doubled its revenue stream from me; and since I must copy every message sent by my new account to my old one,
it has doubled my storage requirements, that I pay for.
Clever Google! Never neglects an opportunity to squeeze more
profit from its users.
Anyway, paying for 2 email accounts in order to send emails is
vastly preferable to be being forced to use the horrible GMail
GUI - I can use MA/GNUS Emacs to send mails - definitely worth it.
The best long term solution, which I will do as soon as I can,
is setup one's own Email server , and scrap use of Google altogether - I wish I was in a position to do this, as soon as
I am I will - it requires a static IP and an always-on server.
so you need to set up the App Password from setting.
This video shows how to set up App Password = link.
setps:
1. Manage your Google Account
2. Goto the Security. on left side
3.At security->Signing in to Google
1 first need on 2-setp-verfication.
2 then click App Password.then select app and device.
for me app=Mail.
device=Windows Compter.
3 A password is genrated. copy the password. It only show one time
4 used this password to login.
Note: Before proceeding please enable 2 factor authentication.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) is no longer available.
We can use apppasswords feature provided by google via following link
https://myaccount.google.com/u/0/apppasswords
Check below image for reference, Use 16 digit code provided by google in-place of password.

SMTPAuthenticationError 5.7.14 Please log\n5.7.14 in via your web browser

I have a script which sends periodically reports to a list of recipients. Everything worked fine until today 4 am, when I checked my inbox and the reports didn't come.
By debugging the code:
import smtplib
username="my.user.account#gmail.com"
password="my.correct.password"
server=smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
#if login worked, it should send a message, but it is not working, so I will suppress this part
server.quit()
I receive the following (old known) result:
(250, b'smtp.gmail.com at your service, [SERVERIP]\nSIZE
35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
(220, b'2.0.0 Ready to start TLS') (250, b'smtp.gmail.com at your
service, [SERVERIP]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2
PLAIN-CLIENTTOKEN OAUTHBEARER
XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
Traceback (most recent call last): File "<pyshell#52>", line 6, in
server.login(username,password) File "C:\Python\Python36\lib\smtplib.py", line 729, in login
raise last_exception File "C:\Python\Python36\lib\smtplib.py", line 720, in login
initial_response_ok=initial_response_ok) File "C:\Python\Python36\lib\smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, b'5.7.14
<https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu5\n5.7.14
wJL_YP3XLTGPzP_UBwtyCHhzHE1y1G8R0iHaz_pxG9fBH4CePmbXuAE1qH8HvnXTgqcmKi\n5.7.14
P23_lJoZX_pfbNUOxQp4Fr2VhgbAjnlu3ZL_pcjVBd-TTMUVCAah6Q2-Vq5Dffm9s4UWwl\n5.7.14
J7V7CjE06eHkw1IlphV4lLbRKVp9Hk7vC92zLQ5zM27cbQiTM8W3lEgm> Please
log\n5.7.14 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
r131sm2964505vkr.40 - gsmtp')
Things I've tried
Allow less secure apps (as indicated in https://www.google.com/settings/security/lesssecureapps): This flag was set to true since I created the account.
Disable Captcha (as indicated in https://accounts.google.com/DisplayUnlockCaptcha): Visited this URL several times. No luck.
Login via browser: It works fine, when logging via any Internet Browser from any computer, although in the first times I had to insert a code sent to the account's recovery phone.
I came across other things I could try, but I am not sure it could work:
Specific app passwords
Oauth2
Change or reset password
I will probably try oauth2, however it would require a change in my code, which I want to avoid due to some restrictions in the server.
I recently got this problem. I've been using the same script for 3+ years and it suddenly stopped working. The above solutions didn't work for me. After contacting google here's what solved the issue (note it can take a few hours for the changes to propagate - up to 24 hours but usually not that long)
ALLOW low secure apps in Google settings (no effect on own but may be related to the full solution)
Try the unlock captcha link: https://accounts.google.com/DisplayUnlockCaptcha (no effect on its own but may be related to the full solution)
Admin Console > Apps > G Suite > Gmail > User Settings > Confidential Mode --> DISABLE Confidential Mode
Admin Console > Apps > G Suite > Gmail > End User Access --> TURN ON "Allow per-user outbound gateways"
I had this issue as well. My mail forwarder script that has been working fine for 6+ months randomly started throwing this error:
534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbts\n5.7.14 y2ku2-8KrWsn_dAUhnkssuTuESwaeG-DNn9hWBpsSpJCmVL ZhH24jzJfd-5bHUdh993dJ7\n5.7.14 a5LsAOJrheyT0cRXIhMSbycqvMAC7sNrizO5qU_6iDescJcQa-QqUUCFVH7IZoWF9uXyrv\n5.7.14 HC9XzAW-pBwgUjV4i_ts8CNhB_yZf2JHOi-wl6gZ9jwAiYLWx DPe7epI> Please log\n5.7.14 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 y5sm3059043 9pge.49 - gsmtp
After some research I tried all the following steps:
Checking allow low secure apps in Google settings (it was already on, tried turing it off and back on)
Trying the unlock captcha link: https://accounts.google.com/DisplayUnlockCaptcha
Singing in and out of Google account manually (verifying with phone number)
Changing the account password
Enabling and disabling 2FA
Countless other things...
Nothing seemed to actually work at first. However, after doing all the above steps, the script started working again ~24 hours later without any modifications.
I have solved this using the Google Gmail API. It seems Google has either blocked or limited logins for my account through unknown devices, as I could login only via the browser and after I confirmed the telephone number and an code sent by SMS.
So I decided to give up using smtplib and implemented the access using the API Google uses for Gmail:
https://developers.google.com/api-client-library/python/
https://developers.google.com/gmail/api/guides/sending
Hope this helps other who had trouble with this issue.
The solution to this seems to be:
Confirm that recent logins really were from you on the Google account security page.
Unlock the account by entering a captcha.
Doing this should unlock the account, though it can take up to an hour to take effect.
In my case, I was using the account from a server in the cloud which I was accessing via SSH. I have to make an SSH tunnel to access from my local browser to the corresponding Google URL as I was accessing it from the SSH terminal:
Create an SSH Tunnel: ssh -D 9090 -C -N user#server-url
Go to settings in Firefox, search proxy option, and set up the SOCK server to 127.0.0.1 and port to 9090:
Now you can check that your current IP is the same as the remote server in myip.com.
Now access the Google URL to log in with the email account.
In my case, it was necessary to access https://www.google.com/accounts/DisplayUnlockCaptcha too
If you have signed in with multiple accounts on the same device you would have to sign out from all your accounts and copy and paste this https://accounts.google.com/b/0/DisplayUnlockCaptcha in your url. You will be directed to your gmail sign in page.Choose the account you used in the script, enter your username and password. The link will direct you to the captcha page and click on continue. Make sure you don't close the tab after unlocking captcha and run your script. You don't have to always unlock captcha if this works for the first time.
Try changing the password of the email and re-run the script with the new password. This worked for me. You probably have received a Sign-in attempt email and make sure you verify the login your script made.
Even i had this issue just try to do these steps because google initially blocks connection without any reason:
try to turn on and off Less secure app access.
log out to your account and login again
Change your password and hit your script
and at last print acknowledgment like below:
print('Your message has been sent!!')
if this doesn't work please login after 24 hour this will definitely work!!
Just click "Yes, it was me", then turn off "Less secure app accses."
If you are signed in with multiple accounts on the same device you do not have to sign out of your accounts for performing these steps.
All you need to do is to open a private window, paste the following URL: https://accounts.google.com/b/0/DisplayUnlockCaptcha
into your browser's private window. And you will be redirected to your Gmail sign-in page. put your account email in the given field and then press next and enter the password. The link will direct you to the captcha page and click on continue. Make sure you don't close the tab after unlocking the captcha and run your script. You don't have to always unlock captcha if this works for the first time.

Python scraping data that can only be accessed through Google OAuth login

I want to scrape some data from a website which uses Google OAuth for the authentication. Some data can only be accessed if I perform a login.
Basically, when you open the website (mamikos.com) and click login, there is no option for a normal login form, it will give you options to log in with facebook or google. It then will redirect you to the google login page. After login with google account, you then be redirected to website's homepage and all the data will easily be accessible with a simple click.
I am basically a noob, only know some basic coding and googling. I have looked everywhere but it seems like I am looking in the wrong place. I tried to write code with selenium to automate the click, passing username/password, and perform the login, but apparently, selenium is not the right tool for this as it will open a browser and do the stuff.
Is it possible to do this login and authentication process in the background? I got over a hundred thousand URL of pages in which I need the data from. Using selenium will crash my computer and will take a long time to finish.
Can someone here please show or at least point me into the right tools/library/method. or idk is it even possible?
Thanks
Please note that this answer is currently a work in progress - I'm working on (almost) the exact same problem, (different site, and I'll be using go), but I can provide a rough workaround for getting started, and when my solution matures I will update this.
Reiteration of problem statement
What you are asking for is a way for your scraper (third party client) to authenticate with a website (Resource Server) via google oauth (Authorization Server), to access resources that your specific account (Resource owner) has permission to see.
This sounds like three legged ouath.
"OAuth2 Simplified" is a nicely written article by Aaron Parecki giving a broad overview of the roles of client, resource owner, resource server, and authorization server in the three legged oauth process.
Another requirement (from what I can infer) is that the client you're implmenting/authenticating with is not trusted by the Authorization Server or the Resource Server.
This is significant, as it does preclude certain oauth flows from being usable, and may mean that various oauth client libraries/packages are not viable, as they may not implement flows for untrusted clients.
Workaround (Rough pass)
You identified selenium as a potential workaround for achieving authentication.
You accurately identified that selenium is not a great solution for large-scale scraping, as it is very heavyweight, relatively slow, & uses a lot of resources.
This being said, you only need to use selenium once in this process - To automate the oauth process to obtain an access token to use the website.
Once you get a token, you can discard your selenium instance & use your favourite high performance scraping library to carry out the rest of your tasks. From there, you can attach the token to your requests and recieve access.
This blog post describes this approach broadly, using a JS selenium API (Under "Use a automated UI Test to get the access token via Authorization Code Grant" )
I will provide more specifics once I implement them.
I understand it can be tough to scrape data from such websites which are behind log-in pages. You will need to learn the basic replication of request that is being sent to the server through requests library of Python. It can be daunting in the beginning but you can learn it step by step here.

Gathering bug reports?

Premise: I am a beginner in search for an easy way to send bug reports from users over sea.
I've made a script for some friends that are living on the other side of the sea (US - EUROPE)... I will like to gather automatic bug reports whenever they happen. So my first idea was to send myself an email with the smtplib module. It works fine when testing home, but as soon as the sender "sends", my email provider (gmail) blocks the connection because of course, its from an "unknown device". I've already enabled "Allow less secure apps" as someone suggested but with no avail.
What I am searching its a simple way of dealing with this.
Yes I could make the script to ignore the error if the email its not being sent, and then go into my google account and enable those devices so at least it will work from the second run..
But it doesn't seem what a programmer would do in this case. I am learning so a solution withing the language is what I am after.
A different provider that has no restriction its also a good start but I tried Yahoo, Live, Yandex but I couldn't make them work. Are there any?
So my question is: how others do? what is the best solution for some one like me?
I've read about sentry or other error/bug tracking but its obviously way too much for want I need
You should certainly not incorporate e.g. Gmail credentials in the code that is remotely executed on devices you do not control, given I understand correctly the Gmail less secure device issue happens as every "user" is running this code and using your credentials. This holds true for any other provider.
Now this won't exactly be simple but one way to go about it would be to create a server side API endpoint that can accept HTTP(s) or any other protocol requests that then will authenticate in a little more secure way on the server side with Gmail.
The concept for emails is:
Bug > Python Script > API call > Email
This could be implemented using Python on the API side (Flask e.g.) using an AWS Lambda Function with Amazon API Gateway, but again that is something to get through and understand by itself which will take a good chunk of time.
You need to touch a lot of concepts, like auth tokens to make this really secure.
Could you elaborate a little on where the code needs to run and if you are willing to try AWS or any other cloud provider, or would have access to an internet connected server ? This makes it easier to provide you with a full example on the solution in a hackish way while I would highlight the problems you could face on the security side.
I understand that this is not the way to go but as for my needs and my level of experience it works for me!
Yandex allows you to send email from different ip so Yandex is the way to go. What I was doing wrong in the first place was to use the wrong port (587 instead of 465)

Categories