Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 2, 2025

This PR implements custom email templates for verification codes with support for both plain text and HTML content, including automatic placeholder replacement.

Features Added

  • Custom email subject and body templates - Users can now provide custom mail_subject and mail_body parameters to the Auth class constructor
  • {verifcode} placeholder replacement - Automatic replacement of {verifcode} placeholders in both subject and body with the actual verification code
  • HTML email support - Automatic detection of HTML content and creation of multipart emails with both HTML and plain text versions
  • Fallback to defaults - When custom templates are not provided, the system falls back to the original default templates
  • Backward compatibility - All existing code continues to work without any changes

Usage Examples

Custom Plain Text Templates

auth = Auth(
    mongo_uri="mongodb://localhost:27017",
    db_name="your_db",
    mail_info={...},
    mail_subject="Welcome! Your code: {verifcode}",
    mail_body="Please use this verification code: {verifcode} to complete registration."
)

HTML Email Templates

auth = Auth(
    mongo_uri="mongodb://localhost:27017",
    db_name="your_db", 
    mail_info={...},
    mail_subject="🔐 Verification Code: {verifcode}",
    mail_body="""
    <html>
    <body style="font-family: Arial, sans-serif;">
        <h2>Welcome!</h2>
        <p>Your verification code is:</p>
        <div style="background: #007bff; color: white; padding: 10px; font-size: 20px;">
            {verifcode}
        </div>
    </body>
    </html>
    """
)

Default Behavior (No Changes Required)

# Existing code works unchanged
auth = Auth(mongo_uri="...", db_name="...", mail_info={...})
# Uses defaults: Subject: "Your Verification Code", Body: "Your verification code is: {verifcode}"

Technical Implementation

  • Enhanced send_verification_email() function to accept optional subject and body parameters
  • Added HTML detection using common HTML tags (<html>, <body>, <p>, <br>, <div>, <span>)
  • Implemented multipart email creation with both HTML and plain text versions for HTML content
  • Updated all 4 calls to send_verification_email() in the Auth class to pass custom templates
  • Added comprehensive documentation for the new parameters in the Auth class constructor

Files Changed

  • src/easy_mongodb_auth_handler/package_functions/func.py - Enhanced email sending function
  • src/easy_mongodb_auth_handler/auth.py - Updated function calls and documentation

Testing

All functionality has been thoroughly tested including:

  • Default template behavior
  • Custom plain text templates
  • HTML email generation
  • Placeholder replacement in both subject and body
  • Edge cases (empty codes, no placeholders, partial customization)
  • Case sensitivity (only {verifcode} is replaced, not variations)

Fixes #5.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: lukbrew25 <79376204+lukbrew25@users.noreply.github.com>
Copilot AI changed the title [WIP] Custom verification code email subject and body Add custom verification email templates with HTML support and placeholder replacement Jul 2, 2025
Copilot finished work on behalf of lukbrew25 July 2, 2025 01:36
@lukbrew25 lukbrew25 requested a review from Copilot July 2, 2025 01:36

This comment was marked as outdated.

@lukbrew25 lukbrew25 requested a review from Copilot July 2, 2025 01:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds customizable email templates for verification codes with HTML support and placeholder replacement, ensuring fallback to defaults and backward compatibility.

  • Added subject and body parameters to the email-sending function, with automatic {verifcode} replacement
  • Implemented HTML detection to send multipart emails (HTML + plain text)
  • Updated Auth class to pass custom templates and extended documentation accordingly

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/easy_mongodb_auth_handler/package_functions/func.py Added HTML detection, multipart email support, and placeholder replacement in send_verification_email.
src/easy_mongodb_auth_handler/auth.py Updated calls to send_verification_email, and expanded constructor docs for new template parameters.
.pylintrc Disabled the R0914 warning to accommodate increased positional arguments.
Comments suppressed due to low confidence (1)

src/easy_mongodb_auth_handler/package_functions/func.py:109

  • The default subject was previously "Your Verification Code" and is documented as such in the PR description. This change may break existing integrations—consider aligning the default value with prior behavior or clearly documenting the new default.
        subject = "Verification Code"

@lukbrew25 lukbrew25 added enhancement New feature or request help wanted Extra attention is needed labels Jul 2, 2025
@lukbrew25 lukbrew25 marked this pull request as ready for review July 2, 2025 01:51
@lukbrew25 lukbrew25 merged commit 74756e1 into main Jul 2, 2025
14 checks passed
@lukbrew25 lukbrew25 deleted the copilot/fix-5 branch July 2, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom verification code email subject and body

2 participants