Skip to content

[Mailer] add File transport #31947

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
wants to merge 1 commit into from
Closed

[Mailer] add File transport #31947

wants to merge 1 commit into from

Conversation

Kocal
Copy link
Member

@Kocal Kocal commented Jun 8, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #31819
License MIT
Doc PR symfony/symfony-docs#11702

This PR add the file transport to the Mailer component which is useful when running integrations tests.

I've taken this idea from SwiftMailer, but without the spool behavior. It just saves messages to the filesystem and nothing else.

It's supposed to be used like this:

# .env.test
MAILER_URL=file://null?path=%kernel.project_dir%/var/emails

Sent message will now be saved in the directory %kernel.project_dir%/var/emails, in a .message file.

What do you think about it?
Thanks!

@Kocal
Copy link
Member Author

Kocal commented Jun 8, 2019

Hum, I'm not sure how to make it working on Windows since file://C:\Users\appveyor\AppData\Local\Temp\1/symfony/emails is not a valid DNS. 😕

Any ideas?

@Kocal
Copy link
Member Author

Kocal commented Jun 21, 2019

Status: Needs review

@Kocal
Copy link
Member Author

Kocal commented Jun 27, 2019

PR has been rebased and conflicts resolved.
However I had to remove a test from #31993 because it tried to create the directory /some/path (which fails), and I don't know any valid scheme that let me not specify a host. :/

@Kocal
Copy link
Member Author

Kocal commented Jun 27, 2019

Travis/AppVeyor failures are unrelated I think.

@Kocal
Copy link
Member Author

Kocal commented Jul 1, 2019

Status: Needs review

I've just updated the way we pass a path in order to be compatible with Windows, since file://C:\path/to/symfony-project/var/emails is not a valid DSN.
Now we use path query parameter. WDYT?

Before:

MAILER_URL=file://%kernel.project_dir%/var/emails

After:

MAILER_URL=file://null?path=%kernel.project_dir%/var/emails

Travis & AppVeyor failures are still unrelated.

@Kocal
Copy link
Member Author

Kocal commented Jul 17, 2019

I've resolved conflicts and squashed everything.
It should be good now.

EDIT: this travis failure seems to be unrelated.

@Koc
Copy link
Contributor

Koc commented Jul 21, 2019

Nice to see that you've adopted PR to changes introduced in #31946 .

I've just updated the way we pass a path in order to be compatible with Windows, since file://C:\path/to/symfony-project/var/emails is not a valid DSN. Now we use path query parameter. WDYT?

You should use urls like file:///c:/windows/My%20Documents%20100%2520/foo.txt, it correctly parsed.

protected function doSend(SentMessage $message): void
{
$file = $this->path.'/'.uniqid().'.message';
$serializedMessage = serialize($message);
Copy link
Contributor

Choose a reason for hiding this comment

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

How integration test will looks like? Why not save in .eml format?

Copy link
Member Author

@Kocal Kocal Jul 21, 2019

Choose a reason for hiding this comment

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

I didn't know about .eml files. In my project, we exposed a endpoint only available in dev/test env that list *.message file and which display message details like from, to, subject and body. It was consumed by Cypress and it worked well.

But if it can be easily parsed with PHP or JS, why not.

@Kocal
Copy link
Member Author

Kocal commented Jul 21, 2019

You should use urls like file:///c:/windows/My%20Documents%20100%2520/foo.txt, it correctly parsed.

Indeed that better, but it means there is no actual solution to make it usable in Windows with a symfony parameter like this MAILER_URL=file://%kernel.project_dir%/var/emails? 🤔

fabpot added a commit that referenced this pull request Aug 4, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Add message events logger

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| 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 | refs #31592, refs #32409, refs #31947, refs #31747
| License       | MIT
| Doc PR        | n/a

To allow testing emails and for the web profiler, we need a way to store all sent/queued messages.

Commits
-------

7642178 [Mailer] added message events logger
@fabpot
Copy link
Member

fabpot commented Aug 4, 2019

I think that testing emails is orthogonal to the transport. See #32930 for an implementation that allows to receive the email AND test it at the same time in functional tests.

@fabpot fabpot closed this Aug 4, 2019
@Kocal
Copy link
Member Author

Kocal commented Aug 5, 2019

If you use PHPUnit for functional tests yes, but I'm actually using Cypress (JavaScript) for E2E tests (like a password-reset functionality + email).

That's why it was nice to have emails persisted in the filesystem (or somewhere else in fact), with a Symfony controller for displaying sent emails, Cypress were actually able to see them (and click on a button inside the mail, for example).

But since #31946 has been merged, I will be able to re-implement the file transport.

fabpot added a commit that referenced this pull request Aug 5, 2019
…for the Mailer (fabpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer][Mime] Add PHPUnit constraints and assertions for the Mailer

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| 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 | refs #31592, closes #32409, closes #31947, closes #31747, closes #30850
| License       | MIT
| Doc PR        | n/a

This PR introduces PHPUnit constraints and assertions to ease testing emails in functional tests:

```php
<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MailerAssertionsTraitsTest extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        $client->request('GET', '/');

        $this->assertEmailCount(2);
        $this->assertEmailIsQueued($this->getMailerEvent(0));

        $email = $this->getMailerMessage(0);
        $this->assertEmailHasHeader($email, 'To');
        $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
        $this->assertEmailTextBodyContains($email, 'Bar');
        $this->assertEmailHtmlBodyContains($email, 'Foo');
        $this->assertEmailAttachementCount($email, 1);
    }
}
```

Commits
-------

23f237b added PHPUnit constraints and assertions for the Mailer
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants