> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Email Extention Notification Pipeline

> Learn to use the Email Extension Plugin for sending customizable email notifications from a Jenkins Declarative Pipeline.

Learn how to leverage the [Email Extension Plugin](https://plugins.jenkins.io/email-ext/) 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**

<Callout icon="lightbulb" color="#1CB2FE">
  Ensure the **Email Extension Plugin** is installed and up to date. Without it, the `emailext` step won’t be available in your pipeline.
</Callout>

***

## 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`.

<Frame>
  ![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."](https://kodekloud.com/kk-media/image/upload/v1752870363/notes-assets/images/Certified-Jenkins-Engineer-Demo-Email-Extention-Notification-Pipeline/jenkins-snippet-generator-extended-email.jpg)
</Frame>

4. Fill in **To**, **Subject**, **Body**, etc., then click **Generate Pipeline Script**:

```groovy theme={null}
emailext body: '', recipientProviders: [developers()], subject: ''
```

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870365/notes-assets/images/Certified-Jenkins-Engineer-Demo-Email-Extention-Notification-Pipeline/jenkins-pipeline-email-notifications.jpg)
</Frame>

***

## 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:

```groovy theme={null}
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:

```groovy theme={null}
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.*'
        }
    }
}
```

<Callout icon="lightbulb" color="#1CB2FE">
  To only send emails on failures, place `emailext` under `post { failure { ... } }` instead of `always`.
</Callout>

***

## 5. Execute the Pipeline

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

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870366/notes-assets/images/Certified-Jenkins-Engineer-Demo-Email-Extention-Notification-Pipeline/jenkins-dashboard-pipeline-email-notification.jpg)
</Frame>

***

## 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

<Frame>
  ![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".](https://kodekloud.com/kk-media/image/upload/v1752870367/notes-assets/images/Certified-Jenkins-Engineer-Demo-Email-Extention-Notification-Pipeline/gmail-inbox-jenkins-build-email.jpg)
</Frame>

Sample console output:

```text theme={null}
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: sid.live.demos@gmail.com
```

***

## 7. Sending HTML-Formatted Emails

To use rich HTML templates, create an HTML file (e.g., `email-template.html`):

```html theme={null}
<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`.

<Frame>
  ![The image shows a Jenkins system configuration page for setting up email notifications, including SMTP port, credentials, and security options like SSL.](https://kodekloud.com/kk-media/image/upload/v1752870369/notes-assets/images/Certified-Jenkins-Engineer-Demo-Email-Extention-Notification-Pipeline/jenkins-email-notifications-configuration.jpg)
</Frame>

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

***

## References

* [Email Extension Plugin — Official](https://plugins.jenkins.io/email-ext/)
* [Jenkins Pipeline Syntax](https://www.jenkins.io/doc/book/pipeline/syntax/)
* [Jenkins System Configuration](https://www.jenkins.io/doc/book/managing/system-configuration/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/4d6d1f39-307c-4fdb-8d2b-834c1650e792/lesson/7cbefa02-9152-4145-9e36-705b8b170459" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/4d6d1f39-307c-4fdb-8d2b-834c1650e792/lesson/4860d04d-a61e-4d6c-884c-fc7f87a75e63" />
</CardGroup>
