Skip to content

Basic Email Sender

Abstract

Basic Email Sender is a Python application with a graphical user interface that allows users to send emails through Gmail’s SMTP server. The application features input validation, secure authentication using app passwords, and a clean Tkinter-based interface. This project demonstrates GUI development, email protocols, regular expressions for validation, and secure communication practices. It’s an excellent project for learning about email automation and desktop application development.

Prerequisites

  • Python 3.6 or above
  • A code editor or IDE
  • tkinter module (usually comes pre-installed)
  • smtplib module (built-in)
  • Gmail account with 2-Step Verification enabled

Before you Start

Before starting this project, you must have Python installed on your computer. If you don’t have Python installed, you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed, you can download Visual Studio Code from here.

Gmail App Password Setup

To use Gmail with this application, you need to set up an App Password:

  1. Go to your Google Account settings
  2. Click on Security
  3. Under “Signing in to Google,” select 2-Step Verification
  4. At the bottom of the page, select App passwords
  5. Enter a name that helps you remember where you’ll use the app password
  6. Select Generate
  7. Copy the 16-character code that generates on your device
  8. Use this generated password in the application (not your regular Gmail password)

For more information: Google Support - App Passwords

Getting Started

Create a Project

  1. Create a folder named email-senderemail-sender.
  2. Open the folder in your favorite code editor or IDE.
  3. Create a file named emailsender.pyemailsender.py.
  4. Copy the given code and paste it in your emailsender.pyemailsender.py file.

Write the Code

  1. Copy and paste the following code in your emailsender.pyemailsender.py file.
⚙️ Basic Email Sender
Basic Email Sender
# Basic Email Sender in Python
 
# Before the using your gmail and password follows these steps:
# 1. Go to your google account
# 2. Click on Security
# 3. Under "Signing in to Google," select 2-Step Verification.
# 4. At the bottom of the page, select App passwords.
# 5. Enter a name that helps you remember where you’ll use the app password.
# 6. Select Generate.
# 7. To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
# 8. Select Done.
# 9. Use Generated Password in the password field
# For More Infomation: https://support.google.com/mail/?p=InvalidSecondFactor
 
import tkinter as tk # pip install tk
from tkinter import *
import smtplib # pip install smtplib
from tkinter import messagebox
import re
 
 
def sendEmail():
    try:
        sender = email.get()
        rec = receiver.get()
        pas = password.get()
        msg = message.get()
        
        # Validating the Email
        if(sender == "Enter Your Email" or rec == "Enter Receiver's Email" or pas == "Enter Your Password" or msg == "Enter Your Message"):
            messagebox.showerror("Error", "Please Enter All The Fields")
            return
        
        # Validate Email Using Regular Expression
        email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")
        if not email_regex.match(sender):
            messagebox.showerror("Error", "Invalid Email")
            return
        
        if not email_regex.match(rec):
            messagebox.showerror("Error", "Invalid Receiver's Email")
            return
        
        
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender, pas)
        server.sendmail(sender, rec, msg)
        server.quit()
        messagebox.showinfo("Success", "Email Sent Successfully")
        email.delete(0, END)
        receiver.delete(0, END)
        password.delete(0, END)
        message.delete(0, END)
        
        email.insert(0, "Enter Your Email")
        receiver.insert(0, "Enter Receiver's Email")
        password.insert(0, "Enter Your Password")
        message.insert(0, "Enter Your Message")
        
    except Exception as e:
        messagebox.showerror("Error", "Something Went Wrong")
        print(e)
        
root = tk.Tk()
root.title("Email Sender")
root.geometry("700x700")
root.config(background="white")
 
label_file_explorer = Label(root, text = "Email Sender using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
 
email = Entry(root, width = 50)
email.insert(0, "Enter Your Email")
receiver = Entry(root, width = 50)
receiver.insert(0, "Enter Receiver's Email")
password = Entry(root, width = 50)
password.insert(0, "Enter Your Password")
message = Entry(root, width = 50)
message.insert(0, "Enter Your Message")
button_explore = Button(root, text = "Send Email", command = sendEmail)
exit_button = Button(root, text = "Exit", command = root.destroy)
 
label_file_explorer.grid(column = 1, row = 1)
email.grid(column = 1, row = 2)
receiver.grid(column = 1, row = 3)
password.grid(column = 1, row = 4)
message.grid(column = 1, row = 5)
button_explore.grid(column = 1, row = 6)
exit_button.grid(column = 1, row = 7)
 
root.mainloop()
 
Basic Email Sender
# Basic Email Sender in Python
 
# Before the using your gmail and password follows these steps:
# 1. Go to your google account
# 2. Click on Security
# 3. Under "Signing in to Google," select 2-Step Verification.
# 4. At the bottom of the page, select App passwords.
# 5. Enter a name that helps you remember where you’ll use the app password.
# 6. Select Generate.
# 7. To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
# 8. Select Done.
# 9. Use Generated Password in the password field
# For More Infomation: https://support.google.com/mail/?p=InvalidSecondFactor
 
import tkinter as tk # pip install tk
from tkinter import *
import smtplib # pip install smtplib
from tkinter import messagebox
import re
 
 
def sendEmail():
    try:
        sender = email.get()
        rec = receiver.get()
        pas = password.get()
        msg = message.get()
        
        # Validating the Email
        if(sender == "Enter Your Email" or rec == "Enter Receiver's Email" or pas == "Enter Your Password" or msg == "Enter Your Message"):
            messagebox.showerror("Error", "Please Enter All The Fields")
            return
        
        # Validate Email Using Regular Expression
        email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")
        if not email_regex.match(sender):
            messagebox.showerror("Error", "Invalid Email")
            return
        
        if not email_regex.match(rec):
            messagebox.showerror("Error", "Invalid Receiver's Email")
            return
        
        
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender, pas)
        server.sendmail(sender, rec, msg)
        server.quit()
        messagebox.showinfo("Success", "Email Sent Successfully")
        email.delete(0, END)
        receiver.delete(0, END)
        password.delete(0, END)
        message.delete(0, END)
        
        email.insert(0, "Enter Your Email")
        receiver.insert(0, "Enter Receiver's Email")
        password.insert(0, "Enter Your Password")
        message.insert(0, "Enter Your Message")
        
    except Exception as e:
        messagebox.showerror("Error", "Something Went Wrong")
        print(e)
        
