This document explains how to configure email forwarding in the Gmail API.
You can use the
settings resource to
configure forwarding for an account. To use an email address for forwarding, it
must meet one of the following criteria:
- The email address is verified. For more information, see Create and verify forwarding addresses.
- The email address belongs to the same domain as the sender.
- The email address belongs to a subdomain within the same domain as the sender.
- The email address belongs to a domain alias configured as part of the same Google Workspace account.
If the forwarding email address doesn't adhere to one of these rules, setting up forwarding using the API fails.
For information on how to
create,
list,
get,
or
delete
forwarding addresses, see the methods on the
settings.forwardingAddresses
resource.
For information on how to
get
or
update
the auto-forwarding settings, see the methods on the
settings resource.
Create and verify forwarding addresses
You must create forwarding addresses before you use them. In some cases, users must also verify ownership of the address.
If Gmail requires user verification for a forwarding address, the API
returns the address with a
VerificationStatus
of pending. Gmail automatically sends a verification message to
the target email address. The owner of the email address must complete the
verification process before you can use it.
Forwarding addresses that don't require verification have a verification status
of accepted.
Enable automatic forwarding
You can choose to forward all of your new messages to another email address.
To do so, call the
updateAutoForwarding
method to enable auto-forwarding for an account. The call requires a registered
and verified forwarding address and an action to take on forwarded messages.
Set these values in the
AutoForwarding
object.
Use the
disposition
field to set the message state after the message is forwarded. Although the
default value is dispositionUnspecified, you can't set this field to
dispositionUnspecified.
The following code samples show how to enable auto-forwarding and then move forwarded messages to the trash:
Java
Python
To disable auto-forwarding, call the
updateAutoForwarding
method and set the
enabled
field on the AutoForwarding object to false.
Forward specific messages
Automatic forwarding sends all received Gmail messages to the target account. To forward specific messages, set up a filter to create rules that forward messages in response to message attributes or content.
To forward messages to multiple accounts, create a filter for every forwarding email address.