Question: How to send a html file in the Email body based on multiple projects?
with the below straight forward code am able to send the mail but if i use " if else condition" then its failing, is it possible to use if condition with in the format?

straight forward code:

```
always {
        emailext mimeType: 'text/html',
        body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
        subject: '${JOB_NAME} Report',
        to: 'xyz.com'
        }

```

I am trying to achieve something like this:

```
post {
    success {
        archiveArtifacts 'logs/${JOB_BASE_NAME}_Build_${BUILD_NUMBER}/**/*, logs/static_results/*'
        script {
            load "logs/${JOB_BASE_NAME}_Build_${BUILD_NUMBER}/display/pipeline_vars.groovy"
        }
    }
    if ((${NODE_NAME}.contains("KG"))&&(${JOB_NAME}.contains("Nightly"))) {
        always {
            emailext mimeType: 'text/html',
            body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
            subject: '${JOB_NAME} Report',
            to: 'xyz.com'
        }
    }
    else if ((${NODE_NAME}.contains("MT")) &&(${JOB_NAME}.contains("Feature"))) {
        always {
            emailext mimeType: 'text/html',
            body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
            subject: '${JOB_NAME} Report',
            to: 'xyz.com'
        }
    }
}

```

Answer: 
The issue here is that you cannot have `always` *inside* an `if-else` block. In `Declarative pipeline` syntax, you will have to use `if-else` inside a `script` block as shown below:

```
pipeline {
    agent { label 'windows' }
    //agent { label 'linux' }

    stages {
        stage('Echo') {
            steps {
                echo 'Inside Echo block'
            }
        }
    }
    post {
        always {
            script {
                if ((env.NODE_NAME == "windows")) {
                    echo "Running on ${env.NODE_NAME} node"
                }
                else if ((env.NODE_NAME == "linux")) {
                    echo "Running on ${env.NODE_NAME} node"
                }
            }
        }
    }
}

```

Output:

[![enter image description here](https://i.stack.imgur.com/PaS99.jpg)](https://i.stack.imgur.com/PaS99.jpg)
Below code solved my problem:

```
always {
                    script {
                       if (env.JOB_NAME.contains('Nightly'))
                       {
                            emailext (
                                to: '${DEFAULT_RECIPIENTS}',
                                subject: "${env.JOB_NAME}-Report",
                                body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/xyz.html"}',
                                attachLog: true,
                                attachmentsPattern: 'logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/xyz.png',
                            )
                        }
                    }
                }

```