-
Notifications
You must be signed in to change notification settings - Fork 0
3. The sendmail() Function
Now that we created the window for the user to write the email, something should happen in the background too, when you send the email. We will have to define another function. I call this one sendmail()
def sendmail():
try:
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(sender_email, password)
message = message_box.get('1.0', 'end-1c')
receiver_email = receiver_email_entry.get().split(' ')
server.sendmail(sender_email, receiver_email, ('Subject: ' + subject_box.get() + '\n' + message) + '\n \nSent with Python smtplib and ssl modules (Project at: https://github.com/VismayaAtreya/Send-Emails-Python)' )
except Exception as e:
showinfo('Error while sending', e)
The first line starts with the def
keyword, which indicates the definition of a new function.
The second line starts a try
and except
clause. This clause tries to execute the code that comes below try:
. If an error occurred while doing so, then the code below except:
would be executed.
The third line saves a new context with secure default settings in a variable called context
The fourth line, since it is a with:
clause, refers to smtplib.SMTP(smtp_server, port)
as server
, for whatever comes below it.
The fifth line uses server.ehlo()
to identify you to the server.
The sixth line establishes the context as the context
variable.
The seventh line uses server.ehlo
again.
The eighth line uses server.login(sender_email, password)
logs you in to your account with the global variables sender_email
and password
.
The ninth line saves the text present in the global variable message_box
(It is the tkinter text widget) in a variable called message
. The .get()
attribute is used to get the text from the Text
widget too, but you need to specify from where it starts to get the text and where it ends.
The tenth line gets the text from the receiver_email_entry
entry with .get
and splits it into a list wherever it finds a whitespace. This separates the recipients' email addresses.
The eleventh line takes the first argument as the email address of the sender and the second argument as the recipients' email addresses. The third argument starts with the string 'Subject: ', since that line will be used as the subject. It is followed by the subject_box.get()
and a \n
, which is equivalent to pressing the enter key. It is followed by the message
variable, '\n'
and '\n \nSent with Python smtplib and ssl modules (Project at: https://github.com/VismayaAtreya/Send-Emails-Python)'
as a signature.
The twelfth line starts an except
clause, but here it takes the Exception (error found) as e
. The code below this clause will be executed only if there was an error while the code under the try
clause was being executed.
The thirteenth line comes under the except
clause. It uses showinfo
, a function of tkinter.messagebox
to show an alert. It shows an alert with the title 'Error, and
e` as the description. Move over to the last page.