0

我使用 python 来生成一种方法来跟进我已经发送书籍但没有收到回复的潜在客户。快速赶上他们的方法。必须强调这不用于垃圾邮件。

我有一个 gmail_variable.py 文件,其中包含 GMAIL_USERNAME = "myemail@gmail.com" GMAIL_PASSWORD = "myGmailPassword"

下面使用这些详细信息登录 Google Drive 并向尚未回复我的任何人发送电子邮件。工作到今天都很好。开始收到以下错误。我从终端运行它并将代码本地保存在我的机器上。

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.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 回答