Gmail api& Admin SDK用于在同一组织中的Gmail帐户之间传输电子邮件

时间:2016-09-02 21:26:24

标签: javascript gmail-api google-admin-sdk

使用Google Admin SDK& gmail API有一种方法可以像Google数据迁移工具那样自动将用户电子邮件从一个帐户转移到另一个用户吗?

我正在寻找可以在自动化系统中实现的任何现有脚本或方法。 (解决方案可以包含python,javascript,php)

1 个答案:

答案 0 :(得分:0)

尝试在App脚本中使用Admin SDK Groups Migration Service

您可以尝试使用代码段:

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;
}