-
-
Notifications
You must be signed in to change notification settings - Fork 875
docs: add documentation page about sending and testing emails #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enginydigital
wants to merge
3
commits into
dunglas:main
Choose a base branch
from
enginydigital:docs/mailer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Sending Emails | ||
|
|
||
| ## In Development: Catching Emails with Mailpit | ||
|
|
||
| When you install [Symfony Mailer](https://symfony.com/doc/current/mailer.html), | ||
| the Symfony Flex recipe automatically registers a [Mailpit](https://mailpit.axllent.org/) | ||
| service in `compose.override.yaml`. | ||
|
|
||
| Mailpit acts as a fake SMTP server: it catches every email sent by your | ||
| application and displays it in a web UI, so no real email is ever delivered | ||
| during development. | ||
|
|
||
| ### Configuring the DSN | ||
|
|
||
| The recipe leaves `MAILER_DSN=null://null` (emails are discarded), so point the | ||
| Mailer to the `mailer` service in `.env` (or `.env.local`): | ||
|
|
||
| ```dotenv | ||
| MAILER_DSN=smtp://mailer:1025 | ||
| ``` | ||
|
|
||
| > [!WARNING] | ||
| > Use the Compose service name (`mailer`), **not** `localhost` or `127.0.0.1`. | ||
| > The PHP application runs in its own container, so `127.0.0.1` refers to the | ||
| > PHP container itself, not to the Mailpit container. Using | ||
| > `smtp://127.0.0.1:1025` results in a "Connection refused" error. | ||
|
|
||
| ### Accessing the Web UI | ||
|
|
||
| By default, the recipe exposes Mailpit's ports without publishing them to | ||
| fixed ports on the host. To find the random port assigned to the web UI, run: | ||
|
|
||
| ```console | ||
| docker compose port mailer 8025 | ||
| ``` | ||
|
|
||
| Alternatively, publish a fixed port by editing the service definition in | ||
| `compose.override.yaml`: | ||
|
|
||
| ```yaml | ||
| ports: | ||
| - "1025" | ||
| - "8025:8025" | ||
| ``` | ||
|
|
||
| The web UI is then available on `http://localhost:8025`. | ||
|
|
||
| ### Testing | ||
|
|
||
| Send a test email from inside the PHP container: | ||
|
|
||
| ```console | ||
| docker compose exec php bin/console mailer:test you@example.com | ||
| ``` | ||
|
|
||
| The email appears instantly in the Mailpit web UI. | ||
|
|
||
| ## In Production | ||
|
|
||
| The Mailpit service is defined in `compose.override.yaml` only, which is not | ||
| used in production (see the [deployment documentation](production.md)). | ||
| Consequently, nothing intercepts emails in production: you must configure a | ||
| real transport. | ||
|
|
||
| Set the `MAILER_DSN` environment variable to point to your SMTP server or | ||
| email provider when starting the stack: | ||
|
|
||
| ```console | ||
| MAILER_DSN=smtp://user:pass@smtp.example.com:587 \ | ||
| SERVER_NAME=your-domain-name.example.com \ | ||
| APP_SECRET=ChangeMe \ | ||
| CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey \ | ||
| docker compose -f compose.yaml -f compose.prod.yaml up --wait | ||
| ``` | ||
|
|
||
| Symfony Mailer also provides [third-party transports](https://symfony.com/doc/current/mailer.html#using-a-3rd-party-transport) | ||
| (Mailtrap, Amazon SES, Brevo, Mailgun, Postmark, Resend...) installable as | ||
| separate Composer packages, which are usually more reliable than a raw SMTP | ||
| connection. | ||
|
|
||
| > [!TIP] | ||
| > If emails are sent asynchronously through Messenger, remember that they are | ||
| > delivered by the worker consuming the `async` transport, not by the web | ||
| > container. Make sure the worker has access to the same `MAILER_DSN`. | ||
|
|
||
| ## On Staging Environments | ||
|
|
||
| Reusing Mailpit on a staging server is a cheap safety net: by pointing the | ||
| staging `MAILER_DSN` to a Mailpit instance, you can test your application | ||
| with production-like data without any risk of sending real emails to real | ||
| users, while still being able to inspect every message in the web UI. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not done by the recipe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.