From 29ef2b4b2552eb63d3a46c71a51989e9d167c4d2 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 30 Jul 2022 15:59:25 -0400 Subject: [PATCH] Update of the email send Demo Program --- DemoPrograms/Demo_Email_Send.py | 82 ++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/DemoPrograms/Demo_Email_Send.py b/DemoPrograms/Demo_Email_Send.py index 9d443f3a..70515de7 100644 --- a/DemoPrograms/Demo_Email_Send.py +++ b/DemoPrograms/Demo_Email_Send.py @@ -1,38 +1,36 @@ import PySimpleGUI as sg -# import PySimpleGUIWeb as sg -# import PySimpleGUIWx as sg -# import PySimpleGUIQt as sg ''' - Copyright 2019 PySimpleGUI.org + Learn how to send emails from PySimpleGUI using the smtplib and email modules + + The GUI portion is simple + Based on a send-email script originally written by by Israel Dryer + (Thank you Israel for figuring out the hard part of the stmp and email module calls!) + + Copyright 2019, 2022 PySimpleGUI + ''' +# If you are using a mail service that's not gmail, hotmail, live or yahoo: +# then you can enter the smtp server address here so you don't have to keep typing it into the GUI +smtp_host_default = '' + # used for sending the email import smtplib as smtp # used to build the email from email.message import EmailMessage # create and send email -def send_an_email(from_address, to_address, subject, message_text, user, password): - # SMTP Servers for popular free services... add your own if needed. Format is: address, port - google_smtp_server = 'smtp.gmail.com', 587 - microsoft_smtp_server = 'smtp.office365.com', 587 - yahoo_smtp_server = 'smtp.mail.yahoo.com', 587 # or port 465 - - # open the email server connection - if 'gmail' in user: - smtp_host, smtp_port = google_smtp_server - elif 'hotmail' in user or 'live' in user: - smtp_host, smtp_port = microsoft_smtp_server - elif 'yahoo' in user: - smtp_host, smtp_port = yahoo_smtp_server - else: - sg.popup('Username does not contain a supported email provider') - return +def send_an_email(from_address, to_address, subject, message_text, user, password, smtp_host, smtp_port): server = smtp.SMTP(host=smtp_host, port=smtp_port) server.starttls() - server.login(user=user, password=password) + try: + server.login(user=user, password=password) + except Exception as e: + sg.popup_error('Error authenticaing your email credentials', e, image=sg.EMOJI_BASE64_WEARY) + server.close() + return # create the email message headers and set the payload msg = EmailMessage() @@ -42,9 +40,15 @@ def send_an_email(from_address, to_address, subject, message_text, user, passwor msg.set_payload(message_text) # open the email server and send the message - server.send_message(msg) + try: + server.send_message(msg) + except Exception as e: + sg.popup_error('Error sending your email', e, image=sg.EMOJI_BASE64_WEARY) + server.close() + return server.close() + sg.popup('Email sent successfully!', image=sg.EMOJI_BASE64_HAPPY_JOY) ''' important notes about using gmail @@ -61,33 +65,47 @@ def send_an_email(from_address, to_address, subject, message_text, user, passwor ''' def main(): + smtp_server_dict = {'gmail.com':'smtp.gmail.com','hotmail.com':'smtp.office365.com', 'live.com': 'smtp.office365.com', 'yahoo.com':'smtp.mail.yahoo.com'} + sg.theme('Dark Blue 3') layout = [[sg.Text('Send an Email', font='Default 18')], [sg.T('From:', size=(8,1)), sg.Input(key='-EMAIL FROM-', size=(35,1))], [sg.T('To:', size=(8,1)), sg.Input(key='-EMAIL TO-', size=(35,1))], [sg.T('Subject:', size=(8,1)), sg.Input(key='-EMAIL SUBJECT-', size=(35,1))], [sg.T('Mail login information', font='Default 18')], - [sg.T('User:', size=(8,1)), sg.Input(key='-USER-', size=(35,1))], + [sg.T('User:', size=(8,1)), sg.Input(key='-USER-', size=(35,1), enable_events=True)], [sg.T('Password:', size=(8,1)), sg.Input(password_char='*', key='-PASSWORD-', size=(35,1))], + [sg.T('SMTP Server Info', font='_ 14')], + [sg.T('SMTP Hostname'), sg.Input(smtp_host_default, s=20, key='-SMTP HOST-'), sg.T('SMTP Port'), sg.In(587, s=4, key='-SMTP PORT-') ], [sg.Multiline('Type your message here', size=(60,10), key='-EMAIL TEXT-')], [sg.Button('Send'), sg.Button('Exit')]] window = sg.Window('Send An Email', layout) - while True: # Event Loop + while True: event, values = window.read() if event in (sg.WIN_CLOSED, 'Exit'): break if event == 'Send': - if sg.__name__ != 'PySimpleGUIWeb': # auto close popups not yet supported in PySimpleGUIWeb + if values['-SMTP HOST-']: sg.popup_quick_message('Sending your message... this will take a moment...', background_color='red') - send_an_email(from_address=values['-EMAIL FROM-'], - to_address=values['-EMAIL TO-'], - subject=values['-EMAIL SUBJECT-'], - message_text=values['-EMAIL TEXT-'], - user=values['-USER-'], - password=values['-PASSWORD-']) + send_an_email(from_address=values['-EMAIL FROM-'], + to_address=values['-EMAIL TO-'], + subject=values['-EMAIL SUBJECT-'], + message_text=values['-EMAIL TEXT-'], + user=values['-USER-'], + password=values['-PASSWORD-'], + smtp_host=values['-SMTP HOST-'], + smtp_port = values['-SMTP PORT-']) + else: + sg.popup_error('Missing SMTP Hostname... you have to supply a hostname (gmail, hotmail, live, yahoo are autofilled)') + elif event == '-USER-': # as the email sender is typed in, try to fill in the smtp hostname automatically + for service in smtp_server_dict.keys(): + if service in values[event].lower(): + window['-SMTP HOST-'].update(smtp_server_dict[service]) + break window.close() -main() \ No newline at end of file +if __name__ == '__main__': + main() \ No newline at end of file