By following these steps, you will have an email rule that moves emails containing "unfortunately" to a specific folder and marks them as read, along with sending an auto-reply of "k" to the sender.
- Open Outlook and go to the Home tab.
- Click on Rules and then Manage Rules & Alerts.
- In the Rules and Alerts dialog box, click New Rule.
- Select Apply rule on messages I receive and click Next.
- Check with specific words in the body and click on specific words.
- Type "unfortunately" and click Add, then OK.
- Click Next.
- Check move it to the specified folder.
- Click on specified and choose the folder you want to move these emails to, then click OK.
- Click Next.
- Check mark as read and click Next.
- Click Next again to skip any exceptions.
- Give your rule a name and click Finish.
- Go to File > Automatic Replies.
- Select Send automatic replies.
- Check Only send during this time range (optional).
- Enter your auto-reply message "k" in both the Inside My Organization and Outside My Organization tabs.
- Click OK to save.
- Open Gmail and click on the gear icon, then select See all settings.
- Go to the Filters and Blocked Addresses tab.
- Click Create a new filter.
- In the Has the words field, type "unfortunately".
- Click Create filter.
- Check Skip the Inbox (Archive it), Apply the label (choose or create a new label/folder), and Mark as read.
- Click Create filter.
- Open Gmail and click on the gear icon, then select See all settings.
- Go to the Vacation responder section at the bottom.
- Check Vacation responder on.
- Enter the subject and body of your auto-reply message "k".
- Select the date range (optional).
- Click Save Changes.
To set this up programmatically, you can use the Microsoft Graph API for Outlook or the Gmail API for Gmail. Here's a general outline for both.
- Register your application in Azure AD to get the necessary permissions.
- Authenticate using OAuth2 to get an access token.
- Set up a rule to move emails and mark them as read.
- Set up an auto-reply.
-
Register your application in Azure AD
- Go to the Azure portal.
- Register a new application.
- Note the Application (client) ID and Directory (tenant) ID.
- Generate a client secret.
-
Authenticate using OAuth2
- Use the Microsoft Identity platform to authenticate and get an access token.
-
Set up a rule
import requests # Replace with your values tenant_id = 'YOUR_TENANT_ID' client_id = 'YOUR_CLIENT_ID' client_secret = 'YOUR_CLIENT_SECRET' access_token = 'YOUR_ACCESS_TOKEN' # Get access token def get_access_token(): url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" headers = { 'Content-Type': 'application/x-www-form-urlencoded' } body = { 'client_id': client_id, 'scope': 'https://graph.microsoft.com/.default', 'client_secret': client_secret, 'grant_type': 'client_credentials' } response = requests.post(url, headers=headers, data=body) return response.json().get('access_token') access_token = get_access_token() # Create the rule url = "https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules" headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' } body = { "displayName": "Move unfortunately emails", "sequence": 1, "conditions": { "bodyContains": ["unfortunately"] }, "actions": { "moveToFolder": "YOUR_FOLDER_ID", "markAsRead": True } } response = requests.post(url, headers=headers, json=body) print(response.json())
-
Set up an auto-reply
url = "https://graph.microsoft.com/v1.0/me/mailboxSettings" headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' } body = { "automaticRepliesSetting": { "status": "alwaysEnabled", "internalReplyMessage": { "message": "k" }, "externalReplyMessage": { "message": "k" } } } response = requests.patch(url, headers=headers, json=body) print(response.json())
- Enable the Gmail API in the Google Cloud Console.
- Authenticate using OAuth2 to get an access token.
- Set up a filter to move emails and mark them as read.
- Set up an auto-reply.
-
Enable the Gmail API
- Go to the Google Cloud Console.
- Enable the Gmail API.
- Create OAuth 2.0 credentials.
-
Authenticate using OAuth2
- Use the Google OAuth2 library to authenticate and get an access token.
-
Set up a filter
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # Replace with your token.json path creds = Credentials.from_authorized_user_file('token.json', SCOPES) service = build('gmail', 'v1', credentials=creds) # Create the filter filter = { 'criteria': { 'query': 'unfortunately' }, 'action': { 'removeLabelIds': ['INBOX'], 'addLabelIds': ['Label_123456'], # Replace with your label ID 'markAsRead': True } } result = service.users().settings().filters().create(userId='me', body=filter).execute() print(result)
-
Set up an auto-reply
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # Replace with your token.json path creds = Credentials.from_authorized_user_file('token.json', SCOPES) service = build('gmail', 'v1', credentials=creds) # Set up auto-reply vacation_settings = { 'enableAutoReply': True, 'responseSubject': '', 'responseBodyPlainText': 'k', 'restrictToContacts': False, 'restrictToDomain': False, } result = service.users().settings().updateVacation(userId='me', body=vacation_settings).execute() print(result)
These scripts provide a starting point for setting up your desired email automation programmatically. Make sure to replace placeholders with your actual values and handle the authentication process according to your needs.