curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Access gmail with pycurl.

From: Hongyi Zhao via curl-library <curl-library_at_cool.haxx.se>
Date: Fri, 2 Jul 2021 13:04:17 +0800

On Fri, Jul 2, 2021 at 12:16 PM Ray Satiro via curl-library
<curl-library_at_cool.haxx.se> wrote:
>
> On 6/29/2021 12:33 AM, Hongyi Zhao via curl-library wrote:
>
> On Tue, Jun 29, 2021 at 5:55 AM Daniel Stenberg <daniel_at_haxx.se> wrote:
>
> On Sat, 26 Jun 2021, Hongyi Zhao via curl-library wrote:
>
> I noticed that libcurl, hence pycurl supports IMAP and IMAPS, so I want to
> know if I can use it to retrieve/remove emails from my Gmail account?
>
> Yes you can.
>
> Jay once wrote ShowGmailFolders.c for example:
>
> https://gist.github.com/jay/a43724c98220de85d020
>
> Thank you for this wonderful code snippet. But for quick testing, I
> try the following command based on the notes at
> <http://cweiske.de/tagebuch/curl-imap-noauth.htm>, but failed:
>
> $ curl -x socks5h://127.0.0.1:18888 -v -k --ssl
> imaps://imap.gmail.com:993 --user "username:passwd"
> * Trying 127.0.0.1:18888...
> * TCP_NODELAY set
> * SOCKS5 communication to imap.gmail.com:993
> * SOCKS5 connect to imap.gmail.com:993 (remotely resolved)
> * SOCKS5 request granted.
> * Connected to 127.0.0.1 (127.0.0.1) port 18888 (#0)
> * successfully set certificate verify locations:
> * CAfile: /etc/ssl/certs/ca-certificates.crt
> CApath: /etc/ssl/certs
> * TLSv1.3 (OUT), TLS handshake, Client hello (1):
> * OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to imap.gmail.com:993
> * Closing connection 0
> curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to
> imap.gmail.com:993
>
>
> syscall error is probably your proxy.

Exactly, you're right. The problem is caused by the socks5 proxy
supplied by HAProxy on local side doesn't enable the sticky-session
option. I use another socks5 proxy exposed from HAProxy with the
sticky-session option, and it does the trick:

$ curl -x socks5h://127.0.0.1:18889 --ssl imaps://imap.gmail.com:993
--user "hongyi.zhao:passwd"
* LIST (\HasNoChildren) "/" "INBOX"
* LIST (\HasNoChildren) "/" "Junk"
* LIST (\HasChildren \Noselect) "/" "[Gmail]"
* LIST (\All \HasNoChildren) "/" "[Gmail]/All Mail"
* LIST (\Drafts \HasNoChildren) "/" "[Gmail]/Drafts"
* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"
* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"
* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"
* LIST (\Flagged \HasNoChildren) "/" "[Gmail]/Starred"
* LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"
* LIST (\HasNoChildren) "/" "&XeVPXJCuTvY-"
* LIST (\HasNoChildren) "/" "&ZTZjbg-"
* LIST (\HasNoChildren) "/" "&ZcWITHb4UXM-"
* LIST (\HasNoChildren) "/" "&ecFOupCuTvY-"


> I can connect fine. Note you will have to enable "less secure app access" [1] on your google account to use the gmail username/password.

Yes, I've enabled this for doing the above testing.


> There is a way to do it with oauth2 instead. I don't think I've ever had to do that though and I don't think we have an example for it.
>
>
> [1]: https://myaccount.google.com/lesssecureapps
>
>
>
> -------------------------------------------------------------------
> Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
> Etiquette: https://curl.se/mail/etiquette.html

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao_at_gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2021-07-02