Many professionals face the same frustrating limitation: you receive repetitive emails that could easily be automated, but your corporate IT policies prevent you from adding API integrations to your work email system.
If you're stuck manually crafting the same responses over and over again, there's a clever workaround that combines email forwarding, Google Apps Script, and AI to create a powerful automation system that works around corporate restrictions.
As a non-365 administrator, you can't add API calls to Outlook to automate email replies using an LLM. But you still need to respond to dozens of similar requests efficiently while maintaining a professional, personalized touch.
This automation uses a simple but effective approach:
The entire system runs for free (or nearly free) and bypasses corporate API restrictions entirely.
1. Create a dedicated Gmail account
yourname.automation@gmail.com2. Set up Google Cloud Project
3. Configure OAuth credentials
4. Set up Outlook forwarding rule
5. Create Google Apps Script project
6. Get Gemini API key
GEMINI_API_KEY, Value = your API key7. Implement the automation code
Here's the complete automation script (customize the templates for your use case):
function processForwardedEmails() {
try {
console.log("Starting email processing...");
// Look for unread forwarded emails
var threads = GmailApp.search('is:unread subject:"FW:" OR subject:"Fwd:"', 0, 10);
console.log("Found " + threads.length + " unread forwarded threads");
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
var latestMessage = messages[messages.length - 1];
var emailBody = latestMessage.getPlainBody();
console.log("Processing email " + (i+1) + ": " + thread.getFirstMessageSubject());
// Check for template keyword at start of email
var templateType = 'standard'; // default
if (emailBody.toLowerCase().startsWith('template2')) {
templateType = 'alternative';
emailBody = emailBody.substring(9).trim(); // Remove keyword
}
var originalSubject = extractOriginalSubject(emailBody, thread.getFirstMessageSubject());
console.log("Generating reply with template:", templateType);
var reply = generateReply(emailBody, templateType);
console.log("Sending reply to work email...");
sendReplyToSelf(originalSubject, emailBody, reply);
thread.markRead();
}
console.log("Processing complete!");
} catch (error) {
console.log("Error: " + error.toString());
}
}
function generateReply(emailBody, templateType) {
try {
var apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
var template = '';
var instructions = '';
if (templateType === 'alternative') {
template = `Hi {NAME_OF_ORIGINAL_SENDER},
Thank you for your inquiry. I'd be happy to help you with this request.
[Your alternative template content here]
Please let me know if you have any questions.
Best regards,
[Your Name]`;
instructions = 'Use this alternative template and replace {NAME_OF_ORIGINAL_SENDER} with the sender\'s first name only.';
} else {
template = `Hi {NAME_OF_ORIGINAL_SENDER},
Thank you for reaching out.
[Your standard template content here]
I look forward to hearing from you.
Best regards,
[Your Name]`;
instructions = 'Use this standard template and replace {NAME_OF_ORIGINAL_SENDER} with the sender\'s first name only.';
}
var prompt = `${instructions}
TEMPLATE:
${template}
EMAIL TO RESPOND TO:
${emailBody}
CUSTOMIZED REPLY:`;
var payload = {
'contents': [{
'parts': [{
'text': prompt
}]
}]
};
var options = {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
},
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(
`https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key=${apiKey}`,
options
);
var data = JSON.parse(response.getContentText());
if (data.candidates && data.candidates[0] && data.candidates[0].content) {
return data.candidates[0].content.parts[0].text;
} else {
return "Error generating reply - please check manually";
}
} catch (error) {
console.log("Gemini API error: " + error.toString());
return "Error generating reply: " + error.toString();
}
}
function extractOriginalSubject(emailBody, forwardedSubject) {
// Try to find original subject in forwarded email body
var subjectMatch = emailBody.match(/Subject:\s*(.+)/i);
if (subjectMatch && subjectMatch[1]) {
return subjectMatch[1].trim();
}
// Fallback: remove "Fw:" from Gmail subject
return forwardedSubject.replace(/^(Fw:|Fwd:)\s*/i, '').trim();
}
function sendReplyToSelf(originalSubject, originalBody, generatedReply) {
var subject = "AUTOMATED REPLY - " + originalSubject;
var body = "Generated Reply:\n" + generatedReply + "\n\n--- Original Email ---\n" + originalBody;
// Replace with your work email address
GmailApp.sendEmail("your.work.email@company.com", subject, body);
}8. Test the system
processForwardedEmails() in Apps Script9. Set up automatic execution
processForwardedEmails function10. Customize for your workflow
To use different templates, add keywords to the beginning of forwarded emails:
You can expand this system to handle various scenarios:
This automation solution demonstrates that corporate IT restrictions don't have to limit your productivity. By creatively combining existing tools and services, you can build powerful workflow automations that save hours of manual work while maintaining professional standards.
The key is thinking outside the box - sometimes the best solution isn't a direct API integration, but a clever workaround that achieves the same result. With this system in place, you can focus on higher-value work while AI handles your routine email responses.
Ready to implement this for your own workflow? Start with the setup steps above and customize the templates for your specific use case. The entire system can be built and deployed in under an hour.