Symfony Mailer in Slim4

Hello,

I try to implement Symfony Mailer to work with contact form. Everything Ok (user’s name, email, phone, message are sent). But until attempting to send attached file. The contact form doesn’t work (the program hangs).
I tried to find a solution, but failed.
What is going wrong?
Thank you in advance.

<?php

namespace App\Action;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;


final class MailerAction
{

    private $mailer;

    public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }

    public function __invoke(ServerRequestInterface $request,
        ResponseInterface $response): ResponseInterface 
    {

        // getting user's inputs (name, email, phone, message, 
		// file) via AJAX jQuery
		$arr = $request->getParsedBody();
		
        // ....

        // file attached
		$file = $arr["user_file"];
		
		// sanitizing and validating user's inputs

		// ....

        $messageBody = "<b>Name:</b> ".$name."<br><b>Phone:</b> ".$phone."<br><b>E-mail:</b> ".$email."<br><b>Message:</b> ".$msg;
        
        $mail = new Email();
        $mail->from('name@mail.com')
            ->to('name@mail.com')
            ->subject('OnLine Order')
            ->html($messageBody);

        // attachment
        if (!empty($file['name'][0])) {           
            $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $file['name']));
            $filename = $file['name'];

            if (move_uploaded_file($file['tmp_name'], $uploadfile)) {    
                $mail->attachFromPath($uploadfile, $filename);          
            }else {
                $note = array('success' => false, 'message' => 'No file!');
                $response->getBody()->write(json_encode($note));
                return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
            }
        }else {
            $note = array('success' => false, 'message' => 'Add file!');
            $response->getBody()->write(json_encode($note));
            return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
        }

        try {
            $this->mailer->send($mail);
            $note = array('success' => true, 'message' => 'Thank you. Your order has sent.');
            $response->getBody()->write(json_encode($note));
            return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
        } catch (Exception $exception) {
            $note = array('success' => false, 'message' => "Mailer Error!");
            $response->getBody()->write(json_encode($note));
            return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
        }
	}
}

It looks like you are sending the file via Ajax so you don’t need move_uploaded_file.
What is the value of $file = $arr["user_file"];?
Does it contain an absolute path from the server? Which would be a security issue.

If you have the file content as string or stream you could alternatively use the attach() method to attach contents from a stream:

$stream = fopen('php://memory','r+');
fwrite($stream, $string);
rewind($stream);

$email = (new Email())
    // ...
    ->attach($stream))
;

Thank you, @odan, for your prompt reply.
To clarify some things I present some more code.

order.twig
{% extends "base.twig" %}

{% block content %}

{# ... #}

    <form id="form_order" method='post' enctype="multipart/form-data">

        <div class="uk-margin">

            <input id='form_order_name' name='form_order_name' class="uk-input" type="text" placeholder="Name">

        </div>

        <div class="uk-margin">

            <input id='form_order_phone' name='form_order_phone' class="uk-input" type="phone" placeholder="Phone">

        </div>

        <div class="uk-margin">

            <input id='form_order_email' name='form_order_email' class="uk-input" type="email" placeholder="E-mail">

        </div>

        <div class="uk-margin">

            <textarea id='form_order_message' name='form_order_message' class="uk-textarea" rows="3" style='resize:none;' placeholder="Message..."></textarea>

        </div>

        <div class="uk-margin" uk-margin>

            <div uk-form-custom="target: true">

                <input id='form_order_file' name='form_order_file' type="file">

                <input class="uk-input uk-form-width-medium" type="text" placeholder="Add file..." disabled>

            </div>

        </div>

    </form>           

    <button id='form_order_btn' class="uk-button uk-button-primary uk-border-rounded uk-margin-medium-right" type="button">Submit</button>

    <div id='form_order_spinner' style='display:none' uk-spinner></div>

    <div id='form_order_response' class="uk-margin-top"></div>

{# ... #}

<script src='public/js/order.js'></script>

{% endblock content %}

order.js

$(function() {

    

    $("#form_order_btn").click(function(e) {

        e.preventDefault();

        $("#form_order_btn").prop("disabled", true);

        $('#form_order_spinner').show();

        

        var m_data = new FormData();

        m_data.append( 'user_name', $('input[name=form_order_name]').val());

        m_data.append( 'user_phone', $('input[name=form_order_phone]').val());

        m_data.append( 'user_email', $('input[name=form_order_email]').val());

        m_data.append( 'user_msg', $('textarea[name=form_order_message]').val());

        m_data.append( 'user_file', $('input[name=form_order_file]')[0].files[0]);      

        

        $.ajax({

          type: 'POST',

          url: './mailer',

          dataType: 'json',

          data: m_data,

          processData: false,

          contentType: false,

          success: function(response) {

             //load json data from server and output message   

            if(response.success == false){

                output = "<p class='uk-text-danger uk-text-bold'><span class='uk-margin-small-right' uk-icon='icon: warning'></span>"+response.message+"</p>";

            }else {

                output = "<p class='uk-text-success uk-text-bold'><span class='uk-margin-small-right' uk-icon='icon: happy'></span>"+response.message+"</p>";

                $('#form_order_response').html('');

            }

            $('#form_order_spinner').hide();

            $('#form_order_response').html(output);

            $("#form_order_btn").prop("disabled", false);

          }

        });

    });

    

});

Thank you in advance

It looks like you are not sending the filename.

You should read this (developer.mozilla.org/en-US/docs/Web/API/FormData/append) the formData(); append method has an optional third parameter for a file.