0

私は Python を使用して、本を送ったが返事がない見込み客をフォローアップする方法を生成します。彼らに追いつくための簡単な方法。これはスパムには使用されないことを強調する必要があります。

GMAIL_USERNAME = "myemail@gmail.com" GMAIL_PASSWORD = "myGmailPassword" を含む gmail_variable.py ファイルが 1 つあります。

以下は、これらの詳細を使用して Google ドライブにログインし、まだ返信がない人にメールを送信します。今日まで絶対に元気に働いています。次のエラーを受け取り始めました。これを端末から実行し、コードを自分のマシンにローカルに保存します。

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

以下は、実行されるコードです。私が読んだものによると、4月20日にoAuthが変更されたことを知っています。しかし、それまでは以下のコードが機能していました。

いくつかの初期調査の後、gspread が置き換えを推奨していることを発見しました

gc = gspread.login(GMAIL_USERNAME, GMAIL_PASSWORD)

gc = gspread.authorize(OAuth2Credentials)

次に、こちらのガイドを読み、提案どおりに API をセットアップしました。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 = "nat@programmingformarketers.com"

    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]
4

1 に答える 1

0

SMTP を使用して、gmail を使用してメールを送信できます。

コード例:

import smtplib
from email.mime.text import MIMEText

hostname = "smtp.gmail.com"
password = "<password>"
me = "<me@gmail.com>"
you = "<you@gmail.com>"

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/

于 2015-06-01T13:48:03.653 に答える