This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jefe.sis22208
jefe.sis22208

Hello Guys,

I am beginner and I need some help to solve a issue, I had try to send mail with attach, but, this file that will be attached is load on front end by user, and I dont need to store this file, only send the e-mail with the file attached.

My partial upload.htm code:

==
<form method="post" enctype="multipart/form-data" data-request="onSendCur">
        <input type="file" name="arquivo" id="file-2" class="inputfile"/>
        <button type="submit" class="btn btn-primary">SEND</button>
</form>

My onSendCur function code:

function onSendCur(){
        $vars = [];

        Mail::send('send-curriculum', $vars, function($message) {
            $message->to('any@any.com', 'Curriculum');
            $message->subject('Curriculum');
            $message->attach(Input::file('arquivo'));
        });
}

When I went click on send button, I got the error:

"The path cannot be empty" on line 48 of /home/jeferson/public_html/october-bootstrap4/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php

On httpd logs:

"POST / HTTP/1.1" 500

I tried to get the value of the variable arquivo in several ways, with the post() function, with the global variable $_FILES, with the Input::file('arquivo') method, but in all ways this variable comes null.

Thank you very much Best regards

abel
abel

Try replacing

<form method="post" enctype="multipart/form-data" data-request="onSendCur">

for

{{ form_open({request: 'onSendCur', files: 'true'}) }}

https://octobercms.com/docs/markup/function-form

jefe.sis22208
jefe.sis22208

Hi abel, thank you so much for your reply.

I have tried to use form_open, but the same issue persists.

My solution for a while is using java script on event onchange of input file, and creating a post request using XMLHttpRequest object and specifying a new partial to target. In the new partial I can get the file uploaded by the global variable $_FILES and to send the e-mail.

My JS code:

<script type="text/javascript">
    document.getElementById("arquivo").onchange = function(e){
        if(e.target.files != null && e.target.files.length != 0){
            var arq = e.target.files[0];
            var fd = new FormData();
            fd.append("arquivo", arq);
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState===4 && xmlhttp.status===200)
                    alert(xmlhttp.responseText);
            };
            xmlhttp.open("POST", "upload", true);
            xmlhttp.send(fd);
        }
    }
</script>

Works for me for now.

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.