Autoresponder for file access requests in Google mail

Recently, I needed to make it so that when requesting access to a file on Google Drive via mail, an auto-response was sent. With the help of Google scripts, I figured out how to do this and would like to show it with an example in this article.

How it works: Google script searches for unread e-mails with a request to access the file and sends an auto-reply by mail once a day, after that the request letter along with the auto-reply is moved to the trash. If a person writes to you in this chain, then the letter will be in the inbox.

In order to understand which emails will receive an auto-reply make a request by mail is:inbox is:unread Запрос доступа к файлу

is:inbox is:unread File access request

is:inbox is:unread Запрос доступа к файлу

There is nothing on my screenshot, because all the files have already been processed by the script.

Create a google script

Here are the steps to create a Google Apps script that will send auto responses to file access requests:

  1. Open Google Drive and select New > More > Google Apps Script. This will open the script editor.

  2. In the script editor, remove the default code and add the following:

// Подробное описание в статье https://habr.com/ru/articles/739168/

function autoReply() {
    for (const filename of [
            `Имя файла 1`,
            `Имя файла 2`,
            `Имя файла 3`
        ]) {
        var threads = GmailApp.search(`is:inbox is:unread Запрос доступа к файлу "${filename}"`);
        for (var i = 0; i < threads.length; i++) {
            var thread = threads[i];
            console.log(`Результат поиска: "${filename}": ${i} из ${threads.length}:\n---- ${thread.getMessages()[thread.getMessageCount() - 1].getReplyTo()};`)
            from = thread
                .getMessages()[thread.getMessageCount() - 1].getFrom()
                .split(' (через')[0]
                .replace(/\"/g, '')
            thread.reply(`Привет, ${from}!\nБлагодарю Вас за интерес к моему файлу "${filename}". Бла бла бла \n\nС уважением, Михаил Шардин.`);
            GmailApp.moveThreadsToTrash(threads.slice(i, i + 1));

            console.log(`Был обработан и удалён запрос от ${thread.getMessages()[thread.getMessageCount() - 1].getReplyTo()} к ${filename}`)
        }
    }
}
  1. Save the script by choosing File > Save. Name the script, for example “Gmail auto-responder”.

  2. Run the script by clicking Run, selecting autoReply. This will send emails to the addresses of those who requested file access, but only if you already have requests, i.e. request by mail is:inbox is:unread Запрос доступа к файлу not empty.

The result of the script is that the auto-reply is sent, and then the chains with requests and auto-replies are moved to the trash

The result of the script is that the auto-reply is sent, and then the chains with requests and auto-replies are moved to the trash

  1. Add a trigger to automatically run the script. Go to “Current Project Triggers”. Click “Add New Trigger” and select “Time Driven” to run the script once a day or even every few minutes, for example.

  Changing the trigger for a project

Changing the trigger for a project

Compose the text of the letter for an automatic reply

This is an important part and I gave it a fairly large place in the article, because the recipient will see only it. You can follow several rules:

  1. Greet the sender by name, if provided.
    You can get the sender name from a Gmail message using: var senderName = message.getFrom().split("<")[0].trim();

  2. Thank the sender for the file access request. For example:Спасибо за ваш запрос на доступ к файлу.

  3. Specify that this is an automatic response. For example:Это автоматический ответ системы запросов на доступ к файлам.

  4. Indicate the estimated time frame for responding to the request. For example: Мы стараемся отвечать на все запросы на доступ к файлам в течение 2 рабочих дней.

  5. Tell the sender what to expect in a full response.

  6. Once again, thank the sender for their patience and understanding. For example: Еще раз спасибо за ваше терпение и понимание.

  7. Finish politely and professionally. For example: С наилучшими пожеланиями, [Ваше имя]

Conclusion

Here are a few ways to use Google Apps Script to automatically respond to email file access requests:

  1. The script can check for new unread emails containing the string Запрос доступа к файлу and automatically send a response. This eliminates the need to manually check each request and respond to it.

  2. It can populate the auto-reply email with details specific to each request, such as sender’s name, request date, etc. This makes each response more personalized.

Author: Mikhail Shardin,

June 5, 2023

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *