Lesson-20: Python Smtplib Module. Sending Python Mail

Python Smtplib Module. Sending Python Mail

In this lesson, we will see how sending mail occurs with the smtplib module that comes with python. When using this module, we will send mail via Gmail SMTP. In order to send Mail, we will need to have a gmail account and, most importantly, make some important adjustments. If we don’t make these adjustments, we’ll encounter an error code. First, let’s look at the “less secure app access” section in our gmail account.

Gmail Account Setting:

Some apps and devices use less secure sign-in technologies that make your account vulnerable to attacks. Access to these applications is already disabled by default in your Gmail account. Google recommends you do this. But in order to send mail from python, we need to turn on this feature. Google automatically turns this feature off when not in use. To open this feature, you must click on the link below to turn the feature on, as shown in the picture.

Link: https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none

Daha az güvenli uygulama erişimi

After making this adjustment, you will be able to send mail using the smtplib module of the python program. Now down to business. In this course, we will try to understand the logic of the job by sending mail with simple code structures. With the interface that we will develop in the Tkinter lessons, you can review our tkinter lessons for mail sending operations.

Click Here For Our Tkinter Lessons…

Sending mail with Smtplib:

First, we must import this module. See the following codes:

import smtplib

sunucu=smtplib.SMTP('smtp.gmail.com', 587)
sunucu.ehlo()
sunucu.starttls()
sunucu.login("google account name", "pass")
to=["send mail"]
mesaj="Try Mail Sending"
try:
    sunucu.sendmail('google account name',to,mesaj)
    print ('Mail OK....')
except:
    print ('Mail not OK!!!')

sunucu.quit()

Now let’s explain these codes in order.
server = smtplib. SMTP (‘smtp.gmail.com’, 587) we make adjustments to Gmail for sending mail with Gmail Smtp.
sunucu.ehlo() we’re checking for any problems with the connection.
sunucu.starttls() all our information will be encrypted and protected.
sunucu.login(“google account name”, “pass”) we connect to our google account. We have some points to pay attention to. In the first parameter, we only type the name part of our gmail information before the @ sign. As a second parameter, we write our gmail password in quotes.
we defined information in the to and mesaj variables. in the to variable, the e-mail must be written by the person we send the e-mail to. One of the important points is a list of to variables. We can also send emails to many people by typing more than one email in square brackets.
sunucu.sendmail(‘google account name’,to,mesaj) we send our mail with the command.
sunucu.quit() we’re shutting down the server.

In our next courses, we will design an interface through the tkinter module and send mail.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *