Send an email using Python

In Python, we can use the smtplib standard module to create a SMTP session that can be used to send an email.

The simplest way to do that is by sending a plain text, where the Subject and the body are storage in the same string object (see message varible in the the script below).


Here is a small script that makes this mini task of sending an email possible.


import smtplib

if __name__=="__main__":
    sender_email = ""
    password = "YOUR-PASSWORD"
    receiver_email = ""
    message = """Subject: Hi there

    This message is sent from Python."""

    # set up the SMTP server
    s = smtplib.SMTP(host="", port=587)
    s.login(sender_email, password)

    # send the email
    s.sendmail(sender_email, receiver_email, message)

    # close session

Host and Port

The host name and port for the creation of the SMTP object depends on the Email provider.

For instance, in Outlook (on the browser) you can get the port number and the SMTP host by going to settings > Mail > Sync email section.

Outlook settings

Send the email

Run the script and the receiver will receive it!


Email received

πŸ“Š Create a modern CV
Create a modern CV

Create a modern CV to impress recruiters