ईमेल फ़ॉरवर्ड करने की सुविधा मैनेज करना

इस दस्तावेज़ में, Gmail API में ईमेल फ़ॉरवर्ड करने की सुविधा को कॉन्फ़िगर करने का तरीका बताया गया है.

किसी खाते के लिए ईमेल फ़ॉरवर्ड करने की सुविधा कॉन्फ़िगर करने के लिए, settings संसाधन का इस्तेमाल किया जा सकता है. किसी ईमेल पते को फ़ॉरवर्डिंग ईमेल पते के तौर पर इस्तेमाल करने के लिए, इनमें से कोई एक शर्त पूरी होनी चाहिए:

  • ईमेल पते की पुष्टि हो गई है. ज़्यादा जानकारी के लिए, ईमेल फ़ॉरवर्ड करने के पते बनाना और उनकी पुष्टि करना लेख पढ़ें.
  • ईमेल पता, भेजने वाले के डोमेन का ही हो.
  • ईमेल पता, भेजने वाले के डोमेन के सबडोमेन से जुड़ा हो.
  • यह ईमेल पता, एक ऐसे डोमेन एलियास से जुड़ा है जिसे एक ही Google Workspace खाते के हिस्से के तौर पर कॉन्फ़िगर किया गया है.

अगर फ़ॉरवर्ड करने के लिए इस्तेमाल किया जा रहा ईमेल पता, इनमें से किसी एक नियम का पालन नहीं करता है, तो एपीआई का इस्तेमाल करके ईमेल फ़ॉरवर्ड करने की सुविधा सेट अप नहीं की जा सकेगी.

फ़ॉरवर्डिंग पते बनाने, सूचीबद्ध करने, पाने या मिटाने के तरीके जानने के लिए, settings.forwardingAddresses संसाधन देखें.

ईमेल अपने-आप फ़ॉरवर्ड होने की सेटिंग को पाने या अपडेट करने के तरीके के बारे में जानने के लिए, settings संसाधन में दिए गए तरीके देखें.

ईमेल फ़ॉरवर्ड करने के पते बनाना और उनकी पुष्टि करना

फ़ॉरवर्डिंग पतों का इस्तेमाल करने से पहले, उन्हें बनाना ज़रूरी है. कुछ मामलों में, उपयोगकर्ताओं को पते के मालिकाना हक की पुष्टि भी करनी होगी.

अगर Gmail को ईमेल फ़ॉरवर्ड करने के लिए उपयोगकर्ता की पुष्टि करनी होती है, तो पते को VerificationStatus के साथ वापस भेज दिया जाता है. pending. पुष्टि करने के लिए एक मैसेज, टारगेट ईमेल पते पर अपने-आप भेजा जाता है. ईमेल पते का इस्तेमाल करने से पहले, उसके मालिक को पुष्टि की प्रक्रिया पूरी करनी होगी.

जिन ईमेल पतों की पुष्टि करने की ज़रूरत नहीं होती उनके लिए, पुष्टि की स्थिति accepted के तौर पर दिखती है.

मैसेज, अपने-आप फ़ॉरवर्ड करने की सुविधा चालू करना

आपके पास अपने सभी नए मैसेज को किसी दूसरे ईमेल पते पर फ़ॉरवर्ड करने का विकल्प होता है.

इसके लिए, किसी खाते के लिए अपने-आप फ़ॉरवर्ड होने की सुविधा चालू करने के लिए, updateAutoForwarding तरीके का इस्तेमाल करें. इसके लिए, फ़ॉरवर्ड करने के लिए रजिस्टर किया गया और पुष्टि किया गया ईमेल पता होना चाहिए. साथ ही, फ़ॉरवर्ड किए गए ईमेल पर की जाने वाली कार्रवाई की जानकारी होनी चाहिए. इन्हें AutoForwarding ऑब्जेक्ट का इस्तेमाल करके सेट किया जाता है.

disposition फ़ील्ड का इस्तेमाल, मैसेज फ़ॉरवर्ड होने के बाद मैसेज का स्टेटस सेट करने के लिए किया जाता है. डिफ़ॉल्ट वैल्यू dispositionUnspecified है, लेकिन इस फ़ील्ड को dispositionUnspecified पर सेट नहीं किया जा सकता.

