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.

Clone this wiki locally