Skip to content
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

Path is not readable - mime\Part\TextPart checks if a directory is a directory and then throws #54767

Closed
KevsRepos opened this issue Apr 29, 2024 · 5 comments

Comments

@KevsRepos
Copy link

Symfony version(s) affected

6.3

Description

Error is thrown that /var/www/html/foo/bar is not readable when trying to attach a file to an email.

How to reproduce

Using symfonys mailer to create a mail and attach a file:

		$mail = new Mail();

		$mail->from('[email protected]');
		$mail->to('[email protected]');

		$mail->attach($myFileToAttach); //$myFileToAttach = \Component\Mime\Part\File

symfony will do as follows:

  1. call attach method from Symfony\Component\Mime
  2. call $this->addPart with Symfony\Component\Mime\Part\DataPart as its argument
  3. causing the constructor of DataPart
  4. DataPart constructs its parent TextPart
  5. TextPart then does something confusing:
        if ($body instanceof File) {
            $path = $body->getPath();
            if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
                throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
            }
        }

It gets the containing directory into $path and then checks if this path is a directory, which it just logically has to be and then the error is thrown.

Possible Solution

Dont check if a directory is a directory, please check instead if a file is a directory.

        if ($body instanceof File) {
            $path = $body->getPath() . $body->getFilename(); // getPath() will just return the path to the file, excluding the file itself.
            if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
                throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
            }
        }

Additional Context

No response

@xabbuh
Copy link
Member

xabbuh commented Apr 29, 2024

Symfony 6.3 does not receive bugfixes anymore. If you still see the same behaviour with Symfony 6.4 or higher, please create a small example application that allows to reproduce it.

@KevsRepos
Copy link
Author

Symfony 6.3 does not receive bugfixes anymore. If you still see the same behaviour with Symfony 6.4 or higher, please create a small example application that allows to reproduce it.

It still behaves the same in the latest version. Here is a reproduction: https://github.com/KevsRepos/symfony-mime-bug-reproduction

I would really like to know if I am just doing something wrong, or if this really doesnt make any sense.

@xabbuh
Copy link
Member

xabbuh commented Apr 29, 2024

I think there is a misunderstanding. The file path must be the full path including the base filename. The (optional) filename argument is there to be able to use independent filenames for how the file is stored to what is presented to the recipient of the e-mail.

@KevsRepos
Copy link
Author

That's very misleading, but you're right. Thanks. I think the class signature should clarify that it is expecting the full path including the file name.

/**
 * @param string $fullPath must have file name included
 * @param ?string $newFileName name to use for e-mail attachments
 * /

@xabbuh
Copy link
Member

xabbuh commented Apr 30, 2024

PR welcome to clarify this

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2024
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

3 participants