नीचे दिए गए कोड सैंपल में, ईमेल अपने-आप फ़ॉरवर्ड होने की सुविधा चालू करने और फ़ॉरवर्ड किए गए ईमेल को ट्रैश में ले जाने का तरीका बताया गया है:

Java

gmail/snippets/src/main/java/EnableForwarding.java
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.AutoForwarding;
import com.google.api.services.gmail.model.ForwardingAddress;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;

/* Class to demonstrate the use of Gmail Enable Forwarding API */
public class EnableForwarding {
  /**
   * Enable the auto-forwarding for an account.
   *
   * @param forwardingEmail - Email address of the recipient whose email will be forwarded.
   * @return forwarding id and metadata, {@code null} otherwise.
   * @throws IOException - if service account credentials file not found.
   */
  public static AutoForwarding enableAutoForwarding(String forwardingEmail) throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(GmailScopes.GMAIL_SETTINGS_SHARING);
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

    // Create the gmail API client
    Gmail service = new Gmail.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Gmail samples")
        .build();

    try {
      // Enable auto-forwarding and move forwarded messages to the trash
      ForwardingAddress address = new ForwardingAddress()
          .setForwardingEmail(forwardingEmail);
      ForwardingAddress createAddressResult = service.users().settings().forwardingAddresses()
          .create("me", address).execute();
      if (createAddressResult.getVerificationStatus().equals("accepted")) {
        AutoForwarding autoForwarding = new AutoForwarding()
            .setEnabled(true)
            .setEmailAddress(address.getForwardingEmail())
            .setDisposition("trash");
        autoForwarding =
            service.users().settings().updateAutoForwarding("me", autoForwarding).execute();
        System.out.println(autoForwarding.toPrettyString());
        return autoForwarding;
      }
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 403) {
        System.err.println("Unable to enable forwarding: " + e.getDetails());
      } else {
        throw e;
      }
    }
    return null;
  }
}

Python

gmail/snippet/settings snippets/enable_forwarding.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def enable_forwarding():
  """Enable email forwarding.
  Returns:Draft object, including forwarding id and result meta data.

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create gmail api client
    service = build("gmail", "v1", credentials=creds)

    address = {"forwardingEmail": "gduser1@workspacesamples.dev"}

    # pylint: disable=E1101
    result = (
        service.users()
        .settings()
        .forwardingAddresses()
        .create(userId="me", body=address)
        .execute()
    )
    if result.get("verificationStatus") == "accepted":
      body = {
          "emailAddress": result.get("forwardingEmail"),
          "enabled": True,
          "disposition": "trash",
      }
      # pylint: disable=E1101
      result = (
          service.users()
          .settings()
          .updateAutoForwarding(userId="me", body=body)
          .execute()
      )
      print(f"Forwarding is enabled : {result}")

  except HttpError as error:
    print(f"An error occurred: {error}")
    result = None

  return result


if __name__ == "__main__":
  enable_forwarding()

अपने-आप फ़ॉरवर्ड होने की सुविधा बंद करने के लिए, updateAutoForwarding तरीके को कॉल करें और AutoForwarding ऑब्जेक्ट पर enabled फ़ील्ड को false पर सेट करें.

चुनिंदा मैसेज फ़ॉरवर्ड करना

अपने-आप फ़ॉरवर्ड होने की सुविधा चालू होने पर, Gmail पर मिले सभी ईमेल, टारगेट खाते पर भेज दिए जाते हैं. कुछ खास मैसेज फ़ॉरवर्ड करने के लिए, फ़िल्टर सेट अप करें. इससे ऐसे नियम बनाए जा सकते हैं जो मैसेज के एट्रिब्यूट या कॉन्टेंट के आधार पर मैसेज फ़ॉरवर्ड करते हैं.

ईमेल को कई खातों पर फ़ॉरवर्ड करने के लिए, ईमेल फ़ॉरवर्ड करने के लिए चुने गए हर ईमेल पते के लिए एक फ़िल्टर बनाएं.