swiftmailer and email form with attachment - beginner

后端 未结 3 417
我在风中等你
我在风中等你 2020-12-31 06:39

As always here is the place where I have learned a lot. And I have now a new things to learn:

I have a html form:

F         


        
3条回答
  •  孤城傲影
    2020-12-31 06:45

    Single Attachment

    My answer is similar to that of André Catita. However, in Laravel 6 you can use $request instead of $_FILES. Let me simplify the code above:

    $path = $request->file('import')->getPathName();
    $fileName = $request->file('import')->getClientOriginalName();      
    
    $message->attach(
            Swift_Attachment::fromPath($path)->setFilename($fileName)
            );
    

    Here I assume that the name of your file tag is import. For eg:

    Multiple Attachments

    Now, lets say instead of single attachment you need multiple attachments. Then the code needs to be changed.

    First your html code will become:

    And for backend or laravel; code will be:

    $files = $request->file('import');
    foreach($files as $file){
    
            $path = $file->getPathName();
            $fileName = $file->getClientOriginalName();      
    
            $message->attach(
                Swift_Attachment::fromPath($path)->setFilename($fileName)
                );
    }
    

提交回复
热议问题