From 9ee179697db1fad19c372ce5a6750c0b59396ea7 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 21 Nov 2019 15:49:07 -0500 Subject: [PATCH 1/2] Demo Email Send - Initial check in --- DemoPrograms/Demo_Email_Send.py | 91 +++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 DemoPrograms/Demo_Email_Send.py diff --git a/DemoPrograms/Demo_Email_Send.py b/DemoPrograms/Demo_Email_Send.py new file mode 100644 index 00000000..ab962658 --- /dev/null +++ b/DemoPrograms/Demo_Email_Send.py @@ -0,0 +1,91 @@ +import PySimpleGUI as sg + +''' + Copyright 2019 PySimpleGUI.org + Based on a send-email script originally written by by Israel Dryer +''' + +# 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 '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 + server = smtp.SMTP(host=smtp_host, port=smtp_port) + server.starttls() + server.login(user=user, password=password) + + # create the email message headers and set the payload + msg = EmailMessage() + msg['From'] = from_address + msg['To'] = to_address + msg['Subject'] = subject + msg.set_payload(message_text) + + # open the email server and send the message + server.send_message(msg) + + server.close() + +''' + important notes about using gmail + + - Gmail has locked things down pretty good with what it considers less secure apps. That + would include access your Gmail account from the smtplib library in Python. However, there + is a work around. You can enable access from "Less Secure Apps" by going to your Gmail + account and enabling that feature. However, you should do this at your own peril, and after + carefully reading the warnings: https://support.google.com/accounts/answer/6010255. + + smtplib | https://docs.python.org/3/library/smtplib.html?#module-smtplib + email.message | https://docs.python.org/3/library/email.message.html?#module-email.message + email examples in Python | https://docs.python.org/3.7/library/email.examples.html +''' + +def main(): + sg.change_look_and_feel('Dark Blue 3') + layout = [[sg.Text('Send an Email', font='Default 18')], + [sg.T('From:', size=(8,1)), sg.Input(key='-EMAIL FROM-')], + [sg.T('To:', size=(8,1)), sg.Input(key='-EMAIL TO-')], + [sg.T('Subject:', size=(8,1)), sg.Input(key='-EMAIL SUBJECT-')], + [sg.T('Mail login information', font='Default 18')], + [sg.T('User:', size=(8,1)), sg.Input(key='-USER-')], + [sg.T('Password:', size=(8,1)), sg.Input(password_char='*', key='-PASSWORD-')], + [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 + event, values = window.read() + if event in (None, 'Exit'): + break + if event == 'Send': + 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-']) + + window.close() + +main() \ No newline at end of file From 559bb227979aaace5898bb9e124305101e6b2c69 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 21 Nov 2019 16:37:44 -0500 Subject: [PATCH 2/2] Made work for all 4 ports of PySimpleGUI! --- DemoPrograms/Demo_Email_Send.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/DemoPrograms/Demo_Email_Send.py b/DemoPrograms/Demo_Email_Send.py index ab962658..b41bbcc5 100644 --- a/DemoPrograms/Demo_Email_Send.py +++ b/DemoPrograms/Demo_Email_Send.py @@ -1,4 +1,7 @@ import PySimpleGUI as sg +# import PySimpleGUIWeb as sg +# import PySimpleGUIWx as sg +# import PySimpleGUIQt as sg ''' Copyright 2019 PySimpleGUI.org @@ -22,8 +25,6 @@ def send_an_email(from_address, to_address, subject, message_text, user, passwor smtp_host, smtp_port = google_smtp_server elif 'hotmail' in user or 'live' in user: smtp_host, smtp_port = microsoft_smtp_server - elif 'live' in user: - smtp_host, smtp_port = microsoft_smtp_server elif 'yahoo' in user: smtp_host, smtp_port = yahoo_smtp_server else: @@ -62,12 +63,12 @@ def send_an_email(from_address, to_address, subject, message_text, user, passwor def main(): sg.change_look_and_feel('Dark Blue 3') layout = [[sg.Text('Send an Email', font='Default 18')], - [sg.T('From:', size=(8,1)), sg.Input(key='-EMAIL FROM-')], - [sg.T('To:', size=(8,1)), sg.Input(key='-EMAIL TO-')], - [sg.T('Subject:', size=(8,1)), sg.Input(key='-EMAIL SUBJECT-')], + [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-')], - [sg.T('Password:', size=(8,1)), sg.Input(password_char='*', key='-PASSWORD-')], + [sg.T('User:', size=(8,1)), sg.Input(key='-USER-', size=(35,1))], + [sg.T('Password:', size=(8,1)), sg.Input(password_char='*', key='-PASSWORD-', size=(35,1))], [sg.Multiline('Type your message here', size=(60,10), key='-EMAIL TEXT-')], [sg.Button('Send'), sg.Button('Exit')]] @@ -78,7 +79,8 @@ def main(): if event in (None, 'Exit'): break if event == 'Send': - sg.popup_quick_message('Sending your message... this will take a moment...', background_color='red') + if sg.__name__ != 'PySimpleGUIWeb': # auto close popups not yet supported in PySimpleGUIWeb + 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-'],