简体   繁体   English

如何向多个收件人发送邮件?

[英]How to send message to multiple recipients?

I'm having some trouble sending a message to multiple addresses using the Gmail API. 我在使用Gmail API向多个地址发送邮件时遇到问题。 I've successfully sent a message to only one address, but get the following error when I include multiple comma-separated addresses in the 'To' field: 我已经成功地只向一个地址发送了一条消息,但是当我在'To'字段中包含多个以逗号分隔的地址时,会收到以下错误:

An error occurred: https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Invalid to header"> 发生错误: https//www.googleapis.com/gmail/v1/users/me/messages/send ?alt = json返回“标题无效”>

I'm using the CreateMessage and SendMessage methods from this Gmail API guide: https://developers.google.com/gmail/api/guides/sending 我正在使用此Gmail API指南中的CreateMessageSendMessage方法: https//developers.google.com/gmail/api/guides/sending

That guide states that the Gmail API requires messages that are RFC-2822 compliant. 该指南指出Gmail API需要符合RFC-2822的邮件。 I again didn't have much luck using some of these addressing examples in the RFC-2822 guide: https://tools.ietf.org/html/rfc2822#appendix-A 我再次使用RFC-2822指南中的一些寻址示例没有太多运气: https//tools.ietf.org/html/rfc2822#appendix-A

I'm under the impression that 'mary@x.test, jdoe@example.org, one@y.test' should be a valid string to pass into the 'to' parameter of CreateMessage , but the error that I received from SendMessage leads me to believe otherwise. 我的印象是'mary @ x.test,jdoe @ example.org,one @ y.test'应该是一个有效的字符串,以传递给CreateMessage的'to'参数,但是我从SendMessage收到的错误让我相信不然。

Please let me know if you can recreate this problem, or if you have any advice on where I may be making a mistake. 如果您可以重新创建此问题,或者您对我可能犯错误的地方有任何建议,请告诉我。 Thank you! 谢谢!

Edit: Here is the actual code that yields an error... 编辑:这是产生错误的实际代码......

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)

在单个标题中发送多个收件人(逗号分隔)时,获取“无效标题”是在2014-08-25修复的回归。

As James says in its comment, you shouldn't waste time trying to use Gmail API when Python has excellent documented support for using SMTP : email module can compose message including attachements, and smtplib sends them. 正如詹姆斯在评论中所说,当Python对使用SMTP有很好的文档支持时,你不应该浪费时间尝试使用Gmail API: email模块可以组成包含附件的消息,并且smtplib发送它们。 IMHO you could use Gmail API for what works out of the box but should use the robust modules form Python Standard Library when things go wrong. 恕我直言,您可以使用Gmail API 开箱即用,但在出现问题时应使用Python标准库中的强大模块。

It looks like you want to send a text only message : here is a solution adapted from the email module documentation and How to send email in Python via SMTPLIB from Mkyong.com: 看起来您想发送一条纯文本消息:这里是一个改编自email模块文档的解决方案以及如何通过SMTPXIB从Mkyong.com 发送Python电子邮件

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

msg = MIMEText('message body')
msg['Subject'] = 'subject text'
msg['From'] = 'me@address.com'
msg['To'] = 'Mary Smith <mary@x.test>, jdoe@example.org, "Who?" <60one@y.test>'

# Send the message via Gmail SMTP server.
gmail_user = 'youruser@gmail.com'
gmail_pwd = 'yourpassword'smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver = smtplib.SMTP('smtp.gmail.com')smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
smtpserver.send_message(msg)
smtpserver.quit()

See also User.drafts reference - error"Invalid to header" 另请参阅User.drafts参考 - 错误“标题无效”

Apparently this bug was recently introduced in Gmail API. 显然,最近在Gmail API中引入了此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM