Gmail की बेहतर सेवा

Gmail की बेहतर सेवा आपको Gmail में Gmail API का इस्तेमाल करने की अनुमति देती है Apps Script. Apps Script की पहले से मौजूद Gmail सेवा की तरह, यह एपीआई स्क्रिप्ट को Gmail मेलबॉक्स. ज़्यादातर मामलों में, पहले से मौजूद सेवा का इस्तेमाल करना आसान होता है, लेकिन यह ऐडवांस सेवा, कुछ अतिरिक्त सुविधाएं और ज़्यादा जानकारी वाला ऐक्सेस देती है Gmail कॉन्टेंट के बारे में जानकारी.

रेफ़रंस

इस सेवा के बारे में विस्तृत जानकारी के लिए, देखें Gmail API के लिए रेफ़रंस दस्तावेज़. Apps Script की सभी बेहतर सेवाओं की तरह, बेहतर Gmail सेवा भी एपीआई की तरह ही सभी ऑब्जेक्ट, तरीके, और पैरामीटर. ज़्यादा जानकारी के लिए, डिलीवरी के तरीके के हस्ताक्षर तय करने का तरीका देखें.

समस्याओं की रिपोर्ट करने और अन्य सहायता पाने के लिए, देखें Gmail की सहायता गाइड.

नमूना कोड

नीचे दिया गया सैंपल कोड, एपीआई के वर्शन 1 का इस्तेमाल करता है.

लेबल की जानकारी की सूची बनाएं

नीचे दिया गया उदाहरण उपयोगकर्ता के लेबल की सभी जानकारी को सूची में शामिल करने का तरीका बताता है. इसमें लेबल का नाम, टाइप, आईडी, और 'किसको दिखे' सेटिंग शामिल हैं.

Advanced/gmail.gs
/**
 * Lists the user's labels, including name, type,
 * ID and visibility information.
 */
function listLabelInfo() {
  try {
    const response =
      Gmail.Users.Labels.list('me');
    for (let i = 0; i < response.labels.length; i++) {
      const label = response.labels[i];
      console.log(JSON.stringify(label));
    }
  } catch (err) {
    console.log(err);
  }
}

इनबॉक्स स्निपेट की सूची बनाना

नीचे दिए गए उदाहरण में, लेख स्निपेट से जुड़े टेक्स्ट स्निपेट की सूची बनाने का तरीका बताया गया है उपयोगकर्ता के इनबॉक्स में हर थ्रेड. Google Analytics को ऐक्सेस करने के लिए, पेज टोकन के इस्तेमाल पर ध्यान दें परिणामों की पूरी सूची.

Advanced/gmail.gs
/**
 * Lists, for each thread in the user's Inbox, a
 * snippet associated with that thread.
 */
function listInboxSnippets() {
  try {
    let pageToken;
    do {
      const threadList = Gmail.Users.Threads.list('me', {
        q: 'label:inbox',
        pageToken: pageToken
      });
      if (threadList.threads && threadList.threads.length > 0) {
        threadList.threads.forEach(function(thread) {
          console.log('Snippet: %s', thread.snippet);
        });
      }
      pageToken = threadList.nextPageToken;
    } while (pageToken);
  } catch (err) {
    console.log(err);
  }
}

हाल ही का इतिहास दिखाएं

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

Advanced/gmail.gs
/**
 * Gets a history record ID associated with the most
 * recently sent message, then logs all the message IDs
 * that have changed since that message was sent.
 */
function logRecentHistory() {
  try {
    // Get the history ID associated with the most recent
    // sent message.
    const sent = Gmail.Users.Threads.list('me', {
      q: 'label:sent',
      maxResults: 1
    });
    if (!sent.threads || !sent.threads[0]) {
      console.log('No sent threads found.');
      return;
    }
    const historyId = sent.threads[0].historyId;

    // Log the ID of each message changed since the most
    // recent message was sent.
    let pageToken;
    const changed = [];
    do {
      const recordList = Gmail.Users.History.list('me', {
        startHistoryId: historyId,
        pageToken: pageToken
      });
      const history = recordList.history;
      if (history && history.length > 0) {
        history.forEach(function(record) {
          record.messages.forEach(function(message) {
            if (changed.indexOf(message.id) === -1) {
              changed.push(message.id);
            }
          });
        });
      }
      pageToken = recordList.nextPageToken;
    } while (pageToken);

    changed.forEach(function(id) {
      console.log('Message Changed: %s', id);
    });
  } catch (err) {
    console.log(err);
  }
}