带有文件附件的Laravel表单

时间:2018-02-03 19:58:08

标签: php email-attachments laravel-5.5 laravel-form

我正在尝试使用此页面上的附件来发送电子邮件。我不确定我在这里丢失了什么,我使用了 - > gt; attach()方法,但看起来我的实现可能有误,

请注意:我希望文件属于验证器中可以看到的任何一种类型,然后存储文件并附加副本并发送到to()中的电子邮件

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Mail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use App\Mail\ClientUploadEmail;

class FileUploadController extends Controller
{
    /*
    * Displays a listing of the resource
    *
    *@return \Illuminate\Http\Response
    */
    public function fileUpload()
    {
        return view('home');
    }

    /*
    * Displays a listing of the resource
    *
    *@return \Illuminate\Http\Response
    */

    public function fileUploadPost(Request $request)
    {
        request() -> validate([
            'file' => 'required|file|mimes:jpg,jpeg,png,doc,pdf,svg,gif|max:2048',
        ]);

        $fileName = time().'.'.request()->file->getClientOriginalExtension();
        request()->file->move(public_path('client_uploads'), $fileName);


        $clientupload = [];

        $clientupload['name'] = $request -> get('name');
        $clientupload['phone'] = $request -> get('phone');
        $clientupload['email'] = $request -> get('email');
        $clientupload['subject'] = $request -> get('subject');
        $clientupload['message'] = $request -> get('message');
        $clientupload['file'] = $fileName;

        //send mail logic here
        Mail::to('example@mail.com')
        ->send(new ClientUploadEmail($clientupload));

        flash('Your Message has been sent! If you do not hear back from us within 24hrs, please call: 604 - 503 - 4161') -> success();

        return back();

    }

}

这是我将发送的电子邮件的刀片模板,所以我也尝试按照Laravel文档实现内联附件,但没有成功

<div class="container">
    <h4>Hi,</h4>
        <p>A J. Taylor Registered user has sent you a message with an uploaded file.</p>
    <div class="row">
        <div class="col-md-10">
            <ul class="list-group">
              <li class="list-group-item">Name: {{ $clientupload['name'] }}</li>
              <li class="list-group-item">E-mail: {{ $clientupload['email'] }}</li>
              <li class="list-group-item">Tel: {{ $clientupload['phone'] }}</li>
              <li class="list-group-item">E-mail: {{ $clientupload['subject'] }}</li>
              <img src="{{ $message->embed($pathToFile) }}">
            </ul>

        </div>
    </div>
</div>

最后,这包含我的带有构建功能的上传电子邮件类

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ClientUploadEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
        ->to('example@mail.com')
        ->subject('Message with Upload from Website Client')

        ->view('clientupload.clientfileupload');
        //I tried using this but don't know right way to, so when I do it without /$fileName, it shows a time out (I don't want to increase the  default timeout as well)

        // ->attach('/client_uploads/$fileName');
    }
}

我非常感谢能得到一些帮助。感谢

0 个答案:

没有答案