0

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 Answers1

0

Try using Admin SDK Groups Migration Service in App Script.

A code snippet is provided for you to try:

function migrateMessages() {
  var groupId = 'exampleGroup@example.com';
  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;
}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • I'm not attempting to move it to a google group. I want to move the emails to another user account. USE CASE: User A is to be deleted, and I want emails moved to User B before they get deleted along with the account. – Jeremiah Gibson Sep 06 '16 at 17:32
  • [Move or Copy Mail From One Gmail Account to Another](http://email.about.com/od/gmailtips/qt/How-To-Move-Mail-From-One-Gmail-Account-To-Another-Using-Only-Gmail.htm) – ReyAnthonyRenacia Sep 07 '16 at 05:53
  • Thanks for the continuing support. However I need to programmatically do it this as it happens frequently. – Jeremiah Gibson Sep 08 '16 at 16:01