How to send an email with python

After learning the basics of Python, you may ask yourself what you should do next. Sending an email with Python is one of those tasks that help beginners level up themselves. In this tutorial, we teach you how to send an email with Python since it has many applications such as sending confirmation emails to users when they create an account, receiving email reminders from your code, sending emails to members of your organization, etc. If you rather send them manually, you must notice that this task is too time-consuming, so it is better to automate the task using Python. in this tutorial you will learn how to:

  • Setting up a secure connection using SMTP_SSL()
  • Using Python’s built-in smtplib library to send basic emails

The video

Let’s get started

To send emails with Python, we use the built-in Smtplib module which helps us use the Simple Mail Transfer Protocol (SMTP). Moreover, an SMTP server is needed for sending emails, therefore we utilize the Gmail SMTP server. You should notice that the same principles apply to other SMTP servers. To get started we should set up a Gmail account.

Sending an email is an ordinary task in today’s world. We can simply log in to our email account and send it. It’s not that hard, but for us, as a programmer, it’s not like that. We should know about fundamentals, protocols, codes, and different things. We need them to automate sending emails for companies, personal usage, and any other application.

Let me give you an example to show you the fundamental of sending an email. Imagine you want to send an email to your colleague or friend. You can simply turn on your laptop or maybe you can use your cell phone to log into your email account and send your email.

If you want to directly send the email to your colleague or your friend it’s not that easy because you do not know protocols and security. Maybe you want to send different kinds of files like videos, documents, text, and other stuff. If you want to send it directly to the computer or laptop of your friend, it’s too hard. Due to that, you need to use a server. You can send your email to a server which is called an SMTP server. SMTP stands for simple mail transfer protocol.

When you deliver the message to the server, this server finds the destination server of your recipient which can be anywhere in the world and is another SMTP server. Server one sends that message to the second server and when your friend or colleague logs into their email account they can receive your message. That is the process in which you need two different servers anywhere in the world then you can transfer your message to your friend.

At first, you need an account name and a password plus the server name and the server port. A port is like a window that you use to get into the server and then you can access your account and send your email. Now I want to dive deep into the process and show you the python code.

The code

import smtplib
import ssl
from getpass import getpass

smtp_server = 'smtp.gmail.com'
port = 465

sender_email = 'someemail@gmail.com'  # make sure to change this sample email
password = getpass(prompt='Enter your password:')
receiver_email = 'test@test.com'  # make sure to change this sample email

message = '''\
Subject: Test

This message is from me.
'''

context = ssl.create_default_context()

with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
Was this helpful?
[0]