জিমেইল ফিল্টার পরিচালনা করুন

এই ডকুমেন্টটি Gmail API-তে ফিল্টার কীভাবে ব্যবহার করবেন তা ব্যাখ্যা করে।

আপনি একটি অ্যাকাউন্টের জন্য উন্নত ফিল্টারিং নিয়ম কনফিগার করতে settings.filters রিসোর্স ব্যবহার করতে পারেন। ফিল্টারগুলি স্বয়ংক্রিয়ভাবে লেবেল যোগ করতে বা অপসারণ করতে পারে অথবা আগত বার্তার বৈশিষ্ট্য বা বিষয়বস্তুর উপর ভিত্তি করে যাচাইকৃত উপনামগুলিতে ইমেল ফরোয়ার্ড করতে পারে।

Gmail ফিল্টার তৈরি , তালিকাভুক্ত , পেতে বা মুছে ফেলার তথ্যের জন্য, settings.filters রিসোর্সটি দেখুন।

ম্যাচের মানদণ্ড

প্রেরক, বিষয়, তারিখ, আকার এবং বার্তার বিষয়বস্তুর মতো বৈশিষ্ট্য অনুসারে বার্তা ফিল্টার করুন। ফিল্টারগুলি শুধুমাত্র নির্দিষ্ট বার্তাগুলিতে প্রযোজ্য, সম্পূর্ণ ইমেল থ্রেডে নয়। Gmail এর উন্নত অনুসন্ধান সিনট্যাক্স ব্যবহার করে যেকোনো প্রশ্নও একটি ফিল্টারে ব্যবহার করা যেতে পারে। উদাহরণস্বরূপ, সাধারণ ফিল্টার প্যাটার্নগুলির মধ্যে রয়েছে:

ফিল্টার ম্যাচ
criteria.from='sender@example.com' sender@example.com থেকে আসা ইমেলগুলি
criteria.size=10485760
criteria.sizeComparison='larger'
১০ এমবি-র চেয়ে বড় ইমেল
criteria.hasAttachment=true অ্যাটাচমেন্ট সহ ইমেল
criteria.subject='[People with Pets]' [People with Pets] বিষয়ের ইমেলগুলি
criteria.query='"my important project"' my important project সম্বলিত ইমেলগুলি
criteria.negatedQuery='"secret knock"' secret knock নেই এমন ইমেল

যদি একটি ফিল্টারে একাধিক মানদণ্ড উপস্থিত থাকে, তাহলে একটি বার্তাকে ফিল্টারটি প্রয়োগের জন্য সমস্ত মানদণ্ড পূরণ করতে হবে।

কর্ম

ফিল্টারের মানদণ্ডের সাথে মিলে যাওয়া বার্তাগুলিতে একটি Action প্রয়োগ করুন। Action সাহায্যে, আপনি একটি যাচাইকৃত ইমেল ঠিকানায় বার্তা ফরোয়ার্ড করতে পারেন, অথবা লেবেল যোগ করতে এবং সরাতে পারেন।

ইমেলের অবস্থান পরিবর্তন করতে লেবেল যোগ করুন বা সরান। উদাহরণস্বরূপ, কিছু সাধারণ পদক্ষেপের মধ্যে রয়েছে:

অ্যাকশন প্রভাব
action.removeLabelIds=['INBOX'] ইমেলটি আর্কাইভ করুন (ইনবক্স এড়িয়ে যান)
action.removeLabelIds=['UNREAD'] পঠিত হিসেবে চিহ্নিত করুন
action.removeLabelIds=['SPAM'] কখনও স্প্যাম হিসেবে চিহ্নিত করবেন না।
action.removeLabelIds=['IMPORTANT'] কখনোই গুরুত্বপূর্ণ হিসেবে চিহ্নিত করবেন না
action.addLabelIds=['IMPORTANT'] গুরুত্বপূর্ণ হিসেবে চিহ্নিত করুন
action.addLabelIds=['TRASH'] ইমেলটি মুছে ফেলুন
action.addLabelIds=['STARRED'] তারকাচিহ্নিত হিসেবে চিহ্নিত করুন
action.addLabelIds=['<user label id>'] ব্যবহারকারী-সংজ্ঞায়িত লেবেল দিয়ে মেইলটি ট্যাগ করুন। প্রতি ফিল্টারে শুধুমাত্র একটি ব্যবহারকারী-সংজ্ঞায়িত লেবেল অনুমোদিত।

কোড নমুনা

নিম্নলিখিত কোড নমুনাগুলি দেখায় যে কীভাবে একটি মেইলিং তালিকা থেকে বার্তাগুলি লেবেল এবং সংরক্ষণাগারভুক্ত করতে হয়:

জাভা

জিমেইল/স্নিপেটস/এসআরসি/মেইন/জাভা/ক্রিয়েটফিল্টার.জাভা
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.Filter;
import com.google.api.services.gmail.model.FilterAction;
import com.google.api.services.gmail.model.FilterCriteria;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;

/* Class to demonstrate the use of Gmail Create Filter API */
public class CreateFilter {
  /**
   * Create a new filter.
   *
   * @param labelId - ID of the user label to add
   * @return the created filter id, {@code null} otherwise.
   * @throws IOException - if service account credentials file not found.
   */
  public static String createNewFilter(String labelId) 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_BASIC,
            GmailScopes.GMAIL_LABELS);
    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 {
      // Filter the mail from sender and archive them(skip the inbox)
      Filter filter = new Filter()
          .setCriteria(new FilterCriteria()
              .setFrom("gduser2@workspacesamples.dev"))
          .setAction(new FilterAction()
              .setAddLabelIds(Arrays.asList(labelId))
              .setRemoveLabelIds(Arrays.asList("INBOX")));

      Filter result = service.users().settings().filters().create("me", filter).execute();
      // Prints the new created filter ID
      System.out.println("Created filter " + result.getId());
      return result.getId();
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 403) {
        System.err.println("Unable to create filter: " + e.getDetails());
      } else {
        throw e;
      }
    }
    return null;
  }
}

পাইথন

জিমেইল/স্নিপেট/সেটিংস স্নিপেট/create_filter.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def create_filter():
  """Create a filter.
  Returns: Draft object, including filter id.

  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)

    label_name = "IMPORTANT"
    filter_content = {
        "criteria": {"from": "gsuder1@workspacesamples.dev"},
        "action": {
            "addLabelIds": [label_name],
            "removeLabelIds": ["INBOX"],
        },
    }

    # pylint: disable=E1101
    result = (
        service.users()
        .settings()
        .filters()
        .create(userId="me", body=filter_content)
        .execute()
    )
    print(f'Created filter with id: {result.get("id")}')

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

  return result.get("id")


if __name__ == "__main__":
  create_filter()