0
0
Jenkinsdevops~5 mins

Email notifications in pipelines in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your automated tasks need to tell you when they finish or fail. Email notifications in Jenkins pipelines help you get these messages automatically, so you don't have to check manually.
When you want to get an email if your code build fails so you can fix it quickly.
When you want to notify your team that a deployment finished successfully.
When you want to send a summary email after running tests in your pipeline.
When you want to alert yourself if a scheduled job did not run as expected.
When you want to keep stakeholders informed about pipeline results without logging into Jenkins.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
  }
  post {
    success {
      mail to: 'team@example.com',
           subject: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
           body: "Good news! The job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' succeeded."
    }
    failure {
      mail to: 'team@example.com',
           subject: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
           body: "Oops! The job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' failed. Please check the logs."
    }
  }
}

This Jenkinsfile defines a simple pipeline with two stages: Build and Test.

The post section sends email notifications after the pipeline finishes.

  • success: sends an email if the pipeline succeeds.
  • failure: sends an email if the pipeline fails.

The mail step uses the recipient's email, subject, and body to send the notification.

Commands
This command updates the Jenkins pipeline with the Jenkinsfile that includes email notifications. It applies the pipeline configuration to Jenkins.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command starts the pipeline job named 'my-pipeline' to run the stages and trigger email notifications based on the result.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1
This command shows the console output of the first build of 'my-pipeline' so you can verify the pipeline ran and emails were triggered.
Terminal
jenkins-cli console my-pipeline #1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] mail Sending email to team@example.com [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: use the post section in Jenkinsfile to send emails after pipeline success or failure.

Common Mistakes
Not configuring the SMTP server in Jenkins settings before using the mail step.
Without SMTP setup, Jenkins cannot send emails and the mail step will fail silently or cause errors.
Go to Jenkins system configuration and set up the SMTP server details before running pipelines with email notifications.
Using incorrect email addresses or missing quotes around strings in the mail step.
This causes syntax errors or emails not being sent to the right recipients.
Always use quotes around email addresses and strings, and double-check email addresses for typos.
Summary
Write a Jenkinsfile with a post section to send emails on success and failure.
Use the mail step with to, subject, and body parameters to customize notifications.
Run the pipeline and check console output to confirm emails are sent.