2

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)
        }
    }   
}
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
  • jenkins mail step does not support attachments https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#mail-mail theoretically it's possible to build body as multipart mime type, but then you need an external library... – daggett Sep 21 '22 at 09:06

2 Answers2

1

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: adress@g.com)
ycr
  • 12,828
  • 2
  • 25
  • 45
0

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()
)
daggett
  • 26,404
  • 3
  • 40
  • 56