Skip to content

[Mailer] Add support for multiple mailers #33409

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

Merged
merged 1 commit into from
Sep 2, 2019

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Sep 1, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets closes #32535
License MIT
Doc PR -

This adds the possibility to define several email transports.

If you only have one email transport, nothing changes:

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

But if you need more than one, use the transports entry instead:

framework:
    mailer:
        transports:
            main: '%env(MAILER_DSN)%'
            important: '%env(MAILER_DSN_IMPORTANT)%'

Then, when sending an email via the Mailer, pass the mailer name explicitly (by default, the first one is used):

// use the first "main" transport
$mailer->send($email);

// or use the "important" one
$email->getHeaders()->addTextHeader('X-Transport', 'important');
$mailer->send($email);

The web profiler now displays the name and the shorten DSN.

@fabpot fabpot force-pushed the mailer-multiple-transports branch 6 times, most recently from 2a9323a to 5dfe3b5 Compare September 1, 2019 16:40
@OskarStark
Copy link
Contributor

You wrote mailgun instead of important in the PR header example code

@fabpot fabpot force-pushed the mailer-multiple-transports branch from 5dfe3b5 to 99786d7 Compare September 1, 2019 17:01
@fabpot
Copy link
Member Author

fabpot commented Sep 1, 2019

@OskarStark fixed :)

@nicolas-grekas nicolas-grekas added this to the next milestone Sep 1, 2019
@ro0NL
Copy link
Contributor

ro0NL commented Sep 2, 2019

by default, the first one is used)

is this convention sensible? should we support an explicit option? i.e.

default_transport: some

@OskarStark
Copy link
Contributor

is this convention sensible? should we support an explicit option? i.e.

Yes much better, doctrine or messenger are doing the same 👍

@fabpot
Copy link
Member Author

fabpot commented Sep 2, 2019

In which case this convention would be a problem? The sort does not matter except for the first one which is the default. Less configuration is better, right?

@ro0NL
Copy link
Contributor

ro0NL commented Sep 2, 2019

The sort does not matter except for the first one which is the default.

so the sorting matters :)

Less configuration is better, right?

yes :) but im not sure we should ignore the configuration part in "convention over configuration" otherwise it's just a magic convention.

@fabpot
Copy link
Member Author

fabpot commented Sep 2, 2019

I fail to see the magic here. The first one is the default one. That's all.

@ro0NL
Copy link
Contributor

ro0NL commented Sep 2, 2019

both work :) it's a doc issue eventually. To me the magic is, updating the ordering (i.e. to "alpha order") might change the default implicitly.

however, that's the case anyway as long as someone relies on the "first is default" convention, without knowing about default_transport.

Let's keep it simple then 👍

@ro0NL
Copy link
Contributor

ro0NL commented Sep 2, 2019

note the messenger section does enforce 100% explicit configuration:

->ifTrue(function ($v) { return isset($v['buses']) && \count($v['buses']) > 1 && null === $v['default_bus']; })
->thenInvalid('You must specify the "default_bus" if you define more than one bus.')

@fabpot fabpot force-pushed the mailer-multiple-transports branch 2 times, most recently from 7483aeb to 695a33a Compare September 2, 2019 10:07
@fabpot fabpot removed the BC Break label Sep 2, 2019
@fabpot
Copy link
Member Author

fabpot commented Sep 2, 2019

New (simpler/better?) implementation without BC breaks.

@fabpot fabpot force-pushed the mailer-multiple-transports branch 2 times, most recently from 57faca6 to ecc7898 Compare September 2, 2019 10:18
@fabpot fabpot force-pushed the mailer-multiple-transports branch from ecc7898 to de5fae4 Compare September 2, 2019 10:19
fabpot added a commit that referenced this pull request Sep 2, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Add support for multiple mailers

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | closes #32535
| License       | MIT
| Doc PR        | -

This adds the possibility to define several email transports.

If you only have one email transport,  nothing changes:

```yaml
framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'
```

But if you need more than one, use the `transports` entry instead:

```yaml
framework:
    mailer:
        transports:
            main: '%env(MAILER_DSN)%'
            important: '%env(MAILER_DSN_IMPORTANT)%'
```

Then, when sending an email via the `Mailer`, pass the mailer name explicitly (by default, the first one is used):

```php
// use the first "main" transport
$mailer->send($email);

// or use the "important" one (`null` is the envelope)
$mailer->send($email, null, 'important');
```

The web profiler now displays the name and the shorten DSN.

Commits
-------

de5fae4 [Mailer] Add support for multiple mailers
@fabpot fabpot merged commit de5fae4 into symfony:4.4 Sep 2, 2019
@fabpot fabpot deleted the mailer-multiple-transports branch September 6, 2019 10:41
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Oct 6, 2019
This PR was squashed before being merged into the 4.4 branch (closes #12422).

Discussion
----------

[Mailer] Add support for multiple mailers

#12259 and symfony/symfony#33409

~~Do we need a `versionadded:: 4.4`?~~

![image](https://user-images.githubusercontent.com/1073881/66234058-05c5d680-e6ed-11e9-963f-d2a25c508aee.png)

Commits
-------

aeedbd2 [Mailer] Add support for multiple mailers
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Mailer] Can create many mailers like in SwiftMailer
7 participants