Search code examples
google-apigmailgmail-api

How to add forwarding rule in Google admin routing settings using Google API?


In admin settings in Google you can find section for Gmail routing. It's Apps > Google Workspace > Settings for Gmail > Routing. There, at the bottom of the page, you can find Email forwarding using recipient address map.

Here, you can add rule to forward emails from one email to another. And I'm wondering, if it's possible to add this rule using Google API.

Probably, I have found it, but I am really not sure. Here it is, from Gmail API. But it says only about auto-forwarding, and I guess, it's not about creating forwarding rules, like on screenshot.

So, my question is - is it possible to add forwarding rules using Google API?

enter image description here


Solution

  • I think you may be looking for Managing Filters

    Filters can automatically add or remove labels or forward emails to verified aliases based on the attributes or content of the incoming message.

     # create gmail api client
        service = build('gmail', 'v1', credentials=creds)
    
        label_name = 'IMPORTANT'
        filter_content = {
            'criteria': {
                'from': '[email protected]'
            },
            'action': {
                'addLabelIds': [label_name],
                'removeLabelIds': ['INBOX'],
                'forward': "[email protected]"
            }
        }
    
        # pylint: disable=E1101
        result = service.users().settings().filters().create(
            userId='me', body=filter_content).execute()
    

    I haven't yet figured out how to get it to forward but the documentation says it should be able to.

    i found it under action just add forward