python|June 15, 2022|1 min read

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

TL;DR

Use Python's smtplib and email.mime modules to send HTML emails through an authenticated SMTP server with TLS encryption.

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction

This post has the complete code to send email through smtp server, with authentication.

Requirements

We need following parameters:

  • SMTP Server host address
  • SMTP Server port
  • Sender Email
  • Sender Email Credentials

from smtplib import SMTP_SSL as SMTP
from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

class EmailClient:
  def __init__(self):
    self._mail_server = '<Your-email-smtp-server>'
    self._mail_server_port = 587 # <smtp-server port number>
    self._sender = "<sender email address>"
    self._credentials = {
      "username": "<sender email address>",
      "password": "<password>"
    }

  def send_mail(self, recipients, subject, email_body):    
    msg = MIMEMultipart()
    body = MIMEText(email_body, 'html')
    msg['Subject'] = subject
    msg.attach(body)

    with SMTP(self._mail_server,port=self._mail_server_port) as conn:
      conn.login(self._credentials['username'], self._credentials['password'])

      conn.sendmail(self._sender, recipients, msg.as_string())

Related Posts

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

Implementation of Timeit function, Get performance numbers for a function

Implementation of Timeit function, Get performance numbers for a function

This is regarding the timeit implementation in python. The basic requirement…

Python: How to generate string of arbitrary length of any alphabet characters

Python: How to generate string of arbitrary length of any alphabet characters

I was testing a bug where a field was limited to 255 characters only. I needed…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…