DEV Community


Posted on

How to send E-Mails with Python

Hey everyone! Today we have a look at another automation possibility with Python, this time we are using the smtplib module. SMTP stands for Simple Mail Transfer Protocol and is the protocol for sending emails. It determines how emails should be encrypted, formatted and relayed between mail servers.

In my code we only do a few simple steps to get started.
Alt Text

First we create a SMTP object for connecting to GMail. Next we use the method smtpObj.ehlo() to establish a connection to the server. We then set up TLS Encryption by calling smtpObj.starttls(). This is very important because otherwise anyone could fetch your password.

After this we login with the password, send an Email and at the end, disconnect from the server. For using googlemail, you need an extra step which is getting an app password. Read here how you can do this:

You will then get an extra password for your Python app which you use to login with SMTP instead of your normal password. Have a nice day!

Top comments (0)