0
0
Jenkinsdevops~5 mins

Email notification plugin in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sending email notifications helps you know when your Jenkins jobs succeed or fail without checking the dashboard constantly. The Email Notification plugin automates this by sending emails based on job results.
When you want to get an email alert if a build fails so you can fix it quickly.
When you want to notify your team automatically after a successful deployment.
When you want to send a summary email after nightly builds to stakeholders.
When you want to keep track of build status without logging into Jenkins.
When you want to customize who receives emails based on job outcomes.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
  }
  post {
    success {
      emailext(
        subject: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
        body: "Good news! The build succeeded. Check it here: ${env.BUILD_URL}",
        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
      )
    }
    failure {
      emailext(
        subject: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
        body: "Oops! The build failed. Please check the logs: ${env.BUILD_URL}",
        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
      )
    }
  }
}

This Jenkinsfile defines a simple pipeline with build and test stages.

The post section sends emails after the job finishes.

emailext is used to send emails on success or failure with a subject, body, and recipients set to developers.

Commands
Installs the Email Extension plugin needed to send email notifications from Jenkins.
Terminal
jenkins-plugin-cli --plugins email-ext
Expected OutputExpected
Installing plugin: email-ext Downloading plugin: email-ext Installed plugin: email-ext Restart Jenkins to activate the plugin.
Restarts Jenkins to activate the newly installed Email Extension plugin.
Terminal
jenkins restart
Expected OutputExpected
Jenkins is restarting... Jenkins restarted successfully.
Starts a build of the Jenkins pipeline that includes email notifications.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] emailext Sending email to developers [Pipeline] End of Pipeline Finished: SUCCESS
Shows the console output of the last build to verify email steps ran.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] emailext Sending email to developers [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: configure email notifications in the post-build section to automatically alert on build success or failure.

Common Mistakes
Not installing the Email Extension plugin before using emailext in the Jenkinsfile.
The emailext step will fail because Jenkins does not recognize it without the plugin.
Install the email-ext plugin using jenkins-plugin-cli and restart Jenkins before using emailext.
Forgetting to configure SMTP server settings in Jenkins global configuration.
Emails will not send because Jenkins cannot connect to an email server.
Set up SMTP server details under Manage Jenkins > Configure System before running jobs with email notifications.
Using incorrect recipientProviders or missing recipient emails.
Emails may not reach intended recipients or fail silently.
Use valid recipientProviders like DevelopersRecipientProvider or specify recipient emails explicitly.
Summary
Install the Email Extension plugin and restart Jenkins to enable email notifications.
Add emailext steps in the post section of your Jenkinsfile to send emails on success or failure.
Verify the pipeline runs and emails are sent by checking the console output and email inbox.