Symfony 5 – Mailer – Attach pdf from assets?

in my symfony 5 project, I want to attach a .pdf to a mail with Mailer.

My pdf file was dropped in the assets, then copied with webpack encore in the public/build folder of my project. It is therefore versioned.

I would like, when sending an email, to be able to retrieve the file to attach it to the email.

I have already tried something like this:


    private $mailer;
    private $twig;
    private $package;

    public function __construct(MailerInterface $mailer, Environment $twig, $publicDir)
    {
        $this->mailer = $mailer;
        $this->twig = $twig;
        $this->package = new Package(new JsonManifestVersionStrategy(sprintf('%s/build/manifest.json', $publicDir)));
    }

        public function sendCustomerSubscriptionCreatedMail(StripeSubscription $subscription)
    {
        //...

        $email = (new TemplatedEmail())
                ->to(new Address($user->getEmail(), $user->getNom()))
                ->subject($subject)
                ->htmlTemplate('stripe/email/customer/customer_subscription_created.html.twig')
                ->context([
                    'subscription' => $subscription,
                ])
                ->attachFromPath($this->package->getUrl('files/manuels/Manuel utilisateurs - Salaries.pdf')); //here is the path for my .pdf files (in public/build/files/manuels/Manuel utilisateurs - Salaries.pdf)

        $this->mailer->send($email);

        return $email;
    }

But I’ve this error :

Path "files/manuels/Manuel utilisateurs – Salaries.pdf" is not
readable.

Can someone help me please ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *