Display HTML page inside mail body with Email-ext plugin in Jenkins - jenkins

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.

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.

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'

It worked for me with Jenkins 1.558
${FILE,path="target/failsafe-reports/emailable-report.html"}

It should be something like this:
Navigation:
Configure -> Editable Email Notification
Default Content:
${FILE,path="path/result.html"}

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

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}.

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:

Related

Jenkins emailext plugin with default subject and body in pipeline script

I am using Jenkins with the email extension plugin and declarative pipelines. In https://jenkinsserver/configure i configured the "Default Subject" and "Default Content" which i want to use in a pipeline script.
When i add the following code to a pipeline script, everything works perfectly fine.
post {
always {
emailext (
to: 'my#my.dom',
replyTo: 'my#my.dom',
subject: "foo",
body: "bar",
mimeType: 'text/html'
);
}
}
But i don't want to specify something in the pipeline script, everything should be done whith the data specified in the global configuration. When i remove everything and just call emailext (); it failes with the comment that subject is missing. What can i do to work with default values specified globally?
As stated in the plugin documentation:
The email-ext plugin uses tokens to allow dynamic data to be inserted into recipient list, email subject line or body. A token is a string that starts with a $ (dollar sign) and is terminated by whitespace. When an email is triggered, any tokens in the subject or content fields will be replaced dynamically by the actual value that it represents.
This pipeline block should use the default subject and content from Jenkins configuration:
post {
always {
emailext (
to: 'my#my.dom',
replyTo: 'my#my.dom',
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
mimeType: 'text/html'
);
}
}
Make sure to use single quotes, otherwise Groovy will try to expand the variables.

Print user defined variable inside a HTML page with Email-ext plugin in Jenkins

I am using email ext in the jenkins pipeline script with email body reference to a file. I want to know how to pass user defined variable.
Ex:- I want to pass the variables "mySonarURL" and "myData" to the HTML file.
def committer = getGitCommitterEmail()
def mySonarURL = "My Sonar URL"
def myData = "My Data"
emailext (
to: committer,
subject: "Jenkins Build ${branchName} #${env.BUILD_NUMBER} [${currentBuild.result}]",
body: '${FILE,path="email.html"}',
mimeType: 'text/html'
)
}
HTML file
<table>
<tr><th>My Data:</th><td>"${myData}"</td></tr>
<tr><th>Sonar URL:</th><td>Sonar URL</td></tr>
</table>
I have tried using as above HTML file but they were returning just the string "${myData}" and "${mySonarURL}"
Mention type as Script in body of mail as guided in official Jenkins wiki
body: '${SCRIPT, template="email.html"}'

How can you configure Jenkins Pipeline so you can automatically send an email with a custom body?

I'm trying to have Jenkins pipeline automatically send an email, but with a custom body. The pipeline is called from a web application by a button, so I was thinking about having a text box there to write the desired message before the button is pressed. However, I don't know how this chunk of text can get sent to Jenkins.
Right now the pipeline is sending emails through emailext, with the body message hardcoded. I know I can pass data from a web app to Jenkins with the Build With Parameters API, which I'm currently using for a Username and Password field, but sending a whole email message as a parameter sounds incorrect.
emailext (
subject: "---subject---",
body: """Hi,
This is the hardcoded message that I would the user to have flexibility to create themselves
""",
to: "---list of recipients---"
)
You can also use something like this, where you could add REGEX and EXCERPT to customize your mail content
emailext(
to: "email_list",
subject: "Subject",
body: '''$BUILD_URL
${BUILD_LOG_REGEX, regex="DRYRUN.*DRYRUN.*DRYRUN",maxMatches=1, showTruncatedLines=false}
${BUILD_LOG_EXCERPT, start="EMAIL CONTENT:",end="END OF EMAIL CONTENT"}''',
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
Here is a function ready to be executed, you can also add an attachment.
Adapt to your needs.
def sendMail() {
def body = """
<html>
<body>
<p>Hello</p>
<p><img src="cid:screenshot.jpg" alt="screenshot"/></p>
<ul>
<li><strong>Jenkins Build URL:</strong> ${env.BUILD_URL}</li>
</ul>
</body>
</html>
"""
emailext(to: recipient, subject: 'SUCCESS : ' + subject, body: body, mimeType: 'text/html', attachmentsPattern: 'screenshot.jpg')
}
Ist thing you job needs a parameter MAILTEST and then you can just this parameter in the body of the email just like coldplayer proposed.
Honestley for I used https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin
because the default trigger support only a kind of token as parameter on the rest interface.

Email-Ext plugin templates not updated

I'm working with jenkins pipeline. I'm using this plugin to send email notification with the use templates. I reused an existing templates from github.
I place the templates $Jenkins_Home\email-templates\.
But my changes are not updated in the email. Still the old content was received.
Sample code:
def call(email, subject, content, attachment = null){
def attachBuildLog = currentBuild.result != 'SUCCESS'
emailext attachLog: attachBuildLog,
body: '${SCRIPT, template="groovy-html"}',
mimeType: 'text/html',
subject: "${subject}",
to: "${email}",
replyTo: "${email}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
}
Please advise.
Renaming the existing templates to a custom filename fixes the problem.

Sending HTML report as an attachment to mail in jenkins

I am new to Jenkins. As of now I have added email notification plugin. So once my test is completed now I am getting an email with Build log.
If I want to send Load runner HTML analysis report to mail in the same, what is the procedure?
Can anyone help me here ?
Thanks.
It's possible and it's pretty simple.
Download EmailExt plug-in (https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin)
Then you can use the Pipeline Syntax to easily create it.
However, here's an example your can see as a reference to what you need.
emailext attachmentsPattern: '**/*.html', body: 'Body here', subject: 'Subject here', to: 'mail#gmail.com, mail2#gmail.com, mail3#gmail.com'
attachmentsPattern uses Ant File Syntax (http://ant.apache.org/manual/dirtasks.html)
I don't know if this will help, but i'm using https://wiki.jenkins.io/display/JENKINS/HTML+Publisher+Plugin
to generate html report from the jenkins workspace ( pwd() )
stage('sysdig report'){
publishHTML(target: [
allowMissing : true,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : "${workspace}/",
reportFiles : "sysdig-report.html",
reportName : "sysdig-report"
])
You should be able to send the file after.

Resources