How do I send eMail with curl, gmail, and OAuth2?

偶尔善良 提交于 2020-03-28 06:54:09

问题


Help!

Fedora 31

curl-7.66.0-1.fc31.x86_6

gMail: less secure apps are purposefully off as gMail will be dropping support for less secure in June.

I can not find anything our there on how to use curl with gmail and OAuth2. Google is no help.

I did figure out how to get my token:

First, you must obtain your Client ID and Client Secret by using a browser. Open the following

 https://console.developers.google.com/
 --> Credentials (left column)
   --> "+ Create Credentials"  (button at the top)
      --> OAuth client ID
        --> Application Type = Web Application
          --> Name: give it a unique name
            --> Save
              --> copy the resulting Client ID and Client Secret 
                  into the corresponding variables below.
                  And independently save these values as well

The script will then take you to a browser page where you will sign into your google account to receive your authorization code, which you save

Also note that you have to tell gmail that the suspicious activity is you. gmail will send you a letter with a link to follow

client_id="aaaa"  # from the web page
client_secret="bbb" # also from the web page
scope="https://www.googleapis.com/auth/drive"
endpoint="https://accounts.google.com/o/oauth2/auth"=code&scope=$scope%access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
url="$endpoint?client_id=$client_id&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=$scope&response_type=code"

This is what I have so far

    $ cat mail.txt | curl -vvv smtps://smtp.gmail.com:465 --mail-from "xxxx@gmail.com" --mail-rcpt "yyyy@zoho.com" --ssl -u xxxx@gmail.com:'zzzz' -oauth2-bearer 'big long thing'

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 74.125.142.109:465...
* TCP_NODELAY set
* Connected to smtp.gmail.com (74.125.142.109) port 465 (#0)
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=smtp.gmail.com
*  start date: Mar  3 09:44:15 2020 GMT
*  expire date: May 26 09:44:15 2020 GMT
*  subjectAltName: host "smtp.gmail.com" matched cert's "smtp.gmail.com"
*  issuer: C=US; O=Google Trust Services; CN=GTS CA 1O1
*  SSL certificate verify ok.
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< 220 smtp.gmail.com ESMTP g5sm6303926pfo.192 - gsmtp
> EHLO rn6
< 250-smtp.gmail.com at your service, [50.37.20.94]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> AUTH PLAIN
< 334 
> AFRvZGRBbmRNYXJnb0BnbWFpbC5jb20Ab25jb3JoeW5jaHVzIG15a2lzcyAxMw==
< 535-5.7.8 Username and Password not accepted. Learn more at
< 535 5.7.8  https://support.google.com/mail/?p=BadCredentials g5sm6303926pfo.192 - gsmtp
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Closing connection 0
* TLSv1.3 (OUT), TLS alert, close notify (256):
curl: (67) Login denied
*   Trying 0.0.0.1:80...
* TCP_NODELAY set

来源:https://stackoverflow.com/questions/60736238/how-do-i-send-email-with-curl-gmail-and-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!