This document describes how to test email notifications in Dkron using Mailpit.
Mailpit is a modern email testing tool for developers. It runs a fake SMTP server to capture outgoing emails instead of sending them to real recipients. This allows you to:
- Test email functionality without sending real emails
- Inspect email content, headers, and formatting via a web UI
- Verify emails are being sent correctly during development and testing
- View emails in a modern, responsive interface with search capabilities
Mailpit offers several advantages over older tools:
- Actively Maintained: Regular updates and bug fixes
- Modern UI: Clean, responsive interface with better UX
- Fast: Written in Go with excellent performance
- Feature-Rich: Search, filtering, tagging, and more
- Small Footprint: Lightweight Docker image (~15MB compressed)
- API Support: RESTful API for automated testing
The easiest way to run Mailpit is using Docker:
docker run -p 8025:8025 -p 1025:1025 axllent/mailpitThis exposes:
- Port 1025: SMTP server (for your application to send emails)
- Port 8025: Web UI (to view captured emails)
Mailpit is included in the development docker-compose configuration. Start it with:
docker compose -f docker-compose.dev.yml up mailpitOr start the entire development environment including Mailpit:
docker compose -f docker-compose.dev.yml upYou can also install Mailpit directly on your system. See the Mailpit documentation for installation instructions.
To configure Dkron to send emails through Mailpit, use the following SMTP settings:
MailHost: localhost
MailPort: 1025
MailFrom: dkron@example.comNote: Mailpit does not require authentication, so you don't need to set MailUsername or MailPassword.
The email notification tests in dkron/notifier_test.go are configured to use Mailpit by default. The tests automatically verify that emails are correctly sent and received using Mailpit's REST API.
To run the email notification test:
# Make sure Mailpit is running first
docker run -d -p 8025:8025 -p 1025:1025 axllent/mailpit
# Run the test
go test -v -run TestNotifier_sendExecutionEmail ./dkronOr use the convenient Makefile target:
make test-email- Open your browser and navigate to: http://localhost:8025
- The Mailpit web UI will show all emails sent during testing
- Click on any email to view its details, including:
- Subject line
- Recipients (To, Cc, Bcc)
- Email body (HTML and plain text)
- Attachments
- Raw MIME content
- Headers
- Source code
Mailpit's web interface includes:
- Search: Full-text search across all emails
- Filtering: Filter by sender, recipient, subject
- Responsive Design: Works on desktop and mobile
- Dark Mode: Toggle between light and dark themes
- Real-time Updates: Emails appear instantly via WebSocket
The test in dkron/notifier_test.go uses the following configuration:
c := &Config{
MailHost: "localhost",
MailPort: 1025,
MailFrom: "dkron@dkron.io",
MailSubjectPrefix: "[Test]",
}This configuration:
- Connects to Mailpit's SMTP server at
localhost:1025 - Sets the sender address to
dkron@dkron.io - Adds a
[Test]prefix to all email subjects - Uses Mailpit API at
http://localhost:8025to verify email delivery
The test automatically verifies email delivery using Mailpit's REST API:
// The test performs the following verifications:
// 1. Checks that email was received
// 2. Verifies From address is correct
// 3. Verifies To address matches recipient
// 4. Confirms subject contains expected text
// 5. Validates email body contains execution outputThis ensures that emails are not just sent, but actually captured correctly by Mailpit.
If you see connection errors when running tests:
-
Verify Mailpit is running:
docker ps | grep mailpit -
Check that port 1025 is accessible:
nc -zv localhost 1025
-
If using Docker, ensure the ports are properly mapped
- Check the Mailpit web UI at http://localhost:8025
- Verify the test completed successfully without errors
- Check Mailpit logs for any issues:
docker logs <mailpit-container-id>
If port 1025 or 8025 is already in use:
# Find what's using the port
lsof -i :1025
lsof -i :8025
# Or use different ports
docker run -p 8026:8025 -p 1026:1025 axllent/mailpit
# Then update test configuration to use port 1026Mailpit is automatically configured in the GitHub Actions test workflow. The workflow includes Mailpit as a service container, making it available during test execution.
The .github/workflows/test.yml file includes Mailpit as a service:
services:
mailpit:
image: axllent/mailpit
ports:
- 1025:1025
- 8025:8025When tests run in GitHub Actions:
- Mailpit starts automatically as a service container
- The SMTP server is available at
localhost:1025 - Tests can send emails without any additional configuration
- No secrets or credentials are needed
Since GitHub Actions runners don't expose web UIs, you cannot view the Mailpit web interface during CI runs. However, you can:
- Verify emails are sent successfully by checking test results
- Tests automatically use Mailpit's API to verify email content
- Email assertions include subject, sender, recipient, and body content
To ensure your tests will pass in GitHub Actions:
# Start Mailpit locally with the same configuration as CI
docker run -p 1025:1025 -p 8025:8025 axllent/mailpit
# Run the full test suite
go test -v -timeout 200s ./...Or use the validation script:
./scripts/test-ci-locally.shMailpit provides several advantages for local development and testing:
- Free and Open Source: No account required, runs completely locally
- No External Dependencies: Doesn't require internet connection
- Simple Setup: Single Docker command to get started
- Fast: No network latency from external services
- Privacy: All emails stay on your local machine
- CI/CD Friendly: Easy to integrate into automated testing pipelines
- No Secrets Required: No API keys or credentials needed in CI
Remember that Mailpit is for testing only. For production, configure Dkron with your actual SMTP server credentials:
MailHost: smtp.your-provider.com
MailPort: 587
MailUsername: your-username
MailPassword: your-password
MailFrom: noreply@yourdomain.comThe email notification tests demonstrate how to use Mailpit's API for verification:
resp, err := http.Get("http://localhost:8025/api/v1/messages")resp, err := http.Get(fmt.Sprintf("http://localhost:8025/api/v1/message/%s", messageID))req, err := http.NewRequest("DELETE", "http://localhost:8025/api/v1/messages", nil)
resp, err := http.DefaultClient.Do(req)See dkron/notifier_test.go for a complete example of API usage in tests.