Skip to content

feat: the mail module can use html template directly#1145

Merged
krishankumar01 merged 21 commits intomasterfrom
kkumar-gcc/#260
Aug 12, 2025
Merged

feat: the mail module can use html template directly#1145
krishankumar01 merged 21 commits intomasterfrom
kkumar-gcc/#260

Conversation

@krishankumar01
Copy link
Member

@krishankumar01 krishankumar01 commented Jul 26, 2025

📑 Description

Closes goravel/goravel#260

Preview

Screenshot 2025-08-10 at 9 59 50 PM

This PR adds configurable template engine support to the mail module, allowing users to render email templates using Go's html/template engine with automatic caching and thread-safe access.

Features

  • Configurable Template Engine: Switch between different template engines via configuration
  • Built-in Caching: Templates are parsed once and cached for improved performance
  • Thread-Safe: Safe for concurrent use across multiple goroutines
  • Global Registry: Template engines are cached globally to avoid recreation

Usage

Configuration

// config/mail.go
"template": map[string]any{
	"default": config.Env("MAIL_TEMPLATE_ENGINE", "html"),
	"engines": map[string]any{
		"html": map[string]any{
			"driver": "html",
			"path":   config.Env("MAIL_VIEWS_PATH", "resources/views/mail"),
		},
	},
}

Email Templates

Create template files in your views directory:

<!-- resources/views/mail/welcome.html -->
<h1>Welcome {{.Name}}!</h1>
<p>Thank you for joining {{.AppName}}.</p>

Sending with Templates

// Using fluent API
facades.Mail().
    To([]string{"user@example.com"}).
    Subject("Welcome").
    Content(mail.Content{
        View: "welcome.html",
        With: map[string]any{
            "Name": "Krishan",
            "AppName": "MyApp",
        },
    }).
    Send()

Custom Template Engines

Register custom engines in configuration:

"template": map[string]any{
	"default": "blade",
	"engines": map[string]any{
		"blade": map[string]any{
			"driver": "custom",
			"via": func() (mail.Template, error) {
				return NewBladeTemplateEngine(), nil
			},
		},
	},
}

Breaking Changes

Yes - mail.NewApplication returns error

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings July 26, 2025 14:21
@krishankumar01 krishankumar01 requested a review from a team as a code owner July 26, 2025 14:21

This comment was marked as outdated.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 43434ab Previous: e770490 Ratio
BenchmarkFile_ReadWrite 304402 ns/op 6258 B/op 99 allocs/op 180514 ns/op 6257 B/op 99 allocs/op 1.69
BenchmarkFile_ReadWrite - ns/op 304402 ns/op 180514 ns/op 1.69

This comment was automatically generated by workflow using github-action-benchmark.

@krishankumar01 krishankumar01 marked this pull request as draft July 26, 2025 15:17
@codecov
Copy link

codecov bot commented Aug 8, 2025

Codecov Report

❌ Patch coverage is 36.49289% with 134 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.52%. Comparing base (93dc8a3) to head (fa5ffbc).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
mail/application.go 0.00% 96 Missing ⚠️
mail/setup/config/mail.go 0.00% 23 Missing ⚠️
mail/job.go 51.72% 14 Missing ⚠️
mail/service_provider.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1145      +/-   ##
==========================================
+ Coverage   68.44%   68.52%   +0.08%     
==========================================
  Files         221      223       +2     
  Lines       14320    14466     +146     
==========================================
+ Hits         9801     9913     +112     
- Misses       4144     4178      +34     
  Partials      375      375              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@krishankumar01 krishankumar01 marked this pull request as ready for review August 9, 2025 09:12
@krishankumar01 krishankumar01 changed the title feat: the mail module can use http template directly feat: the mail module can use html template directly Aug 10, 2025
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Awesome. Please add the sent mail screenshots in the description.

@hwbrzzl hwbrzzl requested a review from Copilot August 11, 2025 10:15
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 configurable template engine support to the mail module, enabling users to render email templates using HTML templates with automatic caching and thread-safe access. The implementation introduces a flexible template engine system that supports both built-in HTML templating and custom template engines via factory functions.

  • Introduces a new Template interface and HTML template engine implementation with caching
  • Adds template registry system for managing multiple template engines with global caching
  • Refactors mail job handling to use a single Params struct instead of multiple individual arguments

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
contracts/mail/template.go Defines the Template interface for rendering templates
contracts/mail/mail.go Extends Content struct to support template views and data
mail/template/html.go Implements HTML template engine with thread-safe caching
mail/template/registry.go Provides global template engine registry with factory support
mail/application.go Integrates template rendering and refactors to use Params struct
mail/job.go Simplifies job arguments to use single Params struct
mail/setup/config/mail.go Adds template configuration options
errors/list.go Adds mail template-specific error types
mocks/mail/Template.go Generated mock for Template interface

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Thanks, great 👍 Could you update the description according to the latest logic?

@krishankumar01
Copy link
Member Author

Thanks, great 👍 Could you update the description according to the latest logic?

I have already updated the description

@krishankumar01 krishankumar01 requested a review from Copilot August 11, 2025 15:29

This comment was marked as outdated.

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 configurable HTML template engine support to the mail module, enabling users to render email templates using Go's html/template engine with built-in caching and thread-safe access. The implementation introduces a template registry system that supports both built-in HTML templates and custom template engines via factory functions.

Key changes include:

  • Introduction of a template engine interface and HTML implementation with caching
  • Breaking change to mail.NewApplication signature to return an error for template initialization
  • Extension of mail content structure to support view templates with data binding
  • Updated job handling to accommodate separate HTML and text content

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
contracts/mail/template.go Defines the Template interface for rendering templates
mail/template/html.go Implements HTML template engine with caching and thread-safe access
mail/template/registry.go Provides global template engine registry with factory support
mail/application.go Updates mail application to integrate template rendering with breaking API changes
mail/job.go Modifies job handler to support separate HTML/text content parameters
mail/setup/config/mail.go Adds template configuration section with HTML engine defaults
errors/list.go Adds mail template-specific error definitions

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Perfect 👍

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Great 👍


r.cache.LoadOrStore(templatePath, tmpl)
return tmpl, nil
actual, _ := r.cache.LoadOrStore(templatePath, tmpl)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please judge the error here.

Copy link
Member Author

Choose a reason for hiding this comment

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

The second parameter is not an error, but a boolean indicating whether the template was loaded successfully. Here, we are storing a new template, so the actual value always presents.

// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.

Copy link
Contributor

@hwbrzzl hwbrzzl Aug 12, 2025

Choose a reason for hiding this comment

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

Thanks for the explanation.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Amazing 👍

@krishankumar01 krishankumar01 merged commit 828daf7 into master Aug 12, 2025
14 of 15 checks passed
@krishankumar01 krishankumar01 deleted the kkumar-gcc/#260 branch August 12, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

✨ [Feature] The mail module can use http template directly

3 participants