如何在WSO2 Siddhi IO EMAIL中发送HTML模板邮件?
Hey there! Let’s figure out how to send nicely formatted HTML emails with dynamic content using Siddhi’s email component. First off, a quick heads-up: your current config uses a @source which is for receiving emails—if you want to send them, you’ll need to switch to an @sink instead. That’s a common mix-up, no worries!
Here are two straightforward ways to add HTML content to your emails:
1. Static HTML Template with Dynamic Placeholders
You can define a fixed HTML structure right in the sink’s @map configuration, using double curly braces {{}} to inject values from your Siddhi events. This is great for consistent, reusable templates.
// First, define your input stream (matches your original) define stream InputStream (name string, age int, country string); // Define the email sink with HTML template @sink(type='email', username='your.sender.email@domain.com', password='your-email-password', // Use app-specific password if using services like Gmail to='receiver.account@domain.com', subject='New User Details', content.type='text/html', @map(type='text', @payload("<html><body> <h2 style='color: #2c3e50;'>User Profile</h2> <div style='padding: 10px; border: 1px solid #eee; border-radius: 4px;'> <p><strong>Name:</strong> {{name}}</p> <p><strong>Age:</strong> {{age}}</p> <p><strong>Country:</strong> {{country}}</p> </div> <p style='color: #7f8c8d;'>Sent via Siddhi Email Sink</p> </body></html>"))) define stream OutputStream(name string, age int, country string); // Route input events to trigger the email send @info(name='send-formatted-email') from InputStream select name, age, country insert into OutputStream;
2. Dynamic HTML Content from Event Attributes
If you want full control over the HTML content per event (like generating custom HTML for each user), you can build an HTML string directly in your Siddhi query and pass it as an event attribute.
// Input stream (same as your original) define stream InputStream (name string, age int, country string); // Sink that uses dynamic HTML content @sink(type='email', username='your.sender.email@domain.com', password='your-email-password', to='receiver.account@domain.com', subject='Customized User Email', content.type='text/html', @map(type='text', @payload("{{customHtml}}"))) define stream OutputStream(customHtml string); // Generate custom HTML per event and route to the sink @info(name='dynamic-html-generation') from InputStream select concat("<html><body> <h1>Hi {{name}}!</h1> <p>Thanks for joining us from {{country}}. We see you're {{age}} years old.</p> <div style='margin-top: 20px; padding: 15px; background: #f8f9fa; border-radius: 4px;'> <p>Feel free to reach out if you need help!</p> </div> </body></html>") as customHtml insert into OutputStream;
Key Things to Keep in Mind:
- Swap
@sourcefor@sink: Your original config is set up to receive emails—@sinkis what you need to send them. - Double-check
content.type='text/html': This tells email clients to render your content as HTML instead of plain text. - For secure email services (like Gmail), use an app-specific password instead of your regular account password if 2FA is enabled.
内容的提问来源于stack exchange,提问作者Jorge Monroy

