Certified Jenkins Engineer
Automation and Security
Demo Email Extention Notification Pipeline
Learn how to leverage the Email Extension Plugin to send customizable email notifications from a Jenkins Declarative Pipeline.
Overview
In this guide, you'll:
- Create a Jenkins Pipeline job
- Generate an
emailext
snippet with the Pipeline Syntax Generator - Configure default email variables
- Build a complete Declarative Pipeline with post-build email steps
- Send both plain-text and HTML-formatted notifications
Prerequisites
- Jenkins (2.x or later) with Pipeline and Email Extension Plugin installed
- SMTP server credentials configured under Manage Jenkins > Configure System
Note
Ensure the Email Extension Plugin is installed and up to date. Without it, the emailext
step won’t be available in your pipeline.
1. Create the Pipeline Job
- In Jenkins, click New Item.
- Enter a job name (e.g.,
pipeline-email-notification
). - Select Pipeline and click OK.
- Scroll to the Pipeline section and choose Declarative Pipeline as the definition.
2. Generate the emailext
Snippet
- Within the job configuration, click Pipeline Syntax (the Declarative Pipeline Generator).
- Choose Declarative: Pipeline Generator.
- In Steps, filter for
emailext
.
- Fill in To, Subject, Body, etc., then click Generate Pipeline Script:
emailext body: '', recipientProviders: [developers()], subject: ''
3. Configure Default Placeholders
The plugin provides built-in variables you can reference:
Placeholder | Description |
---|---|
$DEFAULT_RECIPIENTS | Recipients defined in Manage Jenkins |
$DEFAULT_SUBJECT | Default subject line |
$DEFAULT_CONTENT | Default email body content |
You can also attach logs or artifacts. For example:
emailext attachLog: true,
body: '$DEFAULT_CONTENT',
subject: '$DEFAULT_SUBJECT',
to: '$DEFAULT_RECIPIENTS'
4. Full Declarative Pipeline Example
This pipeline runs a Hello stage, generates testFile.txt
, and always sends an email with the build log and testFile.*
attachments:
pipeline {
agent any
stages {
stage('Hello') {
steps {
sh "echo 'Testing pipeline-email-notification' > testFile.txt"
}
}
}
post {
always {
emailext attachLog: true,
body: '$DEFAULT_CONTENT',
subject: '$DEFAULT_SUBJECT',
to: '$DEFAULT_RECIPIENTS',
attachmentsPattern: 'testFile.*'
}
}
}
Alternate Configuration
To only send emails on failures, place emailext
under post { failure { ... } }
instead of always
.
5. Execute the Pipeline
- Save your job and click Build Now.
- Observe the Hello stage run and the post-build action trigger.
6. Verify the Email Delivery
Check your inbox for a new message. You should see:
- An attached build.log
- The
testFile.txt
generated by the Hello stage
Sample console output:
Started by user siddharth
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline-email-notification
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] sh
+ echo Testing pipeline-email-notification
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] emailext
Sending email to: [email protected]
7. Sending HTML-Formatted Emails
To use rich HTML templates, create an HTML file (e.g., email-template.html
):
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jenkins Build Status</title>
</head>
<body>
<h1 style="color: #007bff;">Jenkins Build Status Notification</h1>
<p>Hi team,</p>
<p>Latest build of <b>${JOB_NAME}</b>:</p>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>Build #</th>
<th>Status</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>${BUILD_NUMBER}</td>
<td>${BUILD_STATUS}</td>
<td><a href="${BUILD_URL}">View Log</a></td>
</tr>
</tbody>
</table>
<p>Regards,<br>Your Jenkins Bot</p>
</body>
</html>
- Go to Manage Jenkins > Configure System.
- Under Extended E-mail Notification, set Default Content to your HTML and Content Type to
text/html
.
Re-run the pipeline to see your styled, HTML-based email in action.
References
Watch Video
Watch video content
Practice Lab
Practice lab