Home > Software design >  How to send email with attachment in jenkinsfile
How to send email with attachment in jenkinsfile

Time:09-22

I want to send email after post build action in jenkins. Hence I have write jenkinsfile as follows. But i want to send mail with pdf attachment.

Note: Please don't suggest email plugin procedure & configuration. I preferred Jenkins file method configuration

post {
        success{
            script{
                    echo "Success!! e-mailing scan results url to ${emailRecipients}"
                    mail(from: emailFrom, subject: emailSubjectCDSuccess   COMMIT, to: emailRecipients, body: emailBodyCheckmarx)   
              }
        }
        failure {
            script{
                     echo "Failure :( !! e-mailing scan results url to ${emailRecipients}"
                     mail(from: emailFrom, subject: emailSubjectCDFailure, to: emailRecipients, body: emailBodyCD)
            }
         } 
    }   
}

CodePudding user response:

not an answer

try approximately the following parameters of mail step:

def boundary = '----------12345'
def attachment = 'hello world'.bytes.encodeBase64() //read bytes from file and encode as base64
def body = 'hello body'.getBytes('UTF-8').encodeBase64()

mail(
...
mimeType: "multipart/mixed; boundary=${boundary}",
//you can't add extra-spaces or drop empty lines in following string
body: """
${boundary}
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

${body}
${boundary}
Content-Type: application/octet-stream; name="my-attachment.txt"
Content-Disposition: attachment
Content-Transfer-Encoding: base64

${attachment}
${boundary}--
""".trim()
)

CodePudding user response:

Instead of the default Email step, use the Email Extension Plugin, which allows you to add attachments.

emailext(
      subject: "SUBJECT",
      attachLog: true, attachmentsPattern: "**/*.txt",compressLog: true,
      body: "Test Email" ,to: [email protected])
  • Related