root = tk.Tk()
root.title("Email Sender")
root.geometry("700x700")
root.config(background="white")
 
label_file_explorer = Label(root, text = "Email Sender using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
 
email = Entry(root, width = 50)
email.insert(0, "Enter Your Email")
receiver = Entry(root, width = 50)
receiver.insert(0, "Enter Receiver's Email")
password = Entry(root, width = 50)
password.insert(0, "Enter Your Password")
message = Entry(root, width = 50)
message.insert(0, "Enter Your Message")
button_explore = Button(root, text = "Send Email", command = sendEmail)
exit_button = Button(root, text = "Exit", command = root.destroy)
 
label_file_explorer.grid(column = 1, row = 1)
email.grid(column = 1, row = 2)
receiver.grid(column = 1, row = 3)
password.grid(column = 1, row = 4)
message.grid(column = 1, row = 5)
button_explore.grid(column = 1, row = 6)
exit_button.grid(column = 1, row = 7)
 
root.mainloop()
 
  1. Save the file.
  2. Open the terminal in your code editor or IDE and navigate to the folder email-senderemail-sender.
command
C:\Users\Your Name\email-sender> python emailsender.py
# A GUI window will open with:
# - Email input field
# - Receiver's email field  
# - Password field
# - Message field
# - Send Email button
# - Exit button
command
C:\Users\Your Name\email-sender> python emailsender.py
# A GUI window will open with:
# - Email input field
# - Receiver's email field  
# - Password field
# - Message field
# - Send Email button
# - Exit button

Explanation

Code Breakdown

  1. Import required modules.
emailsender.py
import tkinter as tk # pip install tk
from tkinter import *
import smtplib # pip install smtplib
from tkinter import messagebox
import re
emailsender.py
import tkinter as tk # pip install tk
from tkinter import *
import smtplib # pip install smtplib
from tkinter import messagebox
import re
  1. Define the email sending function with validation.
emailsender.py
def sendEmail():
    try:
        sender = email.get()
        rec = receiver.get()
        pas = password.get()
        msg = message.get()
        
        # Validating the Email
        if(sender == "Enter Your Email" or rec == "Enter Receiver's Email" or pas == "Enter Your Password" or msg == "Enter Your Message"):
            messagebox.showerror("Error", "Please Enter All The Fields")
            return
emailsender.py
def sendEmail():
    try:
        sender = email.get()
        rec = receiver.get()
        pas = password.get()
        msg = message.get()
        
        # Validating the Email
        if(sender == "Enter Your Email" or rec == "Enter Receiver's Email" or pas == "Enter Your Password" or msg == "Enter Your Message"):
            messagebox.showerror("Error", "Please Enter All The Fields")
            return
  1. Email validation using regular expressions.
emailsender.py
        # Validate Email Using Regular Expression
        email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")
        if not email_regex.match(sender):
            messagebox.showerror("Error", "Invalid Email")
            return
        
        if not email_regex.match(rec):
            messagebox.showerror("Error", "Invalid Receiver's Email")
            return
emailsender.py
        # Validate Email Using Regular Expression
        email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")
        if not email_regex.match(sender):
            messagebox.showerror("Error", "Invalid Email")
            return
        
        if not email_regex.match(rec):
            messagebox.showerror("Error", "Invalid Receiver's Email")
            return
  1. SMTP connection and email sending.
emailsender.py
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender, pas)
        server.sendmail(sender, rec, msg)
        server.quit()
        messagebox.showinfo("Success", "Email Sent Successfully")
emailsender.py
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender, pas)
        server.sendmail(sender, rec, msg)
        server.quit()
        messagebox.showinfo("Success", "Email Sent Successfully")
  1. GUI setup and layout.
emailsender.py
root = tk.Tk()
root.title("Email Sender")
root.geometry("700x700")
root.config(background="white")
 
