Integrating Amazon SES with Jenkins Pipeline via Python Script

Kavindra Lunuwilage
3 min readJun 6, 2023

--

In daily regression test runs, the test results are crucial for Quality Engineers to identify if there are failures in the software product. Also notifying the build status via email is essential sometimes.

Jenkins is a popular open-source automation server that can help you automate various tasks related to software development and delivery. One of the common use cases of Jenkins is to send email notifications to stakeholders when a job is completed, failed, or requires attention.

This article explains how you can set up email-sending functionality with Jenkins integrating Amazon Simple Email Service (SES). While there are multiple ways to do this, this article outlines how you can implement it using a Python script.

Prerequisites -

  1. Install Python in your Jenkins via Manage Jenkins -> Manage Plugins
  2. Valid AWS SMTP Endpoint
  3. Jenkins Pipeline

Steps -

  1. Create a .py file inside the GitHub project with a desired name and place it in a location that Jenkins can access, such as the workspace directory of the Jenkins job.
  2. Copy the below code to that file and do the modification you need.
#! /usr/bin/python
import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sender = "no-reply-aws@amazon.com"
receiverList = "<Add receiverList>"
receivers = receiverList.split(",")

msg = MIMEMultipart('alternate')
Subject = "<Add Subject>"
msg['Subject'] = Subject
msg['From'] = sender
msg['To'] = ", ".join(receivers)
html_message = f"""\
<html>
<head></head>
<body>
<p>Dear Team,<br>
<br><Add your message><br>
<br>
</p>
</body>
</html>
"""

html_body = MIMEText(html_message, 'html')
msg.attach(html_body)
smtpObj = smtplib.SMTP('<Add SMTP Endpoint>', <Add SMTP Port>)
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()

3. In the project configuration page of the Pipeline you created, go to the Pipeline section and select the Definition as Pipeline Script from the drop-down.

4. In the Command box, enter the command to run the Python script, such as `python send_email.py`. You can also use environment variables or parameters to pass dynamic values to the script, such as the job name, status, or build number.

pipeline {
agent any

stages{
stage('Checkout'){
steps{
git branch: BRANCH,
credentialsId: '<Add github credential Id>',
url: '<Add Github Repo URL>'
}
}

stage('Execute'){
steps{

sh 'chmod +x mvnw'
sh './mvnw clean test'
}
}

}

post{
success{
echo 'Build Success and Email Sending'
sh 'python3 email-configuration-success.py'
}

failure{
echo 'Build Failed and Email Sending'
sh 'python3 email-configuration-fail.py'
}
}

}

5. Save the project configuration and run the pipeline. You should receive an email from Jenkins with the details of the pipeline execution.

You can also use plugins like Email Extension Plugin or Mailer Plugin to enhance the email-sending functionality of Jenkins, such as adding more recipients, triggers, or content. However, using a Python script gives you more flexibility and control over how you want to send emails from Jenkins.

Some Useful Links -

https://blog.knoldus.com/how-to-integrate-aws-simple-email-service-with-jenkins/

--

--