Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Sending Email Using Python and smtplib – Quick HowTo

Sending Email Using Python and smtplib – Quick HowTo
Image Courtesy : www.telegraph.co.uk

What is smtpLib

We can easily send email from python script using smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Import smtplib module

Let’s create a simple script for the same by importing smtplib module.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

Please use different methods if you are using different version of python. Eg: show below.

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Prepare Email Configurations

You may add this items directly inside the script (hardcoded) or add as a separate configuration file outside.

email_from_addr = "[email protected]"
email_to_addr = "[email protected]"
email_smtp_server = "smtp.gmail.com"
email_smtp_port = "587"
email_user = "myfromemail"
email_password = "mypassword"

#Email Subject
email_subject = "Welcome to Python Email Test"

Prepare Email Body

This is not mandatory to test but adding a better way to handle multipart and rich email content.

# HTML + CSS for body
email_body = None

email_body_header = ' '
email_body_header = email_body_header + '<html><head></head><body>'
email_body_header = email_body_header + '<style type="text/css"></style>'
email_body_header = email_body_header + '<br><p>Hello Team,<br><br>This is a test email.<br>'

email_body_content = ' '
email_body_content = email_body_content + '<H1>This is main content area</h1>'

email_body_footer = ' '
email_body_footer = email_body_footer + '<br>Thank you'
email_body_footer = email_body_footer + '<br>Support Team<br>'

email_body = str(email_body_header) + str(email_body_content) + str(email_body_footer)

Prepare Email Content

Now, we have all details of email and let’s stitch all together.

message = MIMEMultipart('alternative')
message['From'] = email_from_addr
message['To'] = email_to_addr
message['Subject'] = email_subject
body = email_body
message.attach(MIMEText(body, 'html'))

# print (email_body)

Send Email

Now with prepared data, we will create a connection and send email over that.

server = smtplib.SMTP(email_smtp_server,int(email_smtp_port))
text = message.as_string()
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_from_addr,email_to_addr,message.as_string())
server.quit()

Note : If you are using an open smtp connection (with whitelisted IP), you don’t need to do login as authentication will happen automatically (if you configured your SMTP server in such a way).

Check Email Received

Sample email received
You can access the full script from github – [python-email.py](https://github.com/ginigangadharan/scripts/blob/master/python-email.py) .

Refer SMTPLib documentation for more details.

Gineesh Madapparambath

Gineesh Madapparambath

Gineesh Madapparambath is the founder of techbeatly. He is the co-author of The Kubernetes Bible, Second Edition and the author of Ansible for Real Life Automation. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (Read more: iamgini.com)


Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

Implementing Ansible Roles

Implementing Ansible Roles

In a production environment, we will have hundreds and thousands of different types machines to handle. Hence the type of operations and …

AWS Summit Singapore 2019

AWS Summit Singapore 2019

This year AWS Summit Singapore will happen on April 10–11, 2019 at Singapore Expo Convention & Exhibition Centre. AWS is introducing a new session …

Google Cloud Global Digital Conference – Jan 24

Google Cloud Global Digital Conference – Jan 24

Google Cloud is arranging Global Digital Conference on 24th January 2019. Agenda has two tracks : Serverless – Build and ship applications faster – …

Automation

Configure Projects with Global Network in OpenShift

We will have requirements, where customer need to configure multiple projects but they want pods inside those projects to communicate each other. …

How to Fix a Failed PV in OpenShift Cluster

How to Fix a Failed PV in OpenShift Cluster

There are several cases a PV (PersistentVolume) appear as Failed in OpenShift or Kubernetes cluster. Once of the reason is wrong ClaimRef by which …

How to delete a pod with Terminating state in OpenShift or Kubernetes Cluster

How to delete a pod with Terminating state in OpenShift or Kubernetes Cluster

There might be situations where you have already deleted pods (or already removed dc aka deployment configuration) but pods are stuck in Terminating …