Guest User

qq

a guest
Aug 3rd, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. /**
  2. * Google Apps Script for Automatic Out of Office Replies
  3. * Works: 5pm-9am on weekdays and entire weekends
  4. */
  5.  
  6. // SETTINGS - Customize according to your needs
  7. const OOO_SETTINGS = {
  8. // Working hours (24-hour format)
  9. workStartHour: 9, // 9am
  10. workEndHour: 17, // 5pm
  11.  
  12. // Out of office message
  13. oooMessage: {
  14. subject: "Auto-Reply: Out of Office",
  15. body: `
  16. Hello,
  17.  
  18. Thank you for your email. I am currently out of office.
  19.  
  20. Office Hours: Monday-Friday, 9:00-17:00
  21.  
  22. I will respond as soon as possible during business hours.
  23.  
  24. For urgent matters, please contact [ADD YOUR PHONE NUMBER HERE].
  25.  
  26. Best regards,
  27. [YOUR NAME HERE]
  28.  
  29. ---
  30. This is an automated response.
  31. `
  32. },
  33.  
  34. // Label to track which emails have already been replied to
  35. labelName: "OutOfOffice-AutoReplied"
  36. };
  37.  
  38. /**
  39. * Main function that checks and sends out of office replies
  40. */
  41. function checkAndSendOOOReplies() {
  42. try {
  43. // Check if we are out of office hours
  44. if (!isOutOfOffice()) {
  45. console.log("We are within business hours - not sending OOO replies");
  46. return;
  47. }
  48.  
  49. console.log("Out of office hours - checking for new emails");
  50.  
  51. // Find or create the label
  52. const label = getOrCreateLabel(OOO_SETTINGS.labelName);
  53.  
  54. // Get new emails (last 2 hours, without our label)
  55. const query = `is:unread -label:${OOO_SETTINGS.labelName} newer_than:2h`;
  56. const threads = GmailApp.search(query, 0, 50);
  57.  
  58. console.log(`Found ${threads.length} new emails`);
  59.  
  60. threads.forEach(thread => {
  61. const messages = thread.getMessages();
  62. const lastMessage = messages[messages.length - 1];
  63.  
  64. // Check if the email is incoming (not from us)
  65. if (lastMessage.getFrom().includes(Session.getActiveUser().getEmail())) {
  66. console.log("Skipping email that we sent");
  67. return;
  68. }
  69.  
  70. // Send the auto-reply
  71. sendOOOReply(lastMessage);
  72.  
  73. // Add the label so we don't send again
  74. thread.addLabel(label);
  75.  
  76. console.log(`Sent OOO reply to: ${lastMessage.getFrom()}`);
  77. });
  78.  
  79. } catch (error) {
  80. console.error("Error in checkAndSendOOOReplies:", error);
  81. }
  82. }
  83.  
  84. /**
  85. * Checks if we are out of office hours
  86. */
  87. function isOutOfOffice() {
  88. const now = new Date();
  89. const day = now.getDay(); // 0 = Sunday, 1 = Monday, etc
  90. const hour = now.getHours();
  91.  
  92. // Weekend (Saturday = 6, Sunday = 0)
  93. if (day === 0 || day === 6) {
  94. return true;
  95. }
  96.  
  97. // Weekdays outside office hours
  98. if (hour < OOO_SETTINGS.workStartHour || hour >= OOO_SETTINGS.workEndHour) {
  99. return true;
  100. }
  101.  
  102. return false;
  103. }
  104.  
  105. /**
  106. * Sends the out of office reply
  107. */
  108. function sendOOOReply(originalMessage) {
  109. try {
  110. const originalSubject = originalMessage.getSubject();
  111. const senderEmail = originalMessage.getFrom();
  112.  
  113. // Check if it's already an auto-reply to avoid loops
  114. if (originalSubject.toLowerCase().includes('auto-reply') ||
  115. originalSubject.toLowerCase().includes('out of office') ||
  116. originalSubject.toLowerCase().includes('automated response')) {
  117. console.log("Skipping auto-reply message");
  118. return;
  119. }
  120.  
  121. // Create the reply
  122. const replySubject = `${OOO_SETTINGS.oooMessage.subject}`;
  123.  
  124. GmailApp.sendEmail(
  125. senderEmail,
  126. replySubject,
  127. OOO_SETTINGS.oooMessage.body,
  128. {
  129. replyTo: Session.getActiveUser().getEmail(),
  130. htmlBody: OOO_SETTINGS.oooMessage.body.replace(/\n/g, '<br>')
  131. }
  132. );
  133.  
  134. } catch (error) {
  135. console.error("Error in sendOOOReply:", error);
  136. }
  137. }
  138.  
  139. /**
  140. * Finds or creates label
  141. */
  142. function getOrCreateLabel(labelName) {
  143. let label = GmailApp.getUserLabelByName(labelName);
  144. if (!label) {
  145. label = GmailApp.createLabel(labelName);
  146. console.log(`Created new label: ${labelName}`);
  147. }
  148. return label;
  149. }
  150.  
  151. /**
  152. * Test function to try the script
  153. */
  154. function testScript() {
  155. console.log("=== SCRIPT TEST ===");
  156. console.log("Current time:", new Date());
  157. console.log("We are out of office hours:", isOutOfOffice());
  158.  
  159. // Test trigger
  160. checkAndSendOOOReplies();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment