Automate Email Sending with Python and Tkinter

In the fast-paced world of automation, streamlining repetitive tasks can save a lot of time and effort. Today, I’ll guide you through creating a simple yet powerful email automation tool using Python, Tkinter, and PyAutoGUI. This tool will allow you to send predefined emails with just a few clicks. Let’s dive into the details!

Introduction

Hello! I’m Asif Khan, and I specialize in developing efficient automation solutions. In this blog post, I will demonstrate how to build an email automation tool using Python. This tool will help you automate sending emails through Microsoft Outlook with predefined templates, subjects, and recipients. Whether you’re managing customer communications, handling internal office tasks, or just looking to automate routine emails, this guide will equip you with a useful tool for your daily tasks.

Prerequisites

Before we start, ensure you have the following installed:

  • Python (preferably 3.7 or newer)
  • Tkinter (comes pre-installed with Python)
  • PyAutoGUI (install via pip install pyautogui)

Code Walkthrough

Here’s the complete code for the email automation tool:

import tkinter as tk
from tkinter import messagebox, ttk
import pyautogui
import time

# Function to perform automation
def send_email_automation():
    email = email_combobox.get()  # Get selected email from combobox
    subject = subject_combobox.get()  # Get selected subject from combobox
    message = message_combobox.get()  # Get selected message from combobox

    if not email or not subject or not message:
        messagebox.showerror("Error", "Please fill in all fields.")
        return

    # Update status message
    status_label.config(text="Sending email...", fg="blue")

    # Delay to switch to the correct window
    time.sleep(2)

    # Perform automation steps
    pyautogui.moveTo(108, 217)
    pyautogui.click()
    pyautogui.moveTo(883, 290)
    pyautogui.click()
    pyautogui.write(email, interval=0)
    pyautogui.moveTo(1038, 405)
    pyautogui.click()
    pyautogui.write(subject, interval=0)
    pyautogui.press('tab')
    pyautogui.press('tab')
    pyautogui.write(message, interval=0)
    pyautogui.moveTo(71, 315)
    pyautogui.click()
    pyautogui.moveTo(1321, 550)
    pyautogui.click()
    pyautogui.moveTo(1157, 595)
    pyautogui.click()
    pyautogui.moveTo(1154, 619)
    pyautogui.click()
    pyautogui.click()

    # Update status message
    status_label.config(text="Email Automation completed successfully!", fg="green")
    messagebox.showinfo("Success", "Email Automation completed successfully!")

# Create the main window
root = tk.Tk()
root.title("Email Automation")
root.geometry("750x600")  # Set a larger window size
root.configure(bg="#f0f0f0")  # Light background color

# Create a heading label at the top
heading_label = tk.Label(root, text="Microsoft Outlook App Automation", font=("Segoe UI", 20, "bold"), bg="#f0f0f0", fg="#333333")
heading_label.pack(pady=20)

# Create a frame for the input fields and layout
frame = tk.Frame(root, bg="#ffffff", padx=20, pady=20, relief="groove", borderwidth=2)
frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)

# Create and place labels and entry fields with styling
tk.Label(frame, text="Email:", font=("Segoe UI", 14), bg="#ffffff").grid(row=0, column=0, sticky="w", pady=10)

# Add Combobox for selecting an email
email_options = ["example1@gmail.com", "example2@gmail.com", "example3@gmail.com"]
email_combobox = ttk.Combobox(frame, values=email_options, font=("Segoe UI", 14), width=60)
email_combobox.grid(row=0, column=1, pady=10)
email_combobox.set("Select an email")  # Default text

tk.Label(frame, text="Subject:", font=("Segoe UI", 14), bg="#ffffff").grid(row=1, column=0, sticky="w", pady=10)

# Add Combobox for selecting a subject
subject_options = [
    "Airway Bill and Pickup Schedule",
    "Fix Visitor Registering Machine",
    "Requested Documents",
    "Meeting Reminder"
]
subject_combobox = ttk.Combobox(frame, values=subject_options, font=("Segoe UI", 14), width=60)
subject_combobox.grid(row=1, column=1, pady=10)
subject_combobox.set("Select a subject")  # Default text

tk.Label(frame, text="Message:", font=("Segoe UI", 14), bg="#ffffff").grid(row=2, column=0, sticky="nw", pady=10)

# Add Combobox for selecting a formatted message
message_options = [
    """
Dear Sir,

I have created the airway bill, and DHL is scheduled to arrive today at 2:00 PM for the pickup.

Kind regards,

Asif Khan
Receptionist
""",
    """
Hi Rajnees,

Could you please help fix our visitor registering machine? It’s not working properly. Let me know when you can take a look.


Thanks,
Asif Khan
""",
    """
Dear Madam,

Please find attached the requested documents for your review. If you need further assistance, feel free to reach out.

Best regards,

Asif Khan
Receptionist
""",
    """
Dear Team,

Just a reminder that we have a meeting scheduled for 10:00 AM tomorrow. Please ensure to attend on time.

Thank you,

Asif Khan
Receptionist
"""
]

message_combobox = ttk.Combobox(frame, values=message_options, font=("Segoe UI", 14), width=60, height=6)
message_combobox.grid(row=2, column=1, pady=10)
message_combobox.set("Select a message")  # Default text

# Create and place a button to start automation
send_button = tk.Button(frame, text="Send", command=send_email_automation, font=("Segoe UI", 14), bg="#4CAF50", fg="white", relief="raised", padx=20, pady=10)
send_button.grid(row=3, column=1, sticky="e", pady=20)

# Create and place a status label
status_label = tk.Label(root, text="", font=("Segoe UI", 14), bg="#f0f0f0")
status_label.pack(pady=10)

# Run the application
root.mainloop()
         

Explanation

  1. Import Libraries: We use tkinter for the GUI, pyautogui for automating mouse and keyboard actions, and time for adding delays.
  2. Create GUI: We set up a Tkinter window with labels, comboboxes, and a button to input email details and trigger the automation.
  3. Automation Function: The send_email_automation function uses PyAutoGUI to interact with the Microsoft Outlook application, filling out the email fields and sending the email.
  4. Status Updates: The GUI updates the status label to inform the user about the progress of the automation process.

Conclusion

By following this guide, you can automate the process of sending emails using Python, Tkinter, and PyAutoGUI. This script is a starting point and can be further customized to suit your specific needs. Feel free to experiment with different email clients and refine the automation process!

If you have any questions or suggestions, please leave a comment below. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *


Shopping Basket
Scroll to Top