Skip to content

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

@alingabrieldm

Description

@alingabrieldm

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;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions