Python Forum
How can a user send a message via Contact Form in tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: How can a user send a message via Contact Form in tkinter (/thread-25703.html)



How can a user send a message via Contact Form in tkinter - karolp - Apr-08-2020

I am trying to create a GUI app in Tkinter that allows user to send an email to me with a query. This will be done via the contact form. All my code to date (for the actual app) is done using Tkinter. I am NOT asking for explaining me how to put a window and widgets in Tkinter. I am asking how to incorporate smptlib module in my case. I would like user to send a query without authorisation like passwords etc.
In my code below the sender and receiver will be the same email address (my email) which is not ideal. I can request the user to input his contact details in the message format but would prefer that the email I receive is linked to the user email address so I can simple reply to his query.

Hope somebody can advise?

import smtplib
import tkinter as tk
from tkinter import ttk
 
class Contact():

    def __init__(self, the_window):

        self.the_window = the_window
        self.the_window.title("Contact Form")
        self.the_window.geometry("500x400")
        self.the_window.attributes('-topmost',True)

        self.sender_box = tk.StringVar()
        self.name_sender = tk.StringVar()

        self.create_widgets()

    def btn_clicked(self):

        sender = self.sender_box_entry.get()
        message = self.mess_box_entry.get('1.0', 'end')
        receiver = "[email protected]"
        login = "[email protected]"
        password = "xxxxxx"


        try:
            server = smtplib.SMTP("smtp.live.com", 25)
            server.ehlo()
            server.starttls()
            server.login(login, password)
            server.sendmail(sender, receiver, message)
            server.quit()
            statement_1 = "MAIL HAS BEEN SENT"
            return statement_1

        except:

            statement_2 = "SOMETHING WENT WRONG"
            return statement_2


    def display_mess(self, btn_clicked):
        self.display['text'] = btn_clicked()


    def create_widgets(self):

        self.sender_label = tk.Label(self.the_window, text="Sender Email")
        self.name_sender_label = tk.Label(self.the_window, text="Name")
        self.message = tk.Label(self.the_window, text="Write your query below")
        self.display = tk.Label(self.the_window)
        self.sender_box_entry = ttk.Entry(self.the_window, textvariable=self.sender_box)
        self.name_box_entry = ttk.Entry(self.the_window, textvariable=self.name_sender)
        self.mess_box_entry = tk.Text(self.the_window, width=55, height=10, wrap=tk.WORD)
        self.button_send = ttk.Button(self.the_window, text="Send", command= lambda:self.display_mess(self.btn_clicked))

        self.sender_label.grid(row=0, column=0, pady=(20,0))
        self.sender_box_entry.grid(row=0, column=1, pady=(20,0))
        self.name_sender_label.grid(row=1, column=0)
        self.name_box_entry.grid(row=1, column=1)
        self.message.grid(row=3,column=0, columnspan=2, pady=(20,0))
        self.mess_box_entry.grid(row=4,column=0, columnspan=2, padx=30)
        self.button_send.grid(row=5,column=1, pady=(10,0))
        self.display.grid(row=5, column=0, padx=(20,0))

new_window = tk.Tk()
application = Contact(new_window)
new_window.mainloop()