Send Jenkins console output as HTML email
Stefan Bogdanescu
Founder & Senior Architect
Sending Formatted HTML Console Output via Jenkins Email: A Developer's Guide
The Challenge: Formatting Build Output for Communication
The question of whether you can send rich, formatted HTML directly from a Jenkins build to an email is very common when dealing with internal tools and reporting. You are absolutely right to ask if this is possible; the default behavior of many notification systems often strips or misinterprets complex formatting, leaving you with unreadable chaos.
The scenario you describe—where your build successfully generates detailed HTML tables for diagnostics but standard email plugins only capture raw text—is a classic integration hurdle. Simply passing the raw console output as an email body results in broken formatting, making the information useless to recipients.
As a senior developer, I can tell you that while direct, magic-button support across all Jenkins plugins is rare, this task is entirely achievable by leveraging Jenkins' powerful scripting capabilities (Groovy) to act as the intermediary layer between the build execution and the email delivery system.
Why Simple Plugins Fall Short
You mentioned trying simple email plugins versus more advanced ones like Email Ext. The core issue often lies in where the formatting occurs:
- Simple Plugins: These typically hook into a pre-defined output mechanism (like the standard build log). They are excellent for sending plain text summaries but struggle when the content is complex, self-contained HTML that needs specific email styling (
<style>tags, table structures). - Email Ext/Post-Build Scripts: These plugins rely on executing a script after the build finishes. This post-build step gives you full control over the data stream. If your output is already generated as an HTML string within the build environment (which it is), you can capture that string, manipulate it if necessary, and inject it directly into the email body using standard JavaMail or similar libraries.
The key takeaway here is: Don't try to make the email plugin do the heavy lifting of parsing complex console logs; let Jenkins handle the data formatting first.
The Solution: Dynamic HTML Generation via Groovy Scripting
Since you cannot rely on external file paths due to firewall restrictions, the most robust solution involves generating the final HTML content within the build environment and then using a dedicated step or script to send that generated string.
Here is the conceptual flow:
- Execution: The build runs, generates the diagnostic HTML (as you have already done).
- Capture: A Jenkins Groovy script captures the output of the previous steps into a multi-line string variable.
- Injection: This variable is passed directly to the email notification step.
Code Example Concept (Jenkins Pipeline)
In a Declarative Pipeline, you can achieve this by using script blocks to manage the data flow:
pipeline {
agent any
stages {
stage('Build & Format Output') {
steps {
// 1. Run your build commands that generate HTML output (e.g., running a diagnostic script)
script {
def htmlReport = """
<html><head><style>/* Your CSS styles here */</style></head>
<body>
<h1>Build Report</h1>
<table>
<thead>...</thead>
<tbody>...</tbody>
</table>
</body></html>
"""
// Simulate running the build that produces this output
echo "Generated HTML: ${htmlReport}"
}
}
}
stage('Email Notification') {
steps {
// 2. Use a notification step (e.g., mailer step or custom shell script)
// The variable 'htmlReport' is passed directly to the email body.
script {
mail(to: 'team@example.com', subject: 'Build Failure Report', body: "The build failed. See attached report:\n\n${htmlReport}")
}
}
}
}
}
Conclusion: Control Over the Data Stream
It is entirely possible, and highly recommended, to send rich HTML output from a Jenkins job. The secret isn't forcing an email plugin to magically interpret raw console text; it’s using the pipeline scripting environment—Groovy—to control the entire data flow. By generating your detailed report as a single, formatted string within the build process, you gain absolute control over what gets sent.
This approach ensures that whether you are dealing with complex structure or need to integrate systems (similar to how robust backend frameworks like those in Laravel handle structured data), you maintain integrity. If you are looking to build highly structured, predictable applications, understanding how to manage data serialization and presentation is crucial. For powerful system integration and data handling techniques, exploring architectural patterns similar to those used in the Laravel ecosystem can provide excellent context on structuring complex information reliably.
Ultimately, modifying your output to display formatted text before it hits the email function is the most reliable and professional path forward.
Note: Blog content is currently available in English.