External Software
— What can you do?  — Everything!
  1. Main
  2. /
  3. Google Apps Script
  4.   7 min

Google Apps Script Autoreply and Autoforward: Tips and Tricks

  •   Mikhail Krotov
  •   April 20th, 2023
  •   22

As a busy professional or business owner, automating your email responses can help you save time and respond to your clients and colleagues faster. Google Apps Script is a powerful tool that allows you to perform these and many other automation tasks using JavaScript. In this tutorial, we will show you how to use Google Apps Script to automatically forward emails when you reply to them.

Why use Google Apps Script?

Google Apps Script is a cloud-based scripting language that allows you to create and automate various tasks in Google products, including Gmail, Google Drive, Calendar, and many others. With Google Apps Script, you can extend the functionality of pre-existing Google Apps, or create your own stand-alone web applications.

One of the many benefits of using Google Apps Script is that it is free, and you can access it right from your Google account. Additionally, it is easy to learn and use, making it the perfect tool for businesses that want to automate their workflows without the need for expensive software or developers.

Getting Started

Before we dive into the code, let’s take a moment to set up our environment. To get started, you’ll need a Gmail account and a basic understanding of JavaScript. Once you have these, follow these steps:

  1. Open your Gmail account and click on the gear icon in the top right corner.
  2. Select “Settings” from the dropdown menu.
  3. Scroll down to the “Forwarding and POP/IMAP” tab.
  4. Under the “Forwarding” section, click on the “Add a forwarding address” button.
  5. Enter the email address to which you want to forward incoming emails.
  6. Click “Next” and then click “Proceed”.
  7. Gmail will now send a verification email to the email address you have entered. Click on the verification link to complete the process.

Now that we have set up our forwarding address, we can move on to creating our Google Apps Script.

Creating the Script

  1. Open a new Google Sheet and click on the “Tools” menu.
  2. Select “Script editor” from the dropdown menu.
  3. In the Script editor, give your script a name by clicking on “File” and selecting “Rename”. We will name ours “AutoForwardEmailScript”.
  4. Next, copy and paste the following code into the script editor:
    function autoForwardEmails() {
        var label = GmailApp.getUserLabelByName('auto-forward-emails');
        var threads = label.getThreads();
        for (var i = 0; i < threads.length; i++) {
            var thread = threads[i];
            var messages = thread.getMessages();
            for (var j = 0; j < messages.length; j++) {
                var message = messages[j];
                if (message.isInReplyTo()) {
                    var from = message.getFrom();
                    var body = message.getBody();
                    var subject = message.getSubject();
                    GmailApp.sendEmail("INSERT FORWARDING EMAIL ADDRESS HERE", subject, from + "\n\n" + body);
                    thread.removeLabel(label);
                }
            }
        }
    }
    

    This code defines a function called “autoForwardEmails” that will search for emails in a specific label called “auto-forward-emails”, and forward them to a specified email address, which we will set shortly. The function then removes the “auto-forward-emails” label from the forwarded email to avoid forwarding it again.

  5. Save your script by clicking on “File” and selecting “Save”.

Configuring the Script

Now that we have created the script, let’s configure it so that it works for us.

  • In your script editor, click on “Edit” and select “Current project’s triggers”.
  • Click the “Add trigger” button in the bottom right corner.
  • In the “Run” dropdown menu, select “autoForwardEmails”.
  • In the “Events” dropdown menu, select “From Inbox”.
  • Click “Save”.

This will set up a trigger that will run the “autoForwardEmails” function whenever a new email arrives in your inbox.

Final Steps

We’re almost done! All that’s left is to test the script and make sure it works.

Here’s what you need to do:

  1. In Gmail, open an email and click “Reply”.
  2. In the reply window, add the label “auto-forward-emails”.
  3. Send the reply.

The script should automatically forward the email to the email address you specified in the code.

Congratulations!

You have now automated your email forwarding process using Google Apps Script. You can customize the code further to add more features and functionality, such as adding a delay before forwarding emails, or adding specific conditions for forwarding emails based on sender or subject.

Tags
  • account
  • Apps
  • Automate
  • Automation
  • Configuration
  • Email
  • environment
  • Forwarding
  • free
  • function
  • Gmail
  • Google
  • Inbox
  • Javascript
  • label
  • Learn
  • Messages
  • script
  • settings
  • trigger
←How to add Google Tag Manager form submit event to Google Ads conversions?Previous article
→How do you get your Google Ads to show in your Google My Business listing?Next article

Related

Automating YouTube Descriptions with Google Apps Script
As a content creator on YouTube, uploading videos and managing channel details can often become a tedious task.
  •   17
  •   2023
What can you do with Google Apps Script?
Google Apps Script is a powerful tool that allows you to automate tasks and customize your Google Workspace apps like Sheets, Docs, and Slides.
  •   19
  •   2023
Deleting Duplicates in Google Drive with Google Apps Script
Google Drive is a great tool for storing and organizing files, but it can be cumbersome to manage and de-duplicate files.
  •   17
  •   2023
How to Create a New Library with Google Apps Script
Google Apps Script is a powerful tool for automating tasks and integrating different Google services together.
  •   12
  •   2023

Popular

How to execute third-party code in Google Ads?
How to use ChatGPT for website accessibility design?
Google Ads Conversion Tracking Tags: Why You Need Them and How to Set Them Up?
Why Are Target CPA Bidding Campaigns Limited by Budget Frequently in Google Ads?
Understanding the Role of Document Features in SEO
How to Add Rule-Based Audiences to Google Ads Campaigns for Effective Targeting?
How to Create Amazing SEO Dashboards to Impress Your Clients
Sitelinks in Yandex.Direct and Google Ads

Tools

  • Phrase processing
  • Phrase Generator/Combinator
  • WP TagGen

Other

  • Word lists
  • World country codes
  • Charity

Categories

  • AI & ML
  • Analytics
  • Contextual advertising
  • Development
  • Google Ads
  • Google Ads Scripts
  • Google Analytics
  • Google Apps Script
  • SEO
  • Yandex.Direct

Archive

  • May 2023
  • April 2023
  • March 2023
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • May 2016
  • January 2016
  • November 2015
  • August 2015
  • July 2015
  • May 2015
  • March 2015
  • February 2015
  • December 2014
  • November 2014
  • September 2014
  • May 2014
  • April 2014

Search

  •   RSS
  •   GitHub
  •   Telegram
  •   Фибулист
  •   Limitation of liability
  •   Privacy policy