|
| 1 | +# Asynchronous Email Sending in phpList |
| 2 | + |
| 3 | +This document explains how to use the asynchronous email sending functionality in phpList. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +phpList now supports sending emails asynchronously using Symfony Messenger. This means that when you send an email, it is queued for delivery rather than being sent immediately. This has several benefits: |
| 8 | + |
| 9 | +1. **Improved Performance**: Your application doesn't have to wait for the email to be sent before continuing execution |
| 10 | +2. **Better Reliability**: If the email server is temporarily unavailable, the message remains in the queue and will be retried automatically |
| 11 | +3. **Scalability**: You can process the email queue separately from your main application, allowing for better resource management |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +The asynchronous email functionality is configured in `config/packages/messenger.yaml` and uses the `MESSENGER_TRANSPORT_DSN` environment variable defined in `config/parameters.yml`. |
| 16 | + |
| 17 | +By default, the system uses Doctrine (database) as the transport for queued messages: |
| 18 | + |
| 19 | +```yaml |
| 20 | +env(MESSENGER_TRANSPORT_DSN): 'doctrine://default?auto_setup=true' |
| 21 | +``` |
| 22 | +
|
| 23 | +You can change this to use other transports supported by Symfony Messenger, such as: |
| 24 | +
|
| 25 | +- **AMQP (RabbitMQ)**: `amqp://guest:guest@localhost:5672/%2f/messages` |
| 26 | +- **Redis**: `redis://localhost:6379/messages` |
| 27 | +- **In-Memory (for testing)**: `in-memory://` |
| 28 | + |
| 29 | +## Using Asynchronous Email Sending |
| 30 | + |
| 31 | +### Basic Usage |
| 32 | + |
| 33 | +The `EmailService` class now sends emails asynchronously by default: |
| 34 | + |
| 35 | +```php |
| 36 | +// This will queue the email for sending |
| 37 | +$emailService->sendEmail($email); |
| 38 | +``` |
| 39 | + |
| 40 | +### Synchronous Sending |
| 41 | + |
| 42 | +If you need to send an email immediately (synchronously), you can use the `sendEmailSync` method: |
| 43 | + |
| 44 | +```php |
| 45 | +// This will send the email immediately |
| 46 | +$emailService->sendEmailSync($email); |
| 47 | +``` |
| 48 | + |
| 49 | +### Bulk Emails |
| 50 | + |
| 51 | +For sending to multiple recipients: |
| 52 | + |
| 53 | +```php |
| 54 | +// Asynchronous (queued) |
| 55 | +$emailService->sendBulkEmail($recipients, $subject, $text, $html); |
| 56 | +
|
| 57 | +// Synchronous (immediate) |
| 58 | +$emailService->sendBulkEmailSync($recipients, $subject, $text, $html); |
| 59 | +``` |
| 60 | + |
| 61 | +## Testing Email Sending |
| 62 | + |
| 63 | +You can test the email functionality using the built-in command: |
| 64 | + |
| 65 | +```bash |
| 66 | +# Queue an email for asynchronous sending |
| 67 | +bin/console app:send-test-email recipient@example.com |
| 68 | +
|
| 69 | +# Send an email synchronously (immediately) |
| 70 | +bin/console app:send-test-email recipient@example.com --sync |
| 71 | +``` |
| 72 | + |
| 73 | +## Processing the Email Queue |
| 74 | + |
| 75 | +To process queued emails, you need to run the Symfony Messenger worker: |
| 76 | + |
| 77 | +```bash |
| 78 | +bin/console messenger:consume async_email |
| 79 | +``` |
| 80 | + |
| 81 | +For production environments, it's recommended to run this command as a background service or using a process manager like Supervisor. |
| 82 | + |
| 83 | +## Monitoring |
| 84 | + |
| 85 | +You can monitor the queue status using the following commands: |
| 86 | + |
| 87 | +```bash |
| 88 | +# View the number of messages in the queue |
| 89 | +bin/console messenger:stats |
| 90 | +
|
| 91 | +# View failed messages |
| 92 | +bin/console messenger:failed:show |
| 93 | +``` |
| 94 | + |
| 95 | +## Troubleshooting |
| 96 | + |
| 97 | +If emails are not being sent: |
| 98 | + |
| 99 | +1. Make sure the messenger worker is running |
| 100 | +2. Check for failed messages using `bin/console messenger:failed:show` |
| 101 | +3. Verify your mailer configuration in `config/parameters.yml` |
| 102 | +4. Try sending an email synchronously to test the mailer configuration |
0 commit comments