Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed image in email body in Jenkins pipeline

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.

like image 918
anudeep Avatar asked Nov 27 '22 02:11

anudeep


1 Answers

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'>
like image 183
emksk Avatar answered Dec 04 '22 06:12

emksk