Skip to content

[Mailer][DX] Unable to set global sender email #31733

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
BoShurik opened this issue May 30, 2019 · 10 comments
Closed

[Mailer][DX] Unable to set global sender email #31733

BoShurik opened this issue May 30, 2019 · 10 comments

Comments

@BoShurik
Copy link
Contributor

Currently looks like its not possible to set global sender without setting sender in message
I tried to set sender globally with:

Symfony\Component\Mailer\EventListener\EnvelopeListener:
    arguments:
        $sender: '%sender%'

and create message with:

$message = new Email();
$message
    ->to($email)
    ->text('Hello world')
;

but got Cannot send message without a valid envelope. (Unable to determine the sender of the message.) because MessageEvent dispatched after SmtpEnvelop created in AbstractTransport

so I need to fill sender with a dummy email:

$message = new Email();
$message
    ->from('noreply@example.com')
    ->to($email)
    ->text('Hello world')
;
@chalasr chalasr added the Mailer label May 30, 2019
@chalasr
Copy link
Member

chalasr commented May 30, 2019

Probably related to the fact that the framework integration of the component still needs to be added #31592 (comment)

@weaverryan
Copy link
Member

It may be related to some missing framework integration, but on a component level, due to the fact that the event is dispatched after the "from" sanity check (https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Mailer/Transport/AbstractTransport.php#L60-L69) - it's not possible to create messages without a from and then using a listener to add it. Or said differently, even if EnvelopeListener were registered automatically as a subscriber and we allowed users to pass a from via config, you would still get this error.

I think that SmtpEnvelope might need to allow for the sender to be missing initially... perhaps moving the exception to getSender(): https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Mailer/SmtpEnvelope.php#L100-L113

@weaverryan
Copy link
Member

weaverryan commented May 30, 2019

Actually, even the proposed solution (of using a dummy email) doesn't work for me. Well, it does correctly set the Sender, but there is not currently a way to globally set the From, except to create your own listener.

@fabpot
Copy link
Member

fabpot commented May 31, 2019

#31774 should fix this issue, can you confirm?

@BoShurik
Copy link
Contributor Author

BoShurik commented May 31, 2019

@fabpot Now I've got

In Message.php line 81:
                                                     
  [Symfony\Component\Mime\Exception\LogicException]  
  An email must have a "From" header.                
   
Exception trace:
 () at /Users/boshurik/Projects/mailer.local/vendor/symfony/mime/Message.php:81
 Symfony\Component\Mime\Message->getPreparedHeaders() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mime/Message.php:124
 Symfony\Component\Mime\Message->toIterable() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mime/RawMessage.php:49
 Symfony\Component\Mime\RawMessage->toIterable() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mailer/Transport/Smtp/Stream/AbstractStream.php:93
 Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream::replace() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php:157
 Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->doSend() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mailer/Transport/AbstractTransport.php:81
 Symfony\Component\Mailer\Transport\AbstractTransport->send() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php:115
 Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->send() at /Users/boshurik/Projects/mailer.local/vendor/symfony/mailer/Mailer.php:38
 Symfony\Component\Mailer\Mailer->send() at /Users/boshurik/Projects/mailer.local/src/Command/InitCommand.php:93
 App\Command\InitCommand->execute() at /Users/boshurik/Projects/mailer.local/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /Users/boshurik/Projects/mailer.local/vendor/symfony/console/Application.php:939
 Symfony\Component\Console\Application->doRunCommand() at /Users/boshurik/Projects/mailer.local/vendor/symfony/framework-bundle/Console/Application.php:87
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /Users/boshurik/Projects/mailer.local/vendor/symfony/console/Application.php:273
 Symfony\Component\Console\Application->doRun() at /Users/boshurik/Projects/mailer.local/vendor/symfony/framework-bundle/Console/Application.php:73
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /Users/boshurik/Projects/mailer.local/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /Users/boshurik/Projects/mailer.local/bin/console:40

@fabpot
Copy link
Member

fabpot commented May 31, 2019

@BoShurik Can you share a small project with me so that I can reproduce on my side?

@BoShurik
Copy link
Contributor Author

@fabpot
Copy link
Member

fabpot commented May 31, 2019

@BoShurik You don't have a From header in your example, so the error message makes sense. There is a difference between the Sender and the From header, they are distinct. If you want to have the same From for all messages, you need to register a MessageListener.

@BoShurik
Copy link
Contributor Author

@fabpot thanks. Now it works. As I understand, this common task will be solved by FrameworkBundle in future? Now I have to write own MessageListener as bundled is harder to add to the container

@fabpot
Copy link
Member

fabpot commented May 31, 2019

Right, you will soon be able to configure it without registering the listener yourself.

@fabpot fabpot closed this as completed Jun 4, 2019
fabpot added a commit that referenced this issue Jun 4, 2019
…sageListener (fabpot)

This PR was merged into the 4.3 branch.

Discussion
----------

[Mailer] Fix the possibility to set a From header from MessageListener

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31733
| License       | MIT
| Doc PR        | n/a

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

f4254e6 [Mailer] fixed the possibility to set a From header from MessageListener
fabpot added a commit that referenced this issue Jul 3, 2019
… from config (Devristo)

This PR was squashed before being merged into the 4.4 branch (closes #32081).

Discussion
----------

[WIP][Mailer] Overwrite envelope sender and recipients from config

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31592 #31733   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

# Description

This MR adds the following configuration, example:

```yaml
# config/packages/mailer.yaml
framework:
  mailer:
    envelope:
      sender: 'sender@example.org'
      recipients: ['redirected@example.org']
```
In turn the `\Symfony\Component\Mailer\EventListener\EnvelopeListener` will be configured to alter the sender and recipient addresses before the message has been sent.

Note: it will only alter the envelope, thus rerouting the message to the correct mailbox. However the message itself will still have the original 'from' and 'to' headers.

# Todos

- [x] Alter configuration and dependency injection
- [x] Create test case
- [ ] Update XML config schema?
- [ ] Doc PR

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

8e0c800 [WIP][Mailer] Overwrite envelope sender and recipients from config
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

5 participants