**Task:** Write a small application that will send an email with the help of SMTP.
### Requirements:
1. **Functionality**:
The program should take a file path as an argument of an Excel file `.xlsx` that contains the list of email addresses.
Send the emails with SMTP using the specified text.
- The application should allow the user to specify:
- Email subject.
- Email body.
- SMTP settings - server, port, username, password, and SSL/TLS usage.
2. **Input Data:
- An Excel file with one column containing the email addresses; for example, a column named `Email`.
- The details of the SMTP server and the email content are to be entered through the application by the user.
3. **Interface**:
- A simple form with the following:
- File picker for choosing the Excel file.
SMTP setting entry fields.
- Entry field for the subject of the email.
- Entry field for the body of the email.
- Button to trigger the sending process.
4. **Validation**:
- Validate email addresses before sending.
- In the case of errors, provide some kind of error messages whether this was because of the email address's being invalid or due to connection issues.
5. **Technologies**:
- Programming Language: Python (Recommended due to its libraries for handling Excel and SMTP).
- Libraries:
- `openpyxl` or `pandas` for reading/writing Excel files.
- `smtplib` for sending emails.
- `tkinter` or PyQt for creating the GUI.
Python Application to Send Emails using SMTP with Excel Data
- paypal56_ab6mk6y7
- Site Admin
- Posts: 72
- Joined: Sat Oct 26, 2024 3:05 pm
- paypal56_ab6mk6y7
- Site Admin
- Posts: 72
- Joined: Sat Oct 26, 2024 3:05 pm
Re: Python Application to Send Emails using SMTP with Excel Data
Code: Select all
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
# Function to send emails
def send_emails():
try:
# Get input values from the user
excel_file = file_path.get()
smtp_server = smtp_server_entry.get()
smtp_port = int(smtp_port_entry.get())
username = username_entry.get()
password = password_entry.get()
use_ssl = ssl_var.get()
email_subject = subject_entry.get()
email_body = body_text.get("1.0", tk.END)
# Load email addresses from Excel
df = pd.read_excel(excel_file)
if 'Email' not in df.columns:
raise ValueError("The Excel file must contain a column named 'Email'.")
email_list = df['Email'].dropna().tolist()
# Initialize SMTP connection
if use_ssl:
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
else:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(username, password)
# Send email to each address
for email in email_list:
if not email:
continue
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = email
msg['Subject'] = email_subject
msg.attach(MIMEText(email_body, 'plain'))
server.sendmail(username, email, msg.as_string())
server.quit()
messagebox.showinfo("Success", "Emails sent successfully!")
except Exception as e:
messagebox.showerror("Error", str(e))
# Function to browse and select the Excel file
def browse_file():
file = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])
if file:
file_path.set(file)
# GUI setup
root = tk.Tk()
root.title("Email Sender")
root.geometry("500x600")
file_path = tk.StringVar()
ssl_var = tk.BooleanVar()
# File picker
tk.Label(root, text="Excel File (.xlsx):").pack(anchor=tk.W, padx=10, pady=5)
tk.Entry(root, textvariable=file_path, width=50).pack(anchor=tk.W, padx=10, pady=5)
tk.Button(root, text="Browse", command=browse_file).pack(anchor=tk.W, padx=10, pady=5)
# SMTP settings
tk.Label(root, text="SMTP Server:").pack(anchor=tk.W, padx=10, pady=5)
smtp_server_entry = tk.Entry(root, width=50)
smtp_server_entry.pack(anchor=tk.W, padx=10, pady=5)
tk.Label(root, text="SMTP Port:").pack(anchor=tk.W, padx=10, pady=5)
smtp_port_entry = tk.Entry(root, width=50)
smtp_port_entry.pack(anchor=tk.W, padx=10, pady=5)
tk.Label(root, text="Username:").pack(anchor=tk.W, padx=10, pady=5)
username_entry = tk.Entry(root, width=50)
username_entry.pack(anchor=tk.W, padx=10, pady=5)
tk.Label(root, text="Password:").pack(anchor=tk.W, padx=10, pady=5)
password_entry = tk.Entry(root, show="*", width=50)
password_entry.pack(anchor=tk.W, padx=10, pady=5)
tk.Checkbutton(root, text="Use SSL", variable=ssl_var).pack(anchor=tk.W, padx=10, pady=5)
# Email content
tk.Label(root, text="Email Subject:").pack(anchor=tk.W, padx=10, pady=5)
subject_entry = tk.Entry(root, width=50)
subject_entry.pack(anchor=tk.W, padx=10, pady=5)
tk.Label(root, text="Email Body:").pack(anchor=tk.W, padx=10, pady=5)
body_text = tk.Text(root, width=60, height=10)
body_text.pack(anchor=tk.W, padx=10, pady=5)
# Send button
tk.Button(root, text="Send Emails", command=send_emails, bg="green", fg="white").pack(anchor=tk.CENTER, pady=20)
root.mainloop()
Reads email addresses from an Excel file with a column labeled as Email.
Allows the user to specify SMTP settings, email subject, and body.
Validates input and correctly handles errors.
Uses tkinter for the graphical user interface.