【问题标题】:Error sending mail with Google API - "'raw' RFC822 payload message string or uploading message via /upload/* URL required"使用 Google API 发送邮件时出错 - “'原始' RFC822 有效负载消息字符串或通过 /upload/* URL 上传消息”
【发布时间】:2016-10-30 03:22:03
【问题描述】:

我正在尝试通过 Python 中的 Google API 发送消息,并尝试运行一个几乎直接取自 Google example page 的示例。

def CreateMessage(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_string().replace('message','resource').encode('ascii'))}

但是当我尝试发送它时

    message = CreateMessage(sender, to, subject, message_text)
    message = service.users().messages().send(body=list(message),userId='me').execute()

我收到错误消息:“'raw' RFC822 payload message string or uploading message via /upload/* URL required”

从其他帖子看来,Google 正在等待附件。 MIMEText 是否有问题使其期望出现问题,如果是,我该如何解决?

【问题讨论】:

标签: python-3.x gmail-api google-api-python-client


【解决方案1】:

list(message) 不是必需的,它为 API 提供了以下内容:

[{"raw": "b64 content..."}]

只是做:

...messages().send(body=message, userId='me'...

【讨论】:

  • 我没有使用 'list' 并收到相同的错误消息。
【解决方案2】:

请尝试以下方法:

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to
msg.attach(MIMEText(message_text, 'plain'))
return {'raw': base64.urlsafe_b64encode(msg.as_string().encode()).decode()}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2014-06-09
    • 2012-07-22
    • 2023-03-04
    • 2019-06-12
    相关资源
    最近更新 更多