Skip to content

[Mailer] Dynamically Change Transport using getTransport in Mailer Class #32523

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

Closed
alingabrieldm opened this issue Jul 12, 2019 · 1 comment
Closed

Comments

@alingabrieldm
Copy link

alingabrieldm commented Jul 12, 2019

Is it a good idea to introduce the getTransport method into the Mailer class so that it can be modified?

I did this because it seemed to me the easiest way to solve the problem of sending emails via SMTP, using authentication data stored in the database (in my case in Website entities);

// vendor/symfony/mailer/Mailer.php

...
class Mailer implements MailerInterface
{
    ...
    public function getTransport() : TransportInterface {
        return $this->transport;
    }
    ...
}

// and in my service:

public function __construct(
    Symfony\Component\Mailer\MailerInterface $mailer
){
    $this->mailer = $mailer;
}

public function getMailer(Website $website){
    $this->mailer->getTransport()
    ->setUsername($website->getSmtpUsername())
    ->setPassword($website->getSmtpPassword());

    $transport->getStream()
    ->setHost($website->getSmtpHost())
    ->setPort($website->getSmtpPort());

    return $this->mailer;
}
@Koc
Copy link
Contributor

Koc commented Jul 14, 2019

easiest way to solve the problem of sending emails via SMTP, using authentication data stored in the database (in my case in Website entities)

It would be possible by creating custom Transport Factory once #31946 be merged.

Koc added a commit to Koc/symfony that referenced this issue Jul 14, 2019
Koc added a commit to Koc/symfony that referenced this issue Jul 14, 2019
Koc added a commit to Koc/symfony that referenced this issue Jul 16, 2019
@fabpot fabpot closed this as completed Jul 17, 2019
fabpot added a commit that referenced this issue Jul 17, 2019
…stom transports (Koc)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Extract transport factory and allow create custom transports

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes, failure unrelated (master hasn't this PR)
| Fixed tickets | #31385, #32523
| License       | MIT
| Doc PR        | TBD

Alternative approach to allow create custom transports and register DSN for it. Replaces #31931, #31935 . Similar to already existent TansportFactory from Messenger.

TODO:
 - [x] Update changelog
 - [x] Add more tests for factories
 - [x] Add test for configuration + DI extension

Commits
-------

5b9cded Add transport factories (closes #31385, closes #32523)
nicolas-grekas added a commit that referenced this issue Jul 17, 2019
* 4.4:
  [WebProfilerBundle] Remove unneeded information in the routing panel
  Add transport factories (closes #31385, closes #32523)
  [Lock] remove uusage of the StoreInterface
  [HttpClient] make toStream() throw by default
  [Mailer] added XML configuration for the mailer envelope
  added missing test
  [Mailer] Allow register mailer configuration in xml format
  [VarDumper] Allow to configure VarDumperTestTrait casters & flags
  Improve errors when trying to find a writable property
  [Lock] add aliases for LockFactory
  fixed CS
  fix some deprecations and add upgrade instructions
  fix typo
  Added Nl translations
  fixed CS
  [FrameworkBundle] Fix descriptor of routes described as callable array
  [Debug][DebugClassLoader] Include found files instead of requiring them
  [HttpKernel] fix tests
  Adding missing event_dispatcher wiring for messenger.middleware.send_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants