Sending an email with Python

In this tutorial, you will learn how to send an email with python and how to use environment variables to protect your personal information.

This tutorial is divided into two main parts. Firstly, I will show you how to create an environment variable to access your Gmail account. Secondly, I will present the Python script that you use to send an email.

Resources Required

This script will require “smtplib”, “os” and “email” libraries.

Environment Credentials

You will be sending an email and to do this you will need to use your email credentials. It is good practice not to show your personal information on the code you are doing. Especially when you are working with other persons. I guarantee you; you will not want to give everyone access to all your personal information. In the following steps, you will learn to save your passwords in environment variables for Windows users.

Open “Environment Variables” window

You can do it by pressing the windows key and typing “environment variables”. It should be the first option that appears, like in the following image.

Another way to access the “Environment Variables” window is by accessing “Control Panel” window, then select the “System” option, followed by “Advanced system settings” on the left side and finally “Environment Variables”.

Create the Environment Variables

You need to create two variables—one for your email address and another for your email password.

To add the email address, click on “New” and add your information like in the image below. You will type the variable name you want to use on your Python code in the “Variable name” parameter. In the “Variable value” entry, you put your email address.

Now you do the same for the password. I recommend generating an app password. If you are using Gmail, then you will need to activate the 2-Step verification and access App passwords. You will see something similar to the next Figure.

Fill in the fields as shown, and then click on the “GENERATE” button. Then copy the generated password. You should paste it in the “Variable value” entry of the new environment variable you are creating for the password. Just like in the following image.

It is done! You have created two environment variables! One for the email address and another for the password. Congratulations! Now let’s send an email!

Send an Email

Simple Mail Transfer Protocol (SMTP) allows you to send an email from the client to an email server.

In the next section of this tutorial, you can see that to get the values of the environment variables the following function was used: os.environ.get()

To get the information that you want, between the parenthesis, you will put the “Variable name” that you give to each environment variable.

Since we are using SSL, the “port” variable, in this case, was defined as 465.

The “smtp_server” variable can be different, depending on your email provider. In this case, “smtp.gmail.com” was used because I am a Gmail user. Below you can find a table with some other options.

ProviderSMTP
Outlook/Hotmailsmtp.live.com
Yahoosmtp.mail.yahoo.com
Gmailsmtp.gmail.com

The “receiver_email” variable will be a string to whom you want to send the email.

The variables “subject” and “body” will contain the information about the subject and the body of the email you are sending.

Code

With the following code, you can send an email with a subject and a body from any email address that you have access to the credentials to another destinatary email address.

import os, smtplib
from email.message import EmailMessage

def email_send(smtp_server, port, sender_email, password, receiver_email, subject, body):
    message = EmailMessage()
    message['From'] = sender_email
    message['To'] = receiver_email
    message['Subject'] = subject
    message.set_content(body)
    
    with smtplib.SMTP_SSL(smtp_server, port) as smtp:
        smtp.login(sender_email, password)
        smtp.send_message(message)

if __name__ == '__main__':
    port = 465
    smtp_server = "smtp.gmail.com"
    #to get environment variable values defined previously
    sender_email = os.environ.get('mail_user')
    password = os.environ.get('mail_pass')
    receiver_email = "geekering@hotmail.com"
    subject = "E-mail test"
    body= "Hello world."

    email_send(smtp_server, port, sender_email, password, receiver_email, subject, body)

You can import the previous code to your file using the following lines:

from (The name that you give to the file with the previous code) import email_send

email_send(smtp_server, port, sender_email, password, receiver_email, subject, body)

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock