Can't attach file
solved

By Sined Samed, 2 years ago

Hi there

I have a PDF File in the root directory of my prestashop. Which path do I have to add for E-Mail attachement? I tested so many paths but always: can't attach the file: file doesn't exist.

Any ideas how can I fix this?

By Petr Hucik, 2 years ago

Hi, the easiest way is to use full path. If your www files are stored inside

/var/www/html/

then use path

/var/www/html/file.pdf
By Sined Samed, 2 years ago

Hi thanks for your answer.

When I add it like this... It doesn't work (see Screenshot).

This is the file.pdf which is placed in the prestashop directory. No subfolder or something.

So what else I can try? Also if I add the main link: https://www.welcomebabybox.ch/filde.pdf it doesn't find the file.

Thanks for your support.

All the best

By Sined Samed, 2 years ago

Sorry, I mean this link: https://www.welcomebabybox.ch/file.pdf

By Petr Hucik, 2 years ago

It's possible that your webserver runs in some chroot environment, where the paths are mapped differently.

I have updated the attach file action to support relative filepaths as well. This will be part of next module release.

Meanwhile, you can edit file

modules/conseqs/actions/email-attach-file.php

and change it to look like this:

<?php

namespace Conseqs\Actions;

use Conseqs\Parameters\StringParameterDefinition;
use Conseqs\Action;
use Conseqs\RuntimeModifier;
use Conseqs\ParameterValues;
use Conseqs\ParameterDefinitions;
use PrestaShopException;

class EmailAttachFile extends Action
{
    /**
     * @return string
     */
    public function getName()
    {
        return $this->l('Email: attach file');
    }

    public function getAllowedTriggers()
    {
        return [
            'beforeEmailSent'
        ];
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->l('Attaches custom file to email body');
    }

    /**
     * @return ParameterDefinitions
     */
    public function getSettingsParameters()
    {
        return ParameterDefinitions::none();
    }

    /**
     * @param ParameterValues $settings
     * @return ParameterDefinitions
     */
    public function getInputParameters(ParameterValues $settings)
    {
        return new ParameterDefinitions([
            'filename' => new StringParameterDefinition($this->l('Path to file')),
        ]);
    }


    /**
     * @param ParameterValues $settings
     * @param ParameterValues $input
     * @param ParameterValues $triggerOutput
     * @param RuntimeModifier $runtimeModifier
     * @throws PrestaShopException
     */
    public function execute(ParameterValues $settings, ParameterValues $input, ParameterValues $triggerOutput, RuntimeModifier $runtimeModifier)
    {
        $file = $this->resolveFilePath($input->getValue('filename'));
        $runtimeModifier->adjustParameter('fileAttachment', function($attachments) use ($file) {
            if (! $attachments) {
                $attachments = [];
            }
            if ($attachments && !is_array($attachments)) {
                $attachments = [ $attachments ];
            }
            $attachments[] = [
                'name' => basename($file),
                'content' => file_get_contents($file),
                'mime' => mime_content_type($file)
            ];
            return $attachments;
        });
    }

    /**
     * Returns path to file to be attached
     *
     * @param string $file
     * @return string
     * @throws PrestaShopException
     */
    protected function resolveFilePath($file)
    {
        if (!is_string($file) || !$file) {
            throw new PrestaShopException("Invalid file parameter");
        }

        $candidates = [
            $file,
            rtrim(_PS_ROOT_DIR_, '/') . '/' . ltrim($file, '/'),
            rtrim(_PS_MODULE_DIR_, '/') . '/conseqs/' . ltrim($file, '/'),
            rtrim(_PS_THEME_DIR_, '/') . '/' . ltrim($file, '/'),
        ];
        foreach ($candidates as $candidate) {
            if (file_exists($candidate) && is_file($candidate)) {
                return $candidate;
            }
        }
        throw new PrestaShopException("Can't attach file '$file' to email: file not exists");
    }

}

By Sined Samed, 2 years ago

Big thanks to you. I will try it and give you a feedback.

By Sined Samed, 2 years ago

Thank you very much for your help... now it works great :-) !! Really good Module and Support!