我'm having some trouble sending a message to multiple addresses using the Gmail API. I' ve已成功向一个地址发送了一条消息,但在 'To'
字段中包含多个以逗号分隔的地址时出现以下错误:
发生错误:https://www.googleapis.com/gmail/v1/users/me/messages/send ?alt = json返回“ Headers 无效”>
我正在使用此Gmail API指南中的 CreateMessage
和 SendMessage
方法:https://developers.google.com/gmail/api/guides/sending
该指南指出Gmail API需要符合RFC-2822的邮件 . 我再次使用RFC-2822指南中的一些寻址示例没有太多运气:https://tools.ietf.org/html/rfc2822#appendix-A
我'm under the impression that ' mary@x.test,jdoe@example.org,one@y.test ' should be a valid string to pass into the '到 CreateMessage
的参数,但我从 SendMessage
收到的错误让我不相信 .
如果您可以重新创建此问题,或者您对我可能犯错误的地方有任何建议,请告诉我 . 谢谢!
编辑:这是产生错误的实际代码......
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())}
def SendMessage(service, user_id, message):
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print 'Message Id: %s' % message['id']
return message
except errors.HttpError, error:
print 'An error occurred: %s' % error
def ComposeEmail():
# build gmail_service object using oauth credentials...
to_addr = 'Mary Smith <mary@x.test>, jdoe@example.org, Who? <60one@y.test>'
from_addr = 'me@address.com'
message = CreateMessage(from_addr,to_addr,'subject text','message body')
message = SendMessage(gmail_service,'me',message)
3 回答
在单个 Headers 中发送多个收件人(逗号分隔)时,获取“无效 Headers ”是在2014-08-25修复的回归 .
正如詹姆斯在评论中所说,当Python对使用SMTP有很好的文档支持时,你不应该浪费时间尝试使用Gmail API:
email
模块可以编写包含附件的消息,并且smtplib
发送它们 . 恕我直言,您可以使用Gmail API开箱即用,但在出现问题时应使用Python标准库中的强大模块 .看起来您想发送一条纯文本消息:这是一个改编自
email
模块文档和来自Mkyong.com的How to send email in Python via SMTPLIB的解决方案:另见User.drafts reference - error"Invalid to header"
显然,最近在Gmail API中引入了此错误 .