Selective Gmail Auto-Reply

In Gmail, you can set a vacation responder, which is great for automatic replies. However, if you have more than one address linked to your account, you cannot set the vacation responder to reply specifically to emails that were sent to one of your addresses only.

This is problematic for me. I have two work email addresses, two developer addresses, and of course my personal one. And everything feeds into the same Gmail inbox! When I go on vacation, I want to set an automatic reply to work-related email, but I don’t want that same message go out to my personal email! Another issue occurred when I changed jobs: my former email address will stop working at some point, so I want to set an auto-reply for that address that tells people to update to my new address.

To solve these problems, I had two options. I could get my life together: Having everything come into one inbox seems to be a very disorganised way of doing stuff, and I should find a better way. Or I could stick with my bad habit, and instead hack my way to a temporary patch. Obviously, I chose the latter.

I found the solution in Google Script, which allows you to set up some JavaScript code to be run periodically. Suppose you change jobs: Your old email is my_mail@oldjob.com and will be deactivated in a few weeks. Your new work address is my_mail@newjob.com, and both work addresses are synced with your Gmail (my_mail@gmail.com). You want people that send messages to your old address to receive an automatic reply that tells them to send their email to your new address instead. Here’s how to do it:

  1. Go to gmail.com and log into your account (my_mail@gmail.com).
  2. Go to script.google.com. You should be automatically logged in if you did step 1.
  3. Open a new script: File -> New -> Script file. Call it autoreply.js.
  4. Paste the following code into your script, and make sure to change the email addresses to match your own:

  5. Set a timer, to have your script run automatically every now and again. Go to Resources -> Current project’s triggers -> Add a new trigger. Set RespondEmail to Time-driven, Minute timer, Every minute.

The script will now run every minute, and check if there are any unread emails to my_mail@oldjob.com. If it finds any unread emails to my_mail@oldjob.com, it will automatically send them a reply (from my_mail@newjob.com), and will then mark the message as read. Easy!

Now you don’t have to manually reply to all those people using your old email, or to people that send you work-related messages while you’re on holiday.

6 Comments:

  1. Hi Edwin,
    Thanks for the write-up, really appreciate. I am trying to modify this so that it works as an auto-reply that runs every minute from 6pm to 8am every day but I believe I have to write a trigger to achieve this.

    Would you have any tips?

    Thank you,

    • Very good question! I’m currently working on a periodic auto-mailer (e.g. to remind lazy co-authors). I’ll include a period auto-reply in there too, as per your request.

      Cheers,
      Edwin

  2. I’m trying to figure out a way of having an out of office hours (including weekends) autoresponder.

    I would love to know if you managed to write the auto-mailer scheduler, and if you would be willing to share the script.

    Thanks

    Derek

  3. This script is not working, it is not sending reply to the sender. It is sending email to me as the script says
    //send response email
    var threads = GmailApp.search(“to:(contactnebi@gmail.com) label:unread”);

    How to replace my email ID with receivers email ID?

    • You copied the bit of code that does a search for all messages that are directed to your mail address (line 7). The reply happens in lines 8-9 (specifically in line 9; line 8 just loops through all emails found by the search in line 7). The script works, because it uses the reply function. The only way in which it’ll send an email to you, is if it’s replying to an email that you sent. (Which you might have done to test it?)

  4. Thank you for sharing this script it is exactly what I had been looking for and works perfectly.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">