kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

How to use auth2.0, self._sslobj.do_handshake() OSError: [Errno 0] Error #240

Closed pgshow closed 2 years ago

pgshow commented 2 years ago

When I tried to use Gmail auth2.0 for yagmail.

import yagmail

yag = yagmail.SMTP("sgdailydata@gmail.com", oauth2_file="./gmail-app.json", port=465)

subject = 'This is obviously the subject'
body = 'This is obviously the body'

# yag.send(to="pgshow@gmail.com", subject=subject, contents=body)
yag.send(subject=subject)

When I use python3.7, the error is self._sslobj.do_handshake() OSError: [Errno 0] Error

When I use python3.8, the error becomes. ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1131)

pgshow commented 2 years ago

Understand, My VPN issue. It totally works on a cloud server.

kootenpv commented 2 years ago

👌

arahman63 commented 1 year ago

Hey where you able to fix the issue?

pgshow commented 1 year ago

Hey where you able to fix the issue?

Don't use a Proxy or VPN, use the script on aws cloud server, and it may work.

arahman63 commented 1 year ago

I am having issues with sending myself emails using yagmail with the use of the OAuth2 verification. Here’s the code snippet: def send_mail(): gmail = os.environ.get("GMAIL") gmail_pass = os.environ.get("GMAIL_PASS")

yag = yagmail.SMTP("[arifurrahman@nypl.org](mailto:arifurrahman@nypl.org)",oauth2_file="./oauth2_creds.json")
# contents = ['This is the body, and here is just text',
#             'You can find an audio file attached.', '/local/path/song.mp3']
yag.send(subject="Great success Wawa Wee Wa!”)

The function runs and obtains my credentials from my json file ; however when I paste in the link that it tells me to go to, after allowing the app to do its thing, I get a black screen saying localhost refused to connect. As stated in the console I just take the localhost url at the top which contains the code param needed to continue on with the script as :”http://localhost/?code=….a&scope=https://mail.google.com/“, and paste it in the console, and then get a massive error as such:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1348, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1282, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1328, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1277, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1037, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 975, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1454, in connect self.sock = self._context.wrap_socket(self.sock, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 517, in wrap_socket return self.sslsocket_class._create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1075, in _create self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1346, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/arah/spotlight/spot.py", line 165, in send_mail() File "/Users/arah/spotlight/spot.py", line 144, in send_mail yag = yagmail.SMTP("arifurrahman@nypl.org",oauth2_file="./oauth2_creds.json") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/arah/spotlight/env/lib/python3.11/site-packages/yagmail/sender.py", line 42, in init oauth2_info = get_oauth2_info(oauth2_file, user) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/arah/spotlight/env/lib/python3.11/site-packages/yagmail/oauth2.py", line 118, in get_oauth2_info google_refreshtoken, , _ = get_authorization(google_client_id, google_client_secret) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/arah/spotlight/env/lib/python3.11/site-packages/yagmail/oauth2.py", line 88, in get_authorization response = call_authorize_tokens(google_client_id, google_client_secret, authorization_code) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/arah/spotlight/env/lib/python3.11/site-packages/yagmail/oauth2.py", line 60, in call_authorize_tokens response = urlopen(request_url, encoded_params).read().decode('UTF-8') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 216, in urlopen return opener.open(url, data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 519, in open response = self._open(req, data) ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 536, in _open result = self._call_chain(self.handle_open, protocol, protocol + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 496, in _call_chain result = func(*args) ^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1391, in https_open return self.do_open(http.client.HTTPSConnection, req, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 1351, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)>

Pertaining to to some SSL certificate error which may because the localhost refused to connect, which I’m not entirely sure how to fix. I checked my console.cloud.google account to make sure the oauth2.0 client ids are for a desktop app. I made the internal for the user type, but that doesn’t as matter as much because I am pasting the link on the google account that has access. I checked with to see if that was an issue by using a different email, but it gave me a 403 error which is expected as that gmail is not in the org I set internally. I also kept the authorized app domain fields blank and just gave the app a name and user support email and developer contact info, which is the bare necessity to make Oauth consent screen work. I am also using

arahman63 commented 1 year ago

I realized how you can fix the SSL error and all you need is this line: ssl._create_default_https_context = ssl._create_unverified_context

and import ssl in the code. I hope this helps anyone for those using oauth2 with yagmail