2015-06-01 86 views
0

我使用python生成一種跟蹤已發送書籍但未收到回覆的潛在客戶的方法。快速的方式趕上他們。必須強調這不是用於垃圾郵件。無法使用gspread進行身份驗證以訪問Google Drive

我有一個包含GMAIL_USERNAME =「[email protected]」 GMAIL_PASSWORD =「myGmailPassword」

下使用這些信息登錄到谷歌驅動器和電子郵件的人誰沒有拿到一個文件gmail_variable.py回到我的身邊。直到今天工作都很好。開始收到以下錯誤。我從終端運行此代碼,並將代碼保存在本地機器上。

Traceback (most recent call last): 
File "pr_email_robot.py", line 6, in <module> 
gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD) 
File "/Library/Python/2.7/site-packages/gspread/client.py", line 312, in login 
client.login() 
File "/Library/Python/2.7/site-packages/gspread/client.py", line 119, in login 
"Unable to authenticate. %s code" % ex.code) 
gspread.exceptions.AuthenticationError: Unable to authenticate. 404 code 

下面是執行的代碼。根據我閱讀的內容,我知道oAuth是4月20日的變化。但是直到那時,下面的代碼纔開始工作。

一些初步的研究後,我發現,gspread建議與

gc = gspread.authorize(OAuth2Credentials) 

我然後通過引導here和設置的API去爲他們建議更換

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD) 

。下載了JSON文件。但是,我將OAuth2Credentials替換爲什麼?

gc = gspread.authorize(OAuth2Credentials) 

任何想法或建議非常感謝。還是很新的Python這樣簡單的解釋是有幫助:)

import smtplib 
import gspread 

from gmail_variables import * 

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD) 
wks = gc.open("horror_reviewers").sheet1 


recipients = wks.get_all_values() 

def sendEmail(recipient): 
    email_subject = "Jay-Jay" 
    #recipient = "[email protected]" 

    body_of_email = "<body><p>Hello "+ recipient[1].encode('utf-8') +",<br /> \ 
        <br /> \ 
        We spoke in the last few months X book.<br /> \ 
        <br /> \ 
        Have a note that we sent you a copy and confirmed a review. Did we send over what you needed or are you still awaiting details from us?</a><br /> \ 
        <br /> \ 
        Apologies if you have already posted the link and we missed it. If you could resend that would be great.<br /> \ 
        <br /> \ 
        </p></body>" 

    session = smtplib.SMTP('smtp.gmail.com', 587) 
    session.ehlo() 
    session.starttls() 
    session.login(GMAIL_USERNAME, GMAIL_PASSWORD) 

    headers = "\r\n".join(["from: " + GMAIL_USERNAME, 
          "subject: " + email_subject, 
          "to: " + recipient[0], 
          "mime-version: 1.0", 
          "content-type: text/html"]) 

    # body_of_email can be plaintext or html!      
    content = headers + "\r\n\r\n" + body_of_email 
    session.sendmail(GMAIL_USERNAME, recipient[0], content) 

[sendEmail(i) for i in recipients] 

回答

0

可以使用SMTP使用Gmail來發送郵件:

代碼示例:

import smtplib 
from email.mime.text import MIMEText 

hostname = "smtp.gmail.com" 
password = "<password>" 
me = "<[email protected]>" 
you = "<[email protected]>" 

payload = "some text here" 
msg = MIMEText(payload) 

msg["Subject"] = "Test subject" 
msg["From"] = me 
msg["To"] = you 

session = smtplib.SMTP_SSL(hostname) 
session.login(me, password) 
session.sendmail(me, [you], msg.as_string()) 
session.quit() 

來源:

接過想法從這裏:http://vi3k6i5.blogspot.in/2015/03/how-to-extract-email-ids-from-your.html

和代碼從這裏:http://www.reddit.com/r/Python/comments/15n6dw/sending_emails_through_python_and_gmail/

+0

感謝@VikashSingh,但它沒有像現有系統一樣與Google Drive集成。 – PatGW

+0

檢查如何使用gspread,將包括在代碼.. –

+0

你可以嘗試這個鏈接,看看它是否適用於你:http://gspread.readthedocs.org/en/latest/oauth2.html –

相關問題