我在使用 Gmail API 向多个地址发送邮件时遇到一些问题。我已成功将一封邮件仅发送到一个地址,但当我在邮件中包含多个以逗号分隔的地址时,出现以下错误'To'
field:
请求时发生错误:
https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json返回“标题无效”>
我正在使用CreateMessage
and SendMessage
此 Gmail API 指南中的方法:https://developers.google.com/gmail/api/guides/sending https://developers.google.com/gmail/api/guides/sending
该指南指出 Gmail API 需要符合 RFC-2822 的邮件。我再次没有太多运气使用 RFC-2822 指南中的一些寻址示例:https://www.rfc-editor.org/rfc/rfc2822#appendix-A https://www.rfc-editor.org/rfc/rfc2822#appendix-A
我的印象是'[电子邮件受保护] /cdn-cgi/l/email-protection, [电子邮件受保护] /cdn-cgi/l/email-protection, [电子邮件受保护] /cdn-cgi/l/email-protection' 应该是传递到 'to' 参数的有效字符串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 <[email protected] /cdn-cgi/l/email-protection>, [email protected] /cdn-cgi/l/email-protection, Who? <[email protected] /cdn-cgi/l/email-protection>'
from_addr = '[email protected] /cdn-cgi/l/email-protection'
message = CreateMessage(from_addr,to_addr,'subject text','message body')
message = SendMessage(gmail_service,'me',message)