5
votes

I need to add an image in email as email body not as attachment, from Jenkins via pipeline. I am using emailext plugin in Jenkins pipeline, below is the code I am using.

emailext (
          subject: "test email",
          body: """
          <html>
          <body>
          <p>please find attached score: Job '${env.JOB_NAME}':</p>
          <p> The last commit was by ${last_commit_user} </p>
          <p>Please check jenkins console at "</p> 
          <p> For detailed report on this analysis, visit "</p>
          </body>
          </html>
          """,
          to: email_recipients,
          attachmentsPattern: '${BUILD_NUMBER}.jpg'
)

I do not want to use "attachmentsPattern" , that comes as an attachment, I tried using,

body: """ 
<html>
<img src="image_name.jpg" >
</html>
"""

That comes only as blue box in my email , I am giving proper image path relative to my Jenkins workspace, I tried to search relevant solutions but in vain.

5
Have you found a way to embed images in Jenkins email-ext?Dinesh
did you find any solutions? i'm also having similar issues on my endPANDA MAN
@Dinesh Please find my solution below, I was facing the same problem.np2807

5 Answers

3
votes

You can either do this with base64 string of your image as correctly described earlier, or you can add your image as attachment in the email text and then reference this in your img src attribute.

Simple way to create html file (which will be your html body in the email) straight in the pipeline

 sh "echo '<b>Job Name: </b>${env.JOB_NAME}<br />' > mail.html"
 sh "echo '<b>Execution Result: </b>${currentBuild.currentResult}<br />' >> mail.html"
 sh "echo '<b>Build Number: </b> ${env.BUILD_NUMBER}<br />' >> mail.html"
 sh "echo '<b>Build URL: </b> ${env.BUILD_URL}<br />' >> mail.html"  
 sh "echo '<img src='cid:sample.jpg' alt='hello'>' >> mail.html"

1.) Add attachment into emailtext

   emailext attachmentsPattern: 'sample.jpg', 
   body: '${FILE,path="mail.html"}',
   to: "${emailRecipientsList}",
   recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
   subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}",
   mimeType: 'text/html' 

2.) Reference the image in attachment in your img src attribude

<img src='cid:sample.jpg' alt='hello'>
1
votes

You need to convert your image to Base64. I do it using python.

import base64

base64Img = ''

with open("image.png", "rb") as imageFile:
    base64Img = base64.b64encode(imageFile.read())

with open("image.html", "wb+") as writer:
    writer.write('<img src="data:image/png;base64,'.encode())
    writer.write(base64Img)
    writer.write('">'.encode())

This writes a file ''image.html''. This file you can then append into your <body>. In my pipeline I do it like so:

${FILE,path="image.html"}
1
votes

You can not add an image in .png format. Instead you can add the encoded base64 formatted image inside your html data.

To convert your actual image into base64, use LINK here. Source need to be changed.

body:"""
<html>
<img src="data:image/png;base64<BASE64_ENCODED_IMAGE>" >
</html>
"""

In above code 'BASE64_ENCODED_IMAGE' is base64 convered code obtained from above link.

Please see the link for Embed Base64-Encoded Images Inline In HTML

-2
votes

Push the image file in workspace directory of jenkins job.
Goto the workspace and click on the image to get the URL.
Place img src="IMAGE_URL" in email template.

-4
votes

To include images into your Jenkins Extended E-mail mails, you have to:

  • Put your images into Jenkins directory: $JENKINS_HOME/war/images/
  • Enable HTML format support in Jenkins Configuration
  • And then restart Jenkins service at final step

And you can insert the image YOUR_IMAGE.jpg into your groovy-html.template. Note that ${rooturl}static/e59dfe28/images is a constant.

<p>Some HTML...</p>
<img src="${rooturl}static/e59dfe28/images/YOUR_IMAGE.jpg"/>
<p>Some HTML...</p>