label_file_explorer = Label(root, text = "Email Sender using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
 
email = Entry(root, width = 50)
email.insert(0, "Enter Your Email")
receiver = Entry(root, width = 50)
receiver.insert(0, "Enter Receiver's Email")
password = Entry(root, width = 50)
password.insert(0, "Enter Your Password")
message = Entry(root, width = 50)
message.insert(0, "Enter Your Message")
emailsender.py
root = tk.Tk()
root.title("Email Sender")
root.geometry("700x700")
root.config(background="white")
 
label_file_explorer = Label(root, text = "Email Sender using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
 
email = Entry(root, width = 50)
email.insert(0, "Enter Your Email")
receiver = Entry(root, width = 50)
receiver.insert(0, "Enter Receiver's Email")
password = Entry(root, width = 50)
password.insert(0, "Enter Your Password")
message = Entry(root, width = 50)
message.insert(0, "Enter Your Message")

Features

  • Graphical User Interface: Clean and intuitive Tkinter-based design
  • Email Validation: Regular expression-based email format checking
  • Secure Authentication: Uses Gmail’s SMTP with TLS encryption
  • Error Handling: Comprehensive error messages and exception handling
  • Input Validation: Ensures all fields are filled before sending
  • Success Feedback: Confirms successful email transmission
  • Field Reset: Automatically clears and resets fields after sending

How to Use

  1. Launch the Application: Run the Python script to open the email sender window
  2. Enter Sender Email: Input your Gmail address
  3. Enter Receiver Email: Specify the recipient’s email address
  4. Enter App Password: Use the generated Gmail app password (not your regular password)
  5. Write Message: Type your email message
  6. Send Email: Click the “Send Email” button
  7. Check Status: Success or error messages will appear in popup dialogs

Security Features

  • TLS Encryption: Uses secure TLS connection for email transmission
  • App Password Support: Works with Gmail’s 2-Step Verification
  • Input Validation: Prevents injection attacks through validation
  • Error Handling: Secure error management without exposing sensitive data

SMTP Configuration

The application uses Gmail’s SMTP server configuration:

  • Server: smtp.gmail.com
  • Port: 587 (TLS)
  • Authentication: Required
  • Encryption: STARTTLS

Common Issues and Solutions

Authentication Error

  • Problem: “Authentication failed”
  • Solution: Use App Password instead of regular Gmail password
  • Steps: Enable 2-Step Verification and generate App Password

Connection Error

  • Problem: “Connection refused”
  • Solution: Check internet connection and firewall settings
  • Alternative: Try different network or VPN

Invalid Email Format

  • Problem: “Invalid email” error
  • Solution: Ensure email follows standard format (user@domain.com)

Next Steps

You can enhance this project by:

  • Adding HTML email support for rich formatting
  • Implementing email templates and signatures
  • Adding file attachment functionality
  • Creating an email address book
  • Adding scheduling for delayed email sending
  • Implementing multiple email provider support (Yahoo, Outlook)
  • Adding email encryption and digital signatures
  • Creating batch email sending capabilities
  • Adding email tracking and read receipts
  • Implementing spam filtering and validation

Enhanced Version Ideas

emailsender.py
def enhanced_email_sender():
    # Features to add:
    # - HTML email composer
    # - File attachment browser
    # - Email templates
    # - Address book integration
    # - Email scheduling
    # - Multiple recipient support
    pass
emailsender.py
def enhanced_email_sender():
    # Features to add:
    # - HTML email composer
    # - File attachment browser
    # - Email templates
    # - Address book integration
    # - Email scheduling
    # - Multiple recipient support
    pass

Email Security Best Practices

  • Use App Passwords: Never use your main Gmail password
  • Enable 2FA: Always use two-factor authentication
  • Validate Inputs: Sanitize all user inputs
  • Secure Storage: Don’t store passwords in plain text
  • Rate Limiting: Implement sending limits to prevent spam

Educational Value

This project teaches:

  • GUI Development: Creating desktop applications with Tkinter
  • Email Protocols: Understanding SMTP and email transmission
  • Regular Expressions: Pattern matching for validation
  • Error Handling: Managing exceptions and user feedback
  • Security Practices: Implementing secure authentication

Real-World Applications

  • Automated Notifications: Send system alerts and notifications
  • Marketing Campaigns: Automated email marketing tools
  • Personal Automation: Send regular updates or reminders
  • Business Communication: Internal communication tools
  • Integration Scripts: Add email functionality to other applications

Troubleshooting Tips

  • Test with Known Emails: Start with your own email addresses
  • Check Spam Folders: Sent emails might end up in spam
  • Verify Credentials: Double-check app password generation
  • Network Issues: Ensure stable internet connection
  • Gmail Limits: Be aware of Gmail’s sending rate limits

Conclusion

In this project, we learned how to create a Basic Email Sender using Python’s Tkinter for the GUI and smtplib for email functionality. We explored secure email authentication, input validation using regular expressions, and error handling techniques. This project demonstrates the integration of multiple Python libraries to create a practical desktop application. The skills learned here are applicable to many automation and communication scenarios in software development. To find more projects like this, you can visit Python Central Hub.

Was this page helpful?

Let us know how we did