邮件服务管理
manage-email-services
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-12
- Stars
- ★ 178
- Forks
- 222
02 资源文件 1
- 其他OWNERSHIP27 B
03 SKILL.md 正文
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
name: manage-email-services description: Configure and implement email services in OFBiz.
Skill: manage-email-services
Goal
Configure SMTP infrastructure and implement reliable, template-driven business notifications using OFBiz Screen Widgets, FreeMarker templates, and CommunicationEvent records.
Triggers
ALWAYS read this skill when:
- Creating new email notifications or modifying existing ones.
- Configuring SMTP settings in
general.properties. - Managing
ProductStoreEmailSettingorEmailTemplateSettingentities.
Use when
- Sending automated business notifications.
- Designing email templates with consistent branding.
- Attaching PDF documents (e.g., invoices) to emails.
Procedure
-
SMTP Configuration:
- Configure
framework/common/config/general.properties:
mail.notifications.enabled=Y mail.smtp.relay.host=smtp.example.com mail.smtp.port=465 mail.smtp.auth=true mail.smtp.auth.user=user@example.com mail.smtp.auth.password=password mail.smtp.starttls.enable=Y mail.smtp.ssl.enable=true- Note:
mail.smtp.starttls.enableoften requiresYin OFBiz, while other boolean flags usetrue. - Use
mail.notifications.redirectToin development to intercept all outgoing mail.
- Configure
-
Template Definition:
- Commerce Emails: Prefer
ProductStoreEmailSettingfor order-to-cash flows.- Examples: Order confirmations, Shipment notifications, Return confirmations.
- System Emails: Use
EmailTemplateSettingfor generic/administrative alerts.- Examples: Password reset, User invitations, Administrative alerts.
- Full Compatibility: Use
altBodyScreenLocationto provide a plain-text fallback for HTML emails. - Example XML Data:
<ProductStoreEmailSetting productStoreId="9000" emailType="PRDS_ODR_CONFIRM" bodyScreenLocation="component://ecommerce/widget/EmailOrderScreens.xml#OrderConfirmNotice" altBodyScreenLocation="component://ecommerce/widget/EmailOrderScreens.xml#OrderConfirmNoticeText" subject="Order Confirmation #${orderId}" fromAddress="orders@example.com"/> - Commerce Emails: Prefer
-
Store & WebSite Mapping:
ProductStoreEmailSettingrelies on theWebSiteentity (viaproductStoreId) to determine the base URL for links.- Ensure your
WebSitehasstandardHostNameandsecureHostNameconfigured to avoid broken links in emails.
-
Screen Design:
- Create a screen in your component's
widget/EmailScreens.xml. - Ensure all links and images use absolute URLs.
- Pro-Tip: Use
NotificationServices.setBaseUrl(delegator, webSiteId, bodyParameters)in your logic before sending to populate${baseUrl}in FTL.
- Create a screen in your component's
-
Service Invocation:
- Use
sendMailFromScreenfor granular control and PDF attachments. - Use
sendMailFromTemplateSettingfor simple entity-driven sending. - ALWAYS use
runAsync(asynchronous) for production mailing to prevent thread blocking.
- Use
-
Handling Attachments:
- PDF Generation: Use
xslfoAttachScreenLocationto render a Screen Widget as a PDF using Apache FOP. - General Files: Use the
bodyPartsparameter (list of Maps) to attach existing files.
- PDF Generation: Use
-
Bulk and High-Volume Mailing:
- For high-volume notifications (e.g., shipment updates), use Job Queuing.
- Pattern: Iterate through your targets and schedule the mailing service via
dispatcher.schedule. - NEVER send thousands of emails in a single service execution or a single DB transaction.
CommunicationEvent Lifecycle
In OFBiz, emails are not just "logged"—they are managed as Communication Events. This is a core architectural pattern that ensures every outbound email has a central source of truth.
Why it Matters
- Customer Service Visibility: Agents can see exactly what was sent to a customer from any module (Order, Marketing, SFA).
- Business Audit Trail: Emails are linked to business documents (e.g.,
OrderHeader) viaCommunicationEventOrder. - Interaction History: Provides a complete timeline of customer touchpoints.
- Compliance: Ensures legal and support records are archived within the database, not just on a mail server.
The Lifecycle Pattern
- Creation: A service (e.g.,
sendOrderNotificationScreen) invokessendMailFromScreen. - Persistence: The
sendMailservice family includes SECA (Service ECA) triggers that automatically create aCommunicationEventrecord. - Linkage: The
communicationEventIdis then linked to the relevant business object (e.g.,OrderHeader,Shipment, orContactList).
Attachments and PDF Generation
OFBiz excels at generating and attaching documents like Invoices, Packing Slips, and Return Labels directly within the mail services.
The PDF Pattern (xslfoAttachScreenLocation)
This is the most common pattern for business documents. You provide a screen location, and the service renders it as a PDF attachment.
- Service:
sendMailFromScreen - Parameter:
xslfoAttachScreenLocation(e.g.,component://accounting/widget/InvoiceScreens.xml#InvoicePDF) - Expert Guidance: For designing XSL-FO templates and report screens, refer to the manage-pdf-export skill.
- Attachment Name: Pass
attachmentNameto set the filename (e.g.,Invoice_10000.pdf).
Multi-part Pattern (bodyParts)
For non-PDF attachments (e.g., CSVs, images) or multiple files, use sendMailMultiPart.
- Parameter:
bodyParts(AListofMapobjects containingcontent,type, andfilename).
Guardrails
- Transactional Integrity: CRITICAL. Commit business transactions before triggering emails. Use
runAsyncor After-Commit SECAs to prevent SMTP hangs from impacting DB locks. - Fault Tolerance: SMTP failures must NEVER break the main user workflow. Log errors, update
CommunicationEventstatus, but ensure primary actions succeed. - Modern Security: Externalize secrets (Env vars/Vaults). Avoid plain-text passwords in
general.properties. - Absolute Continuity: Always provide plain-text fallbacks (
altBodyScreenLocation) and passwebSiteIdfor correct absolute URL resolution. - Pattern Compliance: ALWAYS create and link
CommunicationEventrecords for auditability and customer history. - Environment Safety: Verify
mail.notifications.redirectTois set in dev/test.
Examples
Example: Custom Email Screen
<screen name="MyEmailBody">
<section>
<actions>
<set field="title" value="Special Notification"/>
</actions>
<widgets>
<platform-specific>
<html><html-template location="component://myplugin/template/email/MyEmailBody.ftl"/></html>
</platform-specific>
</widgets>
</section>
</screen>
Example: Sending via Template (Groovy)
// Sending an order confirmation asynchronously
run service: 'sendMailFromTemplateSetting', with: [
emailTemplateSettingId: 'PRDS_ODR_CONFIRM',
sendTo: 'customer@example.com',
bodyParameters: [orderId: '10000', webSiteId: 'MyStore']
]
Example: Sending an Invoice PDF (Groovy)
// Sending an invoice as a PDF attachment asynchronously
run service: 'sendMailFromScreen', with: [
emailTemplateSettingId: 'INVOICE_NOTIFY',
sendTo: 'customer@example.com',
bodyParameters: [invoiceId: '10000'],
xslfoAttachScreenLocation: 'component://accounting/widget/InvoiceScreens.xml#InvoicePDF',
attachmentName: 'Invoice_10000.pdf'
]
Example: Audit Trail Linking (Groovy)
// Step 1: Send the email as usual
Map result = run service: 'sendMailFromScreen', with: [
emailTemplateSettingId: 'PRDS_ODR_CONFIRM',
sendTo: 'customer@example.com',
bodyParameters: [orderId: '10000']
]
// Step 2: The service automatically creates a CommunicationEvent.
// Step 3: Link it to your business document for visibility.
String commEventId = result.communicationEventId;
run service: 'createCommunicationEventOrder', with: [
communicationEventId: commEventId,
orderId: '10000'
]
Anti-Patterns
1. Synchronous Mailing in Transactions
BAD: If SMTP hangs, the order creation fails or hangs indefinitely.
// Inside createOrder service
run service: 'createOrder', with: parameters
// ANTI-PATTERN: Sending synchronously inside the transaction
run service: 'sendOrderConfirmation', with: parameters
GOOD: Decouple delivery from business logic.
// Inside createOrder service
run service: 'createOrder', with: parameters
// Success: Return to caller. SECA or Post-Commit logic handles the email.
run service: 'sendOrderConfirmation', as: 'async', with: parameters
2. Relative URLs in Templates
BAD: Images won't load in Gmail/Outlook.
<img src="/images/company-logo.png"> <!-- BROKEN in email clients -->
GOOD: Use absolute URLs (e.g., https://example.com/images/company-logo.png).
3. Hardcoding HTML in Java
BAD: Frustrating to maintain and style.
String body = "<html><body><h1>Hello " + name + "</h1></body></html>"; // ANTI-PATTERN
GOOD: Use Screen Widgets and FreeMarker templates.