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

How to connect Php docker container with Mongo DB docker container

How to connect Php docker container with Mongo DB docker container

Goto your command terminal. Type: This will expose port: 27017 by default. You…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Eclipse/STS IDE showing compilation errors in java code inspite of all dependencies installed

Eclipse/STS IDE showing compilation errors in java code inspite of all dependencies installed

I have a Java project and dependencies are being managed through maven. I have…

How to configure Grafana (Free version) with oAuth Okta, with SSL on Docker,Nginx and Load dashboard from json

How to configure Grafana (Free version) with oAuth Okta, with SSL on Docker,Nginx and Load dashboard from json

Introduction In this post, we will see: use Grafana Community Edition (Free…

React JS router not working on Nginx docker container

React JS router not working on Nginx docker container

Problem Statement I developed a simple ReactJS application where I have used…

Latest Posts

Serverless vs Containers — The Decision I Keep Revisiting

Serverless vs Containers — The Decision I Keep Revisiting

Every time I start a new service, I have the same argument with myself. Lambda…

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Large Language Models are powerful, but they hallucinate. They confidently make…

Prompt Engineering Patterns That Actually Work in Production

Prompt Engineering Patterns That Actually Work in Production

Most prompt engineering advice on the internet is useless in production. “Be…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…