Display HTML page inside mail body with Email-ext plugin in Jenkins
Asked Answered
H

7

38

I am new to Jenkins and I want to know how it is possible to display the HTML report (not the HTML code) generated after a successful build inside a mail body (not as an attachment).

I want to know the exact steps I should follow and what should be the content of my possible jelly template.

Hampton answered 27/2, 2014 at 11:20 Comment(0)
M
60

Look deeper into the plugin documentations. No need for groovy here.

Just make sure Content Type is set to HTML and add the following to the body:

${FILE,path="my.html"}

This will place the my.html content in your email body (location of file is relative to job's workspace. I use it and it works well.

I hope this helps.

EDIT: Note that you must have the Jenkins version 1.532.1 (or higher) to support this feature with the email-ext plugin.

Metronymic answered 27/2, 2014 at 13:16 Comment(20)
i set the content to html and did as mentioned now it is returning an error saying in the body of the mail saying ERROR: File '/data/test.html' does not existHampton
/data/test.html is an absolute path from the root of the file system (leading slash). Try only data/test.html.Metronymic
supposed that the absolute path of my html file is /data/jenkins/jobs/GAMETV/htmlreports/HTML_Report/GAMETV.html what should i put as path ?Hampton
As an initial test, put the whole path /data/jenkins/jobs/GAMETV/htmlreports/HTML_Report/GAMETV.html to see if it works.Metronymic
Is your Jenkins running as a slave? Is the file accessible to the user running Jenkins? Is the java process that runs the Jenkins instance located on the same file system as the file? Try putting the file in the job's workspace and try just that.Metronymic
the html file i am trying to show is actually the html file generated on my last build so it should be accessible to the user as it is generated by the Jenkins' jobHampton
Try testing with a manually created file in the workspace. Make sure you have an updated version of the plugin...Metronymic
it seems that the problem is with my Jenkins version as the one i have is ver. 1.513 and the required one for the plugin is 1.532.1Hampton
Well, did you manage? Did you update your Jenkins and plugin? If so, let me know so I can edit my answer for others to benefit.Metronymic
yes i managed to do it.just mention that the email-ext plugin needs at least 1.532.1 version of jenkinsHampton
I was able to use this functionality with Jenkins ver. 1.509.4 and Email-ext 2.35.1. Also You can use absolute path and environment variables :)Jurel
this syntax sends html report in the email but i am looking for link to send in email. can some one help me on this?Trochal
Can i somehow enlarge the content of file in email. It looks smallCroix
@EldadAK your solution didn't work for me. For Editable Email notification Content Type selected as Html and Default Content field I've given value per your answer. Please clarify what step am I doing wrong ?Pitchblende
Did you add the ${FILE,path="my.html"} to the email body? Do you have any error?Metronymic
@EldadAK I did below steps but couldn't get it working. ${FILE,path="workspace/target/site/serenity/index.html"} in Content field of Editable Email Notification > Triggers > Always. It shows error message "ERROR: File 'workspace/target/site/serenity/index.html' does not exist". Can you please share screen shotPitchblende
Got it working by updating "Editable Email Notification" > "Content Type" value to HTML (text/html) and "Default Content" field value to "${FILE,path="target/site/serenity/index.html"}. Didn't do any changes for Triggers fields.Pitchblende
I am also working on the same and able to see the report in mail body but the content display is not proper so do I need to any CSS in configuration property to get the proper display in email body?Columbous
Unfortunately this fails when job is aborted. It throws Null Pointer exception.Westberry
Assuming you have expressions in the html like '${BUILD_NUMBER}' will they get resolved or will the remain as are. I need a way to format them before the actual send.Recalcitrate
K
11

Besides reading the file with body: ${FILE,path="index.html"}, you need to set the proper content type, either globally or explicitly for one execution, with mimeType: 'text/html.

emailext subject: '$DEFAULT_SUBJECT',
                    body: '${FILE,path="index.html"}',
                    recipientProviders: [
                        [$class: 'CulpritsRecipientProvider'],
                        [$class: 'DevelopersRecipientProvider'],
                        [$class: 'RequesterRecipientProvider']
                    ], 
                    replyTo: '$DEFAULT_REPLYTO',
                    to: '$DEFAULT_RECIPIENTS',
                    mimeType: 'text/html'
Kop answered 19/6, 2019 at 16:6 Comment(2)
I received the email but css is not displayed. I am using dashboard.html of extent reportGlide
Yes. My problem is caused by missing the mimeType. Great.Caldwell
A
8

It worked for me with Jenkins 1.558

${FILE,path="target/failsafe-reports/emailable-report.html"}
Arabeila answered 19/9, 2014 at 7:2 Comment(1)
can you please add screen shot showing where to add this value ?Pitchblende
N
5

If you use a custom path

I had a complication trying to achieve this result because my path was dynamically changing and I had to use a variable inside a FILE variable. So when I tried any of the following

body: '${FILE,path=${report}}'
body: "${FILE,path=${report}}"
body: '''${FILE,path=${report}}'''

and many more, it didn't work. On the other hand I couldn't read the file with groovy because of Jenkins restrictions

My workaround was to read the html directly with shell like so

html_body = sh(script: "cat ${report}", returnStdout: true).trim()

and then just send the email

emailext replyTo: '$DEFAULT_REPLYTO',
  subject: "subject",
  to: EMAIL,
  mimeType: 'text/html',
  body: html_body

where ${report} was a path to html file like /var/jenkins/workspace_318/report.html

Noteworthy answered 6/1, 2021 at 22:1 Comment(0)
G
3

It should be something like this:

Navigation:

Configure -> Editable Email Notification

Default Content:

${FILE,path="path/result.html"}
Genevivegenevra answered 22/2, 2018 at 9:27 Comment(0)
C
2

You just need to assign the link to the environment variable and then you can use that variable to print in the email using ${ENV, var=ENV_VARIABLE}.

Cauca answered 17/11, 2015 at 22:14 Comment(0)
P
1

You can use Editable Email Notification post build action to send html content as part of mail body.

Copy html content in Default Content and select Content Type as HTML (text/html), as in below image: enter image description here

Poundal answered 8/2, 2017 at 16:28 Comment(2)
As of 2021, it seems to have been deprecated.Romp
@EladAvron if you face issue that instead of HTML content it puts the actual file path in the email body, then check if this latest version of the plugin - plugins.jenkins.io/token-macro is installed. I faced this issue and resolved when I updated this plugin. (it is internally used by Email Ext plugin, but seems to be not auto updated along with Email Ext update)Reign

© 2022 - 2024 — McMap. All rights reserved.