Using PySimpleGUI and Python to Send Emails from Your Application

Posted by


In this tutorial, we will learn how to send an email from your PySimpleGUI application using Python. PySimpleGUI is a simple and easy-to-use GUI library for Python that allows developers to quickly create beautiful and interactive desktop applications. With the help of Python’s built-in smtplib library, we can send emails directly from our PySimpleGUI application.

Before we get started, make sure you have PySimpleGUI installed. If you don’t have it installed, you can install it using pip:

pip install pysimplegui

Now, let’s create a simple PySimpleGUI application that allows the user to enter the recipient’s email address, subject, and message. We will then send an email using the entered details.

Here’s the code for our PySimpleGUI application:

import PySimpleGUI as sg
import smtplib
from email.message import EmailMessage

def send_email(recipient, subject, message):
    email = EmailMessage()
    email['From'] = 'your_email@example.com'
    email['To'] = recipient
    email['Subject'] = subject
    email.set_content(message)

    with smtplib.SMTP('smtp.example.com', 587) as smtp:
        smtp.starttls()
        smtp.login('your_email@example.com', 'your_password')
        smtp.send_message(email)

layout = [
    [sg.Text('Recipient Email:'), sg.InputText(key='recipient')],
    [sg.Text('Subject:'), sg.InputText(key='subject')],
    [sg.Text('Message:'), sg.InputText(key='message')],
    [sg.Button('Send Email')]
]

window = sg.Window('Email Sender', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Send Email':
        recipient = values['recipient']
        subject = values['subject']
        message = values['message']

        send_email(recipient, subject, message)
        sg.popup('Email sent successfully!')

window.close()

Make sure to replace 'your_email@example.com', 'smtp.example.com', and 'your_password' with your email address, SMTP server address, and password, respectively.

To run the above code, save it in a Python file and run it. You should see a window pop up with input fields for the recipient’s email, subject, and message. Enter the details and click on the ‘Send Email’ button to send the email.

That’s it! You have successfully sent an email from your PySimpleGUI application using Python. Feel free to customize the application further to meet your requirements. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@aavalos163
15 days ago

Work great!. I had to work around gmail security settings and create a generated password for third party apps thru settings. Since the option to turn off was not there due to gmail upgrade their security settings. Thank you for sharing.

1
0
Would love your thoughts, please comment.x
()
x