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

  1. In Jenkins, click New Item.
  2. Enter a job name (e.g., pipeline-email-notification).
  3. Select Pipeline and click OK.
  4. Scroll to the Pipeline section and choose Declarative Pipeline as the definition.

2. Generate the emailext Snippet

  1. Within the job configuration, click Pipeline Syntax (the Declarative Pipeline Generator).
  2. Choose Declarative: Pipeline Generator.
  3. In Steps, filter for emailext.

The image shows a Jenkins interface with the "Snippet Generator" for configuring an "Extended Email" step in a pipeline script. It includes fields for email details like "To," "Subject," and "Body."

  1. Fill in To, Subject, Body, etc., then click Generate Pipeline Script:
emailext body: '', recipientProviders: [developers()], subject: ''

The image shows a Jenkins pipeline syntax configuration screen for setting up email notifications using the email-ext plugin. It includes fields for specifying recipients, subject, and body of the email.


3. Configure Default Placeholders

The plugin provides built-in variables you can reference:

PlaceholderDescription
$DEFAULT_RECIPIENTSRecipients defined in Manage Jenkins
$DEFAULT_SUBJECTDefault subject line
$DEFAULT_CONTENTDefault 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

  1. Save your job and click Build Now.
  2. Observe the Hello stage run and the post-build action trigger.

The image shows a Jenkins dashboard displaying the status of a pipeline named "pipeline-email-notification," with stages labeled Start, Hello, Post Actions, and End. The interface includes options for configuring and managing the pipeline.


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

The image shows a Gmail inbox with an email notification about a successful Jenkins pipeline build. The email includes two attachments: "testFile.txt" and "build.log".

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>
  1. Go to Manage Jenkins > Configure System.
  2. Under Extended E-mail Notification, set Default Content to your HTML and Content Type to text/html.

The image shows a Jenkins system configuration page for setting up email notifications, including SMTP port, credentials, and security options like SSL.

Re-run the pipeline to see your styled, HTML-based email in action.


References

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demo Email Extention Notification Freestyle