Enable HTML emails#1685
Conversation
Co-authored-by: fiammybe <3736946+fiammybe@users.noreply.github.com>
Updated development guidelines for code style and PHP standards.
Auto-detect .html.tpl templates and enable HTML mode. Add isHtml and altBody properties with setAltBody() and useHtml() methods; skip plain- text newline normalization for HTML. Fix multimailer fallback to icms_messaging_EmailHandler to avoid self-instantiation.
Review Summary by QodoCode cleanup, property declarations, and formatting improvements
WalkthroughsDescription• Add missing property declarations for Handler class • Rename $LE to $LineEndingChar for clarity • Add PluginDir property to EmailHandler class • Comprehensive code formatting and style improvements • Fix multimailer instantiation bug in constructor Diagramflowchart LR
Handler["Handler.php<br/>Property Declarations"]
EmailHandler["EmailHandler.php<br/>PluginDir Property"]
Formatting["Code Formatting<br/>Style Standardization"]
BugFix["Constructor Bug Fix<br/>Multimailer Instantiation"]
Handler --> Formatting
EmailHandler --> Formatting
BugFix --> Formatting
File Changes1. htdocs/libraries/icms/messaging/Handler.php
|
Code Review by Qodo
1. LineEndingChar not used
|
When XoopsMailerLocal is not available, the fallback was incorrectly creating a new icms_messaging_Handler() instead of icms_messaging_EmailHandler(). This caused: 1. Infinite recursion (Handler creating Handler) 2. Wrong type assigned to \ (should be EmailHandler, a PHPMailer subclass) Changed fallback to correctly instantiate icms_messaging_EmailHandler().
- Use null coalescing operator (??) to safely access icmsConfig['language'] - Default to 'english' if language key is missing - Refactor template path construction for clarity - Add HTML template auto-detection logic (checks for .html.tpl variant) - Improves robustness when configuration keys are missing or overridden
- Remove @ suppression operator from fopen() to expose real errors - Always capture error messages, not just when debug=true - Add is_readable() check before attempting to open file - Include file path in error messages for better diagnostics - Properly close file handle after reading - Distinguish between 'file not readable' and 'cannot open' errors This ensures email template loading failures are always logged with actionable information, improving debugging and troubleshooting.
- Add \ property declaration to track HTML email mode - Initialize \ to false in reset() method - Call \->multimailer->isHTML(\->isHtml) in sendMail() to configure PHPMailer This ensures that when HTML templates (.html.tpl) are detected and loaded, PHPMailer is properly configured to send them as text/html instead of text/plain. The send() method already detects HTML templates and sets \->isHtml = true, but sendMail() was not using this flag to configure PHPMailer. Now the complete HTML email flow works: 1. send() detects .html.tpl template and sets \->isHtml = true 2. sendMail() calls isHTML(\->isHtml) to configure PHPMailer 3. PHPMailer sends email with Content-Type: text/html
- Add \ property for plain text fallback in HTML emails - Initialize \ in reset() method - Improve sendMail() to properly handle both HTML and plain text modes - Always call isHTML() explicitly to reset ContentType between sends - Set AltBody for HTML emails, clear it for plain text emails This ensures: 1. HTML emails have a plain text fallback (AltBody) 2. ContentType is correctly reset between successive sends 3. Plain text emails don't have leftover HTML configuration 4. Better compatibility with email clients that prefer plain text
- Add setAltBody(\) method to allow setting plain text fallback for HTML emails - Add useHtml(\ = true) method to allow manual control of HTML mode - These methods provide a public API for callers to configure HTML email behavior This allows callers to: 1. Manually set HTML mode via useHtml(true) if not using auto-detection 2. Provide a plain text fallback via setAltBody() for better email client compatibility 3. Have full control over email format when needed
The library we're using PHPMailer, has the ability to send HTML emails, ImpressCMS just never was adapted to do so. This is a minimal change to make it possible to send HTML emails.