How to get same Mailer behaviour for Jenkins pipeline

I started using Jenkins declarative pipelines. Now I want to have the same email notification behavior as defined in the Usage of the Mailer plugin:

  1. Every failed build triggers a new e-mail.
  2. A successful build after a failed (or unstable) build triggers a new e-mail, indicating that a crisis is over.
  3. An unstable build after a successful build triggers a new e-mail, indicating that there’s a regression.
  4. Unless configured, every unstable build triggers a new e-mail, indicating that regression is still there.

I read about Notifications in Pipelines, but it does not notify based on above rules. Plus it does not contain part of the console output in the message body in case of a build failure.

  • Jenkins Build Pipeline Scheduled Trigger
  • How to pass output from an upstream project to downstream projects in Jenkins
  • Jenkins won't start
  • Cancel queued builds and aborting executing builds using Groovy for Jenkins
  • Jenkins - Get code from BitBucket with SSH
  • Sonarqube 5.1 core auto assign feature not working
  • Does anybody know how to do this in declarative pipeline?

  • How to use Jenkins to push to Github on test success?
  • How to install Jenkins on WebSphere 8.5
  • Firefox crash when I run selenium unit test
  • How to setup credentials for every project in Jenkins?
  • How do I Efficiently list **All** currently running jobs on Jenkins using Groovy
  • Jenkins Windows Slave ignoring local Git settings
  • One Solution collect form web for “How to get same Mailer behaviour for Jenkins pipeline”

    With the following code you can use the mailer plugin in the post section. This provides the expected behaviour:

    pipeline {
      agent any
      stages {
          stage('test') {
            steps {
              script {
                  // change to 'UNSTABLE' OR 'FAILED' to test the behaviour 
                  currentBuild.result = 'SUCCESS'
      post {
            always {
              step([$class: 'Mailer',
                notifyEveryUnstableBuild: true,
                recipients: "",
                sendToIndividuals: true])
    Git Baby is a git and github fan, let's start git clone.