mjml-bundle: Can't make it work with symfony on the same docker container

I’m working on a symfony project on docker container and i set up a Mailer service.I installed binaries on the container with npm install mjml. In the code :

$mail = new \Swift_Message();
$mail
     ->setFrom($from)
     ->setTo($to)
     ->setSubject($subject)
     ->setBody($body)
     ->setContentType('text/mjml');
$this->mailer->send($mail);

The thing is that when i request the function which send the mail via the docker container, there is no error but it does not send the mail. But when i start a server with php -S adress -t public and i request the function which send the mail, the mail is sent.In the log for the container there is could not found mjml binary but when i enter in the container the mjml command works

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

not sure if this problem relate here, I did not use docker but I bumped here when facing following problem. Hope it help someone (and maybe code modif suggestion below?)

Mac Catalina 10.15.4 Symfony 5.1

Problem Running these lines work well on CLI

which node
which mjml

Check my config :

# config/packages/mjml.yaml
...
binary: '%kernel.project_dir%/node_modules/.bin/mjml'
...

BUT when accessing url (e.g localhost/send-email)

The command """ '/Users/user/.nvm/versions/node/v10.16.0/bin/mjml' '--version'" failed.

Exit Code: 127(Command not found)

Working directory: xxx

Output:
================

Error Output:
================
env: node: No such file or directory

Yeah I know the mjml path is different from the config coz I reinstall it using npm install -global mjml Please read below on ‘Try 2’

Looking at the error, I figured the error happened here BinaryRenderer::getMjmlVersion()

In search of solution : Try 1 Put this line right above $process = new Process putenv('PATH='.$path.':/usr/local/bin');

var_dump-ing it produces ‘PATH’ => ‘/usr/bin:/bin:/usr/sbin:/sbin’ on browser screen (different from echo $PATH run in Terminal)

Try 2 Look for node and mjml binary in /usr/local/bin , /usr/bin but did not find any. Not sure if this affect anything : uninstall mjml then install it using -global Then, tried to create symlink of node to /usr/bin

sudo ln -s /Users/user/.nvm/versions/node/v10.16.0/bin/node /usr/bin/node

But Operation not permitted (apple…)

Try 3 Tried to put env var here /System/Library/LaunchDaemons/org.apache.httpd.plist but again, Catalina put this as read only

Doing csrutil disable does not resolve the issue. I could not change the file, nor create symlink 😦

Last Resort No choice, I have to modify the code. Please let me know if this is not at all best practice but this has helped me

(Worth to note that the problem above does NOT happen in CentOS 7)

BinaryRenderer

...
public function getMjmlVersion(): int
{ 
...
$process = new Process([
                $this->node, // add this
                $this->bin,
                '--version',
...

Configuration

...
->arrayNode('options')
...
                        ->scalarNode('node_path') // add this
                            ->defaultValue(function () use ($finder) {
                                return $finder->find('mjml');
                            })
                            ->info('Path to the binary')
                        ->end()
                        ->scalarNode('binary')
...

MJMLExtension

...
if ('binary' === $config['renderer']) {
            $rendererDefinition = new Definition(BinaryRenderer::class);
            $rendererDefinition->addArgument($config['options']['node_path']); // add this
            $rendererDefinition->addArgument($config['options']['binary']);
...

So now I could have full path of node like so : mjml.yaml

mjml:
    renderer: 'binary'
    options:
        node_path: '/Users/user/.nvm/versions/node/v10.16.0/bin/node' # or '%kernel.project_dir%/node_modules/.bin/node' if you prefer
        binary: '/Users/user/.nvm/versions/node/v10.16.0/bin/mjml' # or '%kernel.project_dir%/node_modules/.bin/mjml' if you prefer
        minify: false
        validation_level: skip

Same issue : https://stackoverflow.com/questions/59004529/run-mjml-cli-using-symfony-process