1

I'm just wondering is it possible to set different global senders for each smtp transport in Symfony Mailer component.

Here is the problem: I have configured 2 different transports:

framework:  
    mailer:
        envelope:
            sender: 'first@example.pl'
        headers:
            from: 'first@example.pl'
        transports:
            first: '%env(MAILER_FIRST)%'
            second: '%env(MAILER_SECOND)%'

And now when I'm using first mailer transport it works just fine

    $email = (new Email())
        ->to('myemail@test.test')
        ->subject('Sample mail subject')
        ->html('<p>See or Symfony Mailer!</p>')         
    ;      
    $mailer->send($email);

And also I can change "from" address manually:

    $email = (new Email())
        ->from('newemail@test.test)
        ->to('yemail@test.test')
        ->subject('Sample mail subject')
        ->html('<p>See or Symfony Mailer!</p>')         
    ;      
    $mailer->send($email);

But when I'm switching to my second transport I got errors about smtp ownership, something like this:

Sender address rejected: not owned by user ....".

Here is code for second transport:

    $email = (new Email())
        ->to('yemail@test.test')
        ->subject('Sample mail subject')
        ->html('<p>See or Symfony Mailer!</p>')         
    ;      
  
    $email->getHeaders()->addTextHeader('X-Transport', 'second');
    $email->from('secondemail@test.test');
    $email->sender('secondemail@test.test');
    $mailer->send($email);

It seems like for second transport there is no rewriting of global sender.

Andrii Sukhoi
  • 749
  • 5
  • 14
  • 1
    *I can change "from" address manually*. The "sender" is the address that's written on the envelope and "from" is what goes into the message EG: Sender: Will B sent a message on behalf of From: Andrii Sukhoi. Despite the sender config, the from is required and is being supplied by your `headers` config or explicitly on the message. However, [`EnvolopeListener`](https://github.com/symfony/mailer/blob/5.3/EventListener/EnvelopeListener.php#L46) overrides the [sender on the envelope](https://symfony.com/doc/5.2/reference/configuration/framework.html#sender) during `send()` when config'd. – Will B. Oct 27 '21 at 23:16
  • 1
    You would need to use a custom EventListener similar to the `EnvolopeListener` to change the sender based on the `X-Transport` header in the message headers and remove the `mailer.envelope.sender` config, since its intent is to force the specified sender on any message envelope. – Will B. Oct 27 '21 at 23:24

0 Answers0