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.

  • Respoinse Assertions in JMeter with Jenkins - never showing a failure
  • Jenkins with Subversion
  • Marking upstream Jenkins/Hudson as failed if downstream job fails
  • Stuck at fetch due to authentication issue
  • Jenkins returned status code 128
  • Jenkins CI: How to trigger builds on SVN commit
  • Does anybody know how to do this in declarative pipeline?

  • jenkins workflow “Build whenever a SNAPSHOT dependency is built”
  • How start identical jobs with different parameters in parallel execution?
  • how to download protected resources in a jenkins workflow?
  • Jenkins Groovy post-build script to evaluate file with function
  • How do I avoid issues with “Protected Mode” using IEDriverServer in Selenium?
  • Passing arguments to Maven release build
  • 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: "test@test.com",
                sendToIndividuals: true])
            }
      }
    }
    
    Git Baby is a git and github fan, let's start git clone.