Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions packages/mailme/MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Migration from go-gomail to wneessen/go-mail

This document outlines the migration of the mailme package from the deprecated `go-gomail/gomail` library to the modern `wneessen/go-mail` library, as requested in GitHub issue #27950.

## Overview

The mailme package has been successfully migrated to use `wneessen/go-mail` instead of the deprecated `go-gomail/gomail` library. This migration provides:

- **Modern Go standards**: Updated to Go 1.21+ with idiomatic code
- **Active maintenance**: The wneessen/go-mail library is actively maintained
- **Enhanced security**: Better TLS support and security practices
- **Improved performance**: More efficient connection handling and context support
- **Better error handling**: More detailed error messages and proper error wrapping

## Changes Made

### 1. Dependencies Updated

**Before:**
```go
require (
github.com/sirupsen/logrus v1.9.3
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)
```

**After:**
```go
require (
github.com/sirupsen/logrus v1.9.3
github.com/wneessen/go-mail v0.4.4
)
```

### 2. API Changes

The public API remains largely compatible, but the internal implementation has been completely rewritten:

#### Email Sending
- **Before**: Used `gomail.NewMessage()` and `gomail.NewDialer()`
- **After**: Uses `mail.NewMsg()` and `mail.NewClient()`
- **Benefit**: Better context support and timeout handling

#### Connection Testing
- **Before**: Basic connection testing only
- **After**: Enhanced testing with proper SMTP handshake verification
- **Benefit**: More reliable connection validation

#### Error Handling
- **Before**: Basic error messages
- **After**: Detailed error context with proper error wrapping
- **Benefit**: Better debugging and troubleshooting

### 3. New Features

1. **Context Support**: All operations now support context with timeouts
2. **Enhanced TLS Configuration**: Better TLS setup and security
3. **Connection Testing**: New `TestSMTPConnection()` method
4. **Structured Logging**: Improved logging with more context
5. **Timeout Management**: Configurable timeouts for all operations

### 4. Breaking Changes

- **Go Version**: Now requires Go 1.21+ (previously 1.20)
- **LocalName**: The `LocalName` field is still supported in the struct but handled automatically by the library
- **Timeout Behavior**: Operations now have enforced timeouts (30s for sending, 15s for testing)

### 5. Backward Compatibility

The following methods maintain full backward compatibility:
- `Mail(to, subject, body, data)` - Main email sending method
- `MailBuffer(to, subject, body, data)` - Alias for Mail() method
- `Mailer` struct fields remain the same
- `Data` struct remains unchanged

## File Structure

```
packages/mailme/
├── go.mod # Updated dependencies
├── mailme.go # Main implementation
├── mailme_test.go # Test suite
├── README.md # Usage documentation
├── MIGRATION.md # This file
└── examples/
└── basic/
├── go.mod # Example dependencies
└── main.go # Usage example
```

## Testing

The package includes comprehensive tests:
- Unit tests for all methods
- Connection testing utilities
- Benchmark tests for performance
- Example usage in the examples directory

To run tests:
```bash
cd packages/mailme
go test -v .
```

## Usage Example

```go
package main

import (
"html/template"
"github.com/supabase/mailme"
)

func main() {
mailer := &mailme.Mailer{
Host: "smtp.gmail.com",
Port: 587,
User: "[email protected]",
Pass: "app-password",
}

subject, _ := template.New("subject").Parse("Welcome {{.Name}}!")
data := mailme.Data{
Name: "User",
Content: map[string]interface{}{
"service": "Supabase",
},
}

err := mailer.Mail(
[]string{"[email protected]"},
subject,
"<h1>Welcome!</h1>",
data,
)
}
```

## Benefits of Migration

1. **Future-proof**: Active development and security updates
2. **Better Performance**: More efficient SMTP handling
3. **Enhanced Security**: Modern TLS and authentication support
4. **Improved Reliability**: Better error handling and recovery
5. **Context Support**: Proper cancellation and timeout handling
6. **Standards Compliant**: Follows modern Go conventions

## Next Steps

