Skip to content

2. The signin() function

Vismaya Atreya edited this page Aug 11, 2020 · 7 revisions

Now that we have imported the modules, let us start off with the functions. This function, signin(), is a function you will create. It won't sign you in with your username and password yet, but it will when you send the email. We will take a better look at this later, but for now, we need to show a new window for the person to send an email.

def signin():
    global sender_email
    global password
    sender_email = username_entry.get()
    password = password_entry.get()
    global smtp_server
    global port
    server = server_name.get()
    if (server == 'Gmail'):
        smtp_server = 'smtp.gmail.com'
        port = 587
    elif (server == 'Outlook'):
        smtp_server = 'smtp.outlook.com'
        port = 587
    elif (server == 'Office 365'):
        smtp_server = 'smtp.office365.com'
        port = 587
    elif (server == 'Yahoo! Mail'):
        smtp_server = 'smtp.mail.yahoo.com'
        port = 465
    elif (server == 'Yahoo! Mail Plus'):
        smtp_server = 'plus.smtp.mail.yahoo.com'
        port = 465
    else:
        smtp_server = 'imap.mail.me.com'
        port = 993
    login.destroy()
    mail_window = tk.Tk()
    mail_window.title('Send Mail')
    global subject_box
    global message_box
    global receiver_email_entry
    reciever_label = tk.Label(mail_window, text = 'Recipients: (Separate with spaces)')
    receiver_email_entry = tk.Entry(mail_window)
    message_label = tk.Label(mail_window, text = 'Message')
    message_box = ScrolledText(mail_window)
    subject_label = tk.Label(mail_window, text = 'Subject:')
    subject_box = tk.Entry(mail_window)
    send_button = tk.Button(mail_window, text = 'Send Email', command = sendmail)
    reciever_label.pack()
    receiver_email_entry.pack()
    subject_label.pack()
    subject_box.pack()
    message_label.pack()
    message_box.pack()
    send_button.pack()
    mail_window.mainloop()

Let's break that down into a few pieces.

def signin():
    global sender_email
    global password
    sender_email = username_entry.get()
    password = password_entry.get()
    global smtp_server
    global port

Now, we have to make the username and password available to another function, so we have to use the global keyword. In the last few lines of the final code, two entries for the username and password will be created. They will be called username_entry and password _entry respectively. To retrieve the text present in an entry, we use the .get() attribute. I used that and saved the username and password as two variables, sender_email and password. Later, in this section, we will use two variables called smtp_server and port to connect to the server, and it will be used in another function too, so you need to make it a global variable.

    server = server_name.get()
    if (server == 'Gmail'):
        smtp_server = 'smtp.gmail.com'
        port = 587
    elif (server == 'Outlook'):
        smtp_server = 'smtp.outlook.com'
        port = 587
    elif (server == 'Office 365'):
        smtp_server = 'smtp.office365.com'
        port = 587
    elif (server == 'Yahoo! Mail'):
        smtp_server = 'smtp.mail.yahoo.com'
        port = 465
    elif (server == 'Yahoo! Mail Plus'):
        smtp_server = 'plus.smtp.mail.yahoo.com'
        port = 465
    else:
        smtp_server = 'imap.mail.me.com'
        port = 993

This may seem hard too, but it simple. server_name is a tkinter StringVar that contains the SMTP server selected form a tkinter Dropdown-menu (OptionMenu), and the .get() attribute of it is being used to retrieve the string selected and that string is being svaed in a local variable called server. If the variable server is equal to 'Gmail', then the global variable smtp_server will be set to smtp.gmail.com and the global variable port will be set to 587. The same has been done for Outlook, Office 365, Yahoo! Mail and Yahoo Mail Plus. Since the only one left out would be iCloud, the else statement is used for it. This is not only a learning about python, but also shows you what happens in the background of any mail app.

    login.destroy()
    mail_window = tk.Tk()
    mail_window.title('Send Mail')
    global subject_box
    global message_box
    global receiver_email_entry
    reciever_label = tk.Label(mail_window, text = 'Recipients: (Separate with spaces)')
    receiver_email_entry = tk.Entry(mail_window)
    message_label = tk.Label(mail_window, text = 'Message')
    message_box = ScrolledText(mail_window)
    subject_label = tk.Label(mail_window, text = 'Subject:')
    subject_box = tk.Entry(mail_window)
    send_button = tk.Button(mail_window, text = 'Send Email', command = sendmail)
    reciever_label.pack()
    receiver_email_entry.pack()
    subject_label.pack()
    subject_box.pack()
    message_label.pack()
    message_box.pack()
    send_button.pack()
    mail_window.mainloop()

In the end, we will create a login window too, so we will have to close that window when the user clicks on the login button (It will be created later). You can close a window by using its .destroy() attribute.

To create the window where the user can edit the email, you need to create a new window, in this case it is called mail_window at it is set to tk.Tk(). The .Tk() attribute of tkinter creates a window, but it is not shown yet.

The third line here, with the .title() attribute changes the window's title to 'Send Mail'. The variables for the subject entry, message box and the receiver entry have been made global in the fourth, fifth and sixth line.

In the seventh line, a tkinter label is created. A label is a tkinter widget that shows text. The first argument there is mail_window, since it is where this label will be shown. The second argument is the text to be shown. It is only saved in a variable called receiver_label, so it is not shown yet.

The line after that is a tkinter entry. Entries are single-line text boxes that you can type in.

Another label is created in the ninth line for the text box that follows. The tenth line creates a tkinter ScrolledText widget, which is a multiple-line text box that you can type in.

In the next two lines, a label and an entry is created. In the thirteenth line, a tkinter button is created. Here, the third argument is the command that should be executed when the button is clicked. The function here, sendmail will be defined later.

In the next six lines, the widgets (Labels, Entries, Text box and Button) are are shown in the window using the .pack() attribute of all tkinter widgets.

Since the window hasn't been shown yet, the .mainloop() attribute is used with the window.

Move over to the next section

Clone this wiki locally