Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send message to multiple recipients?

I'm having some trouble sending a message to multiple addresses using the 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:

An error occurred: <HttpError 400 when requesting
https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Invalid to header">

I'm using the CreateMessage and SendMessage methods from this Gmail API guide: https://developers.google.com/gmail/api/guides/sending

That guide states that the Gmail API requires messages that are RFC-2822 compliant. I again didn't have much luck using some of these addressing examples in the RFC-2822 guide: https://www.rfc-editor.org/rfc/rfc2822#appendix-A

I'm under the impression that '[email protected], [email protected], [email protected]' 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.

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 <[email protected]>, [email protected], Who? <[email protected]>'
    from_addr = '[email protected]'
    message = CreateMessage(from_addr,to_addr,'subject text','message body')
    message = SendMessage(gmail_service,'me',message)
like image 301
Jordan Hawkins Avatar asked Aug 22 '14 00:08

Jordan Hawkins


People also ask

How do I send one message to multiple addresses at the same time?

To send emails to small groups where everybody knows each other, use the Cc field. Enter all of the addresses there, separated by commas. To hide addresses, use the Bcc field, just like the Cc field. No one will be able to see the addresses added in this field.

How do you send multiple people individually?

Open a new email and write the message you intend to send to your contact list. Click BCC in the top-right of your Compose window. Add all the email addresses to which you intend to send the message. It might help to copy and paste your list into this field.


1 Answers

Getting "Invalid to header" when sending with multiple recipients (comma delimited) in a single header was a regression that was fixed on 2014-08-25.

like image 199
Eric D Avatar answered Sep 18 '22 13:09

Eric D