1. **Testing**: The implementation has been tested for compilation and API compatibility
2. **Integration**: Can be integrated into the main Supabase Auth system (GoTrue)
3. **Deployment**: Ready for production use with the same API as before
4. **Monitoring**: Enhanced logging provides better observability

## Conclusion

This migration successfully replaces the deprecated go-gomail library with the modern wneessen/go-mail library while maintaining backward compatibility and adding new features. The mailme package is now future-proof and follows modern Go best practices.
149 changes: 149 additions & 0 deletions packages/mailme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Mailme

A modern Go email library for Supabase, built on top of the actively maintained [wneessen/go-mail](https://github.com/wneessen/go-mail) library.

## Overview

This package replaces the deprecated `go-gomail/gomail` library with `wneessen/go-mail`, providing:

- Modern Go standards and idiomatic code
- Active maintenance and security updates
- Better performance and reliability
- Improved context handling and cancellation
- Enhanced TLS and authentication support

## Features

- SMTP email sending with authentication
- HTML email support
- Template-based subject and body rendering
- Connection testing utilities
- Structured logging with logrus
- Context-aware operations with timeouts
- TLS encryption support

## Installation

```bash
go get github.com/supabase/mailme
```

## Usage

### Basic Email Sending

```go
package main

import (
"html/template"
"log"

"github.com/supabase/mailme"
)

func main() {
// Configure mailer
mailer := &mailme.Mailer{
Host: "smtp.gmail.com",
Port: 587,
User: "[email protected]",
Pass: "your-password",
LocalName: "localhost",
}

// Create subject template
subject, err := template.New("subject").Parse("Welcome {{.Name}}!")
if err != nil {
log.Fatal(err)
}

// Prepare email data
data := mailme.Data{
Name: "John Doe",
Content: map[string]interface{}{
"company": "Supabase",
},
}

// Send email
err = mailer.Mail(
[]string{"[email protected]"},
subject,
"<h1>Welcome to Supabase!</h1><p>Hello John, welcome to our platform.</p>",
data,
)
if err != nil {
log.Fatal(err)
}
}
```

### Testing SMTP Connection

```go
mailer := &mailme.Mailer{
Host: "smtp.gmail.com",
Port: 587,
User: "[email protected]",
Pass: "your-password",
}

if err := mailer.TestSMTPConnection(); err != nil {
log.Fatalf("SMTP connection failed: %v", err)
}
```

## Migration from go-gomail

This package maintains API compatibility with the original mailme implementation while using the modern `wneessen/go-mail` library underneath.

### Key Changes

1. **Library Replacement**: `gopkg.in/gomail.v2` → `github.com/wneessen/go-mail`
2. **Enhanced Error Handling**: More detailed error messages and context
3. **Context Support**: All operations now support context for better cancellation
4. **TLS Configuration**: Improved TLS setup and security
5. **Connection Testing**: New utilities for SMTP connectivity testing

### Breaking Changes

- Go 1.21+ required (updated from Go 1.20)
- Some internal error messages may differ
- Connection timeouts are now enforced (30s for sending, 15s for testing)

## Configuration

### Mailer Struct

```go
type Mailer struct {
Host string // SMTP server hostname
Port int // SMTP server port (usually 587 or 465)
User string // SMTP username (usually email address)
Pass string // SMTP password or app password
LocalName string // Local hostname for HELO command (optional)
}
```

### Data Struct

```go
type Data struct {
Name string // Recipient name for templates
Content map[string]interface{} // Additional template variables
}
```

## Dependencies

- [github.com/wneessen/go-mail](https://github.com/wneessen/go-mail) - Modern Go email library
- [github.com/sirupsen/logrus](https://github.com/sirupsen/logrus) - Structured logging

## Contributing

This package is part of the Supabase ecosystem. Please follow the contribution guidelines in the main repository.

## License

MIT License - see the main Supabase repository for details.
13 changes: 13 additions & 0 deletions packages/mailme/examples/basic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module mailme-example

go 1.21

require github.com/supabase/mailme v0.0.0-00010101000000-000000000000

replace github.com/supabase/mailme => ../../

require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/wneessen/go-mail v0.4.4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
Loading