Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Google Apps Script for Automatic Out of Office Replies
- * Works: 5pm-9am on weekdays and entire weekends
- */
- // SETTINGS - Customize according to your needs
- const OOO_SETTINGS = {
- // Working hours (24-hour format)
- workStartHour: 9, // 9am
- workEndHour: 17, // 5pm
- // Out of office message
- oooMessage: {
- subject: "Auto-Reply: Out of Office",
- body: `
- Hello,
- Thank you for your email. I am currently out of office.
- Office Hours: Monday-Friday, 9:00-17:00
- I will respond as soon as possible during business hours.
- For urgent matters, please contact [ADD YOUR PHONE NUMBER HERE].
- Best regards,
- [YOUR NAME HERE]
- ---
- This is an automated response.
- `
- },
- // Label to track which emails have already been replied to
- labelName: "OutOfOffice-AutoReplied"
- };
- /**
- * Main function that checks and sends out of office replies
- */
- function checkAndSendOOOReplies() {
- try {
- // Check if we are out of office hours
- if (!isOutOfOffice()) {
- console.log("We are within business hours - not sending OOO replies");
- return;
- }
- console.log("Out of office hours - checking for new emails");
- // Find or create the label
- const label = getOrCreateLabel(OOO_SETTINGS.labelName);
- // Get new emails (last 2 hours, without our label)
- const query = `is:unread -label:${OOO_SETTINGS.labelName} newer_than:2h`;
- const threads = GmailApp.search(query, 0, 50);
- console.log(`Found ${threads.length} new emails`);
- threads.forEach(thread => {
- const messages = thread.getMessages();
- const lastMessage = messages[messages.length - 1];
- // Check if the email is incoming (not from us)
- if (lastMessage.getFrom().includes(Session.getActiveUser().getEmail())) {
- console.log("Skipping email that we sent");
- return;
- }
- // Send the auto-reply
- sendOOOReply(lastMessage);
- // Add the label so we don't send again
- thread.addLabel(label);
- console.log(`Sent OOO reply to: ${lastMessage.getFrom()}`);
- });
- } catch (error) {
- console.error("Error in checkAndSendOOOReplies:", error);
- }
- }
- /**
- * Checks if we are out of office hours
- */
- function isOutOfOffice() {
- const now = new Date();
- const day = now.getDay(); // 0 = Sunday, 1 = Monday, etc
- const hour = now.getHours();
- // Weekend (Saturday = 6, Sunday = 0)
- if (day === 0 || day === 6) {
- return true;
- }
- // Weekdays outside office hours
- if (hour < OOO_SETTINGS.workStartHour || hour >= OOO_SETTINGS.workEndHour) {
- return true;
- }
- return false;
- }
- /**
- * Sends the out of office reply
- */
- function sendOOOReply(originalMessage) {
- try {
- const originalSubject = originalMessage.getSubject();
- const senderEmail = originalMessage.getFrom();
- // Check if it's already an auto-reply to avoid loops
- if (originalSubject.toLowerCase().includes('auto-reply') ||
- originalSubject.toLowerCase().includes('out of office') ||
- originalSubject.toLowerCase().includes('automated response')) {
- console.log("Skipping auto-reply message");
- return;
- }
- // Create the reply
- const replySubject = `${OOO_SETTINGS.oooMessage.subject}`;
- GmailApp.sendEmail(
- senderEmail,
- replySubject,
- OOO_SETTINGS.oooMessage.body,
- {
- replyTo: Session.getActiveUser().getEmail(),
- htmlBody: OOO_SETTINGS.oooMessage.body.replace(/\n/g, '<br>')
- }
- );
- } catch (error) {
- console.error("Error in sendOOOReply:", error);
- }
- }
- /**
- * Finds or creates label
- */
- function getOrCreateLabel(labelName) {
- let label = GmailApp.getUserLabelByName(labelName);
- if (!label) {
- label = GmailApp.createLabel(labelName);
- console.log(`Created new label: ${labelName}`);
- }
- return label;
- }
- /**
- * Test function to try the script
- */
- function testScript() {
- console.log("=== SCRIPT TEST ===");
- console.log("Current time:", new Date());
- console.log("We are out of office hours:", isOutOfOffice());
- // Test trigger
- checkAndSendOOOReplies();
- }
Advertisement
Add Comment
Please, Sign In to add comment