Mailing System
The WebFramework provides a flexible mailing system that supports both synchronous and asynchronous email sending. The system is designed to separate the queuing mechanism from the actual email backend, allowing you to easily switch between different email providers without changing your application code.
Core Components
MailService Interface
The MailService interface defines the contract for mail service implementations. It provides two methods for sending emails:
| PHP | |
|---|---|
MailBackend Interface
The MailBackend interface has the same contract as MailService but represents the actual email sending backend (e.g., Postmark, SMTP). This interface is used by queue handlers to send emails after they've been queued.
| PHP | |
|---|---|
Built-in Implementations
NullMailService
The NullMailService is a no-op implementation that implements both MailService and MailBackend. It's useful for testing or when email functionality is not required. All methods return true without actually sending any emails.
QueuedMailService
The QueuedMailService implements MailService and queues email jobs instead of sending them immediately. This allows you to send emails asynchronously, reducing latency in your main application thread.
When using QueuedMailService, emails are dispatched to the queue system and processed by job handlers that use MailBackend to actually send the emails.
Configuration
Synchronous Email Sending
To send emails synchronously (immediately), bind MailService to your backend implementation:
| PHP | |
|---|---|
Asynchronous Email Sending
To send emails asynchronously (queued), configure both MailService and MailBackend:
Important: When using QueuedMailService, you must also bind MailBackend to your actual email backend implementation. The queue handlers will use MailBackend to send emails, preventing an infinite queuing loop.
Queue Worker Setup
For asynchronous email sending to work, you need to run a queue worker:
| Bash | |
|---|---|
By default, the mail job handlers (RawMailJobHandler and TemplateMailJobHandler) are automatically registered when the QueueService is instantiated in the definitions.php file. If you want to register your own job handlers, you can do so by registering them with the QueueService in your application definitions. Use DI\decorate to register your own job handlers in addition to the default ones, use DI\autowire to register your own job handlers and override the default ones.
| PHP | |
|---|---|
Usage
Injecting MailService
Inject MailService into your classes via constructor injection:
| PHP | |
|---|---|
Sending Raw Emails
Use sendRawMail() to send emails with plain text or HTML content:
Sending Template Emails
Use sendTemplateMail() to send emails using templates configured in your email provider:
UserMailer Helper
The framework provides a UserMailer helper class for common user-related emails:
Available UserMailer methods:
- emailVerificationLink() - Send email verification link
- changeEmailVerificationLink() - Send change email verification link
- passwordReset() - Send password reset email
- newPassword() - Send new password email
Template Configuration
You can override default template IDs used by UserMailer in your configuration:
For available configuration options and default settings, see config/base_config.php.
Default Sender Configuration
Configure the default sender email address:
For available configuration options and default settings, see config/base_config.php.
Error Handling
Both sendRawMail() and sendTemplateMail() return:
- true if the email was sent successfully (or queued successfully when using QueuedMailService)
- A string containing an error message if sending failed
Always check the return value:
| PHP | |
|---|---|
Best Practices
-
Use QueuedMailService for Production: Asynchronous email sending reduces latency and improves user experience. Always use
QueuedMailServicein production environments. -
Separate MailService and MailBackend: When using async sending, keep
MailServicebound toQueuedMailServiceandMailBackendbound to your actual email provider. This prevents infinite queuing loops. -
Handle Errors Gracefully: Always check return values and handle errors appropriately. Failed emails should be logged for debugging.
-
Use Templates When Possible: Template emails are easier to manage and update than raw HTML emails. They also support better personalization.
-
Monitor Queue Workers: Ensure queue workers are running when using
QueuedMailService. Emails won't be sent until workers process the queue. -
Test with NullMailService: Use
NullMailServicein test environments to avoid sending actual emails during testing.
Integration with Queue System
The mailing system integrates seamlessly with the queue system. When using QueuedMailService:
- Emails are dispatched as
RawMailJoborTemplateMailJobto the queue - Queue workers process these jobs using registered handlers
- Handlers use
MailBackendto actually send emails - Failed jobs are retried according to queue configuration
For more information on the queue system, see the Queueing documentation.