Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

allow using env variables in transport configuration #159

Merged
merged 1 commit into from
Feb 23, 2017

Conversation

mykiwi
Copy link

@mykiwi mykiwi commented Feb 16, 2017

Example:

# MAILER_URL=transport://user:pass@host:port/?encryption=...&auth_mode=...
swiftmailer:
    url: "%env(MAILER_URL)%"

To do:

@mykiwi mykiwi force-pushed the env-configuration branch 2 times, most recently from 1a70e60 to 69a961d Compare February 17, 2017 09:34
@mykiwi mykiwi changed the title [WIP] Use environment variable [RFR] Use environment variable Feb 17, 2017
// Deprecate url
->beforeNormalization()
->ifTrue(function ($v) {
return is_array($v) && array_key_exists('url', $v);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&& !array_key_exists('dsn', $v)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep this condition to keep the trigger_error anyway, but check if dsn is already set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why renaming the config key to expose the same feature ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

@@ -11,6 +11,7 @@

namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Definition;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be moved below, alpha order in the "DependencyInjection" group

*
* @return string transport
*/
public static function resolveOptions(array &$options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of passing this by reference, I'd make the function return $options, with $options['transport'] always set

*/
public static function resolveOptions(array &$options)
{
if (null === $options['transport']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!isset($options['transport']) now that the code is "public"


if (isset($options['dsn'])) {
$parts = parse_url($options['dsn']);
if (!empty($parts['scheme'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use isset everywhere

->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
;

$options = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not need for this line

;

$options = [];
$container->resolveEnvPlaceholders($mailer, null, $options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/$options/$usedEnvs/

$options = [];
$container->resolveEnvPlaceholders($mailer, null, $options);

if ($options && isset($mailer['dsn'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the && isset($mailer['dsn']) part, isn't it?

->replaceArgument(0, new Reference(sprintf('swiftmailer.mailer.%s.transport', $name)))
;

$enable = !(isset($mailer['disable_delivery']) && $mailer['disable_delivery']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset(foo) && foo is the same as !empty(foo)

if (!empty($query['auth_mode'])) {
$mailer['auth_mode'] = $query['auth_mode'];
}
if (isset($mailer['disable_delivery']) && $mailer['disable_delivery']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty

@mykiwi mykiwi force-pushed the env-configuration branch 3 times, most recently from 19ebd3a to b39da7a Compare February 17, 2017 17:28
@mykiwi mykiwi changed the title [RFR] Use environment variable allow using env variables in transport configuration Feb 17, 2017
@nicolas-grekas
Copy link
Member

LGTM. We should just make this work on 5.3 since that's the minimum version.

@mykiwi mykiwi force-pushed the env-configuration branch 16 times, most recently from 73d2ec3 to e1975b5 Compare February 23, 2017 12:23
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fabpot
Copy link
Member

fabpot commented Feb 23, 2017

Thank you @mykiwi.

@fabpot fabpot merged commit 183b291 into symfony:master Feb 23, 2017
fabpot added a commit that referenced this pull request Feb 23, 2017
…kiwi)

This PR was merged into the 2.5-dev branch.

Discussion
----------

allow using env variables in transport configuration

Example:
```yml
# MAILER_URL=transport://user:pass@host:port/?encryption=...&auth_mode=...
swiftmailer:
    url: "%env(MAILER_URL)%"
```

To do:

- [x] add the doc symfony/symfony-docs/pull/7510
- [x] add tests

Commits
-------

183b291 allow using env variables in transport configuration
@fabpot
Copy link
Member

fabpot commented Feb 28, 2017

While testing this PR, I noticed that 2 configurations cannot be used as env vars: encryption and auth_mode because there are using a validator, which is obviously not compatible with a dynamic value as provided by env vars.

@danrot
Copy link

danrot commented Mar 1, 2017

This breaks some tests for me... We have tests checking if a mail was sent, which is done as described in http://symfony.com/doc/current/email/testing.html.

We've also added a configuration to the config:

swiftmailer:
    disable_delivery: true

But somehow this seems not to be considered anymore. Swiftmailer throws an error at https://github.com/swiftmailer/swiftmailer/blob/c5bd821/lib/classes/Swift/Transport/StreamBuffer.php#L268

I guess this is connected to this PR, because it is working with version 2.4.2 of this bundle, but not anymore with 2.5. Any configuration I am missing now?

@mykiwi
Copy link
Author

mykiwi commented Mar 1, 2017

@fabpot ok, so should we just not manage encryption and auth_mode options (and the gmail transport) from env variable or is it more complex?

@danrot do you use an env variable in your swiftmailer config? If not, I doubt that this PR is the cause of your problem.

@danrot
Copy link

danrot commented Mar 1, 2017

@mykiwi It has to be connected somehow... This PR is the only being merged in 2.5.0.

@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

@mykiwi We have another report for the exact same issue, so it is definitely a problem related to the changes done here. Can you investigate?

@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

Regarding encryption and auth_mode, they cannot be managed (as of today) as env var, but I would let them as is in case it becomes possible in the future.

@danrot
Copy link

danrot commented Mar 2, 2017

It works for me again when I set the configuration as follows:

swiftmailer:
    disable_delivery: true
    transport: "null"

Mind that transport: null is not working. So I guess the issue is somehow related to this line. But I don't have time to further investigate it...

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Apr 28, 2017
…luz)

This PR was merged into the master branch.

Discussion
----------

[SwiftMailer] Update future reference

- [x] Require symfony/swiftmailer-bundle#159

Commits
-------

6aa0117 Removed "encryption" and "auth_mode" from the list of %env()% compatible options
76ce6af Fixed ... again ... the versionadded directives
ef7536c Added the missing "versionadded" directives
635a0ab add mail transport as deprecated
54ad41c Minor tweaks
287b71f update swiftmailer reference
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants