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 and will be deactivated in a few weeks. Your new work address is, and both work addresses are synced with your Gmail ( 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 and log into your account (
  2. Go to 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 If it finds any unread emails to, it will automatically send them a reply (from, 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.


  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.


  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.



  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 =“to:( 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.

  5. just wanted to say thank you very much!

  6. Hi, this was working perfectly up until about two weeks ago, it now sends the reply back to me, I have changed nothing so I assume it must be a Gmail change. I use it to reply to a Mailchimp beamed campaign, is there a way of specifying the reply to email to see if this is the problem.

  7. If you do not specify a “reply to” address in the call to threads.reply, it may reply to the “Return-Path:” in the email envelope header, which may be different than the “From:” or “Reply-to:” address in the message header.

  8. Hey man!

    Wonderful script.

    My question is: is it possible to only reply to unread, new mail adresses? If you have a conversation with like a “known” sender, can the script mark that adress as “known sender” and doesnt automatically reply to new mails from that sender?

    Like: Only reply to the “first ever email” that somebody sent to me.

    That’d be great!

    • Hi Niko,

      Good question! I suppose there are two potential ways of doing this: 1) Use the gmail API to check whether any emails exist in your inbox or any other folder of the new sender. Downside: computationally expensive if you have many emails, and also misidentifies senders as “new” if you’ve deleted all previous communication. 2) Maintain a list of all sender (in whatever format; could be as text file, database entries, or even by adding all new senders to your Contacts), and check any new email against that.


      • Can we not add a new label for a new sender_mail like “known sender” with your script? And if another mail is coming, it will see it as “known sender” 😛

  9. Is there a way to include an attachment? I want the auto reply to include a PDF attachment.

  10. Pingback: Selective gmail auto-reply – Quantitative Exploration of Development (Q.E.D.)

Leave a Reply to Breanna Cancel 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="">