Krahwazi

Gmail Management App

Feb 19th, 2024 (edited)
250
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.13 KB | Source Code | 0 0
  1. // Client ID and API key from the Developer Console
  2. const CLIENT_ID = 'YOUR_CLIENT_ID';
  3. const API_KEY = 'YOUR_API_KEY';
  4.  
  5. // Array of API discovery doc URLs for APIs used
  6. const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'];
  7.  
  8. // Authorization scopes required by the API
  9. const SCOPES = 'https://www.googleapis.com/auth/gmail.modify';
  10.  
  11. let authorizeButton = document.getElementById('authorize_button');
  12. let cleanupButton = document.getElementById('cleanup_button');
  13.  
  14. function handleClientLoad() {
  15.   gapi.load('client:auth2', initClient);
  16. }
  17.  
  18. function initClient() {
  19.   gapi.client.init({
  20.     apiKey: API_KEY,
  21.     clientId: CLIENT_ID,
  22.     discoveryDocs: DISCOVERY_DOCS,
  23.     scope: SCOPES
  24.   }).then(function () {
  25.     // Listen for sign-in state changes
  26.     gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
  27.  
  28.     // Handle initial sign-in state
  29.     updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
  30.     authorizeButton.onclick = handleAuthClick;
  31.     cleanupButton.onclick = cleanupInbox;
  32.   });
  33. }
  34.  
  35. function updateSigninStatus(isSignedIn) {
  36.   if (isSignedIn) {
  37.     authorizeButton.style.display = 'none';
  38.     cleanupButton.disabled = false;
  39.   } else {
  40.     authorizeButton.style.display = 'block';
  41.     cleanupButton.disabled = true;
  42.   }
  43. }
  44.  
  45. function handleAuthClick() {
  46.   gapi.auth2.getAuthInstance().signIn();
  47. }
  48.  
  49. function cleanupInbox() {
  50.   gapi.client.gmail.users.messages.list({
  51.     'userId': 'me',
  52.     'q': 'is:unread older_than:7d' // Example: Cleanup unread emails older than 7 days
  53.   }).then(function(response) {
  54.     let messages = response.result.messages;
  55.     if (messages && messages.length > 0) {
  56.       for (let i = 0; i < messages.length; i++) {
  57.         let message = messages[i];
  58.         gapi.client.gmail.users.messages.modify({
  59.           'userId': 'me',
  60.           'id': message.id,
  61.           'removeLabelIds': ['UNREAD'] // Example: Remove the 'unread' label
  62.         }).then(function() {
  63.           console.log('Email marked as read:', message.id);
  64.         });
  65.       }
  66.     } else {
  67.       console.log('No messages found.');
  68.     }
  69.   });
  70. }
  71.  
Tags: #gmail
Advertisement
Comments
  • Krahwazi
    2 years
    # text 0.53 KB | 0 0
    1. Replace 'YOUR_CLIENT_ID' and 'YOUR_API_KEY' with your actual Google API credentials. This example demonstrates a basic implementation of authorization and inbox cleanup using the Gmail API. It lists unread emails older than 7 days and marks them as read.
    2.  
    3. Please note that this code is a simplified example for demonstration purposes. Building a production-ready app would require error handling, better user feedback, and more comprehensive features for inbox management. Also, handling sensitive data like API keys securely is crucial.
Add Comment
Please, Sign In to add comment