0
votes

Using the Google Admin SDK & gmail API is there a way to automate user email transfers from one account to another user the way Google data migration tool?

I'm looking for any existing scripts or methods that can be implemented in an automated system. (Solutions can contain python,javascript,php)

1

1 Answers

0
votes

Try using Admin SDK Groups Migration Service in App Script.

A code snippet is provided for you to try:

function migrateMessages() {
  var groupId = '[email protected]';
  var messagesToMigrate = getRecentMessagesContent();
  for (var i = 0; i < messagesToMigrate.length; i++) {
    var messageContent = messagesToMigrate[i];
    var contentBlob = Utilities.newBlob(messageContent, 'message/rfc822');
    AdminGroupsMigration.Archive.insert(groupId, contentBlob);
  }
}

/**
 * Gets a list of recent messages' content from the user's Gmail account.
 * By default, fetches 3 messages from the latest 3 threads.
 *
 * @return {Array} the messages' content.
 */
function getRecentMessagesContent() {
  var NUM_THREADS = 3;
  var NUM_MESSAGES = 3;
  var threads = GmailApp.getInboxThreads(0, NUM_THREADS);
  var messages = GmailApp.getMessagesForThreads(threads);
  var messagesContent = [];
  for (var i = 0; i < messages.length; i++) {
    for (var j = 0; j < NUM_MESSAGES; j++) {
      var message = messages[i][j];
      if (message) {
        messagesContent.push(message.getRawContent());
      }
    }
  }
  return messagesContent;
}