Livewire 图片上传电子邮件附件

时间:2021-07-14 18:16:09

标签: php laravel laravel-livewire

我有一个 livewire 表单,我将图像上传到该表单然后附加到电子邮件中,因此每次都是新图像。除了图像附件外,一切都与表格和电子邮件完美配合。目前,它指向本地存储路径并通过电子邮件发送一个空图像文件,该文件“无法打开,因为文件为空”我对 LaravelPHP 还很陌生。任何见解表示赞赏。

这是我的 livewire 组件。

<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Mail;
use Livewire\WithFileUploads;

class ContactForm extends Component {
    use WithFileUploads;

    public $photo;
    public function save() {
        $this->validate([
            'photo' => 'image|max:1024' // 1MB Max
        ]);
        $this->photo->storeAs('photos');  
    }

    public $make;
    public $year;
    public $name;
    public $email;
    public $zip;
    public $phone;
    public $success;
    protected $rules = [
        'make' => 'required',
        'year' => 'required',
        'name' => 'required',
        'email' => 'required|email',
        'zip' => 'required|min:5',
        'phone' => 'required',
        'photo' => 'image|max:1024'
    ];

    public function submit() {
        $contact = $this->validate();
        $this->photo = $this->photo->store('photos');

        Mail::send('email',
            array(
                'make' => $this->make,
                'year' => $this->year,
                'name' => $this->name,
                'email' => $this->email,
                'zip' => $this->zip,
                'phone' => $this->phone,
                'photo' => $this->photo,
            ),
            function($message) {
                $message->from('lead@butlersglass.com');
                $message->to('alex@navsolo.com', 'Alex')->subject('Your Site Contact Form');
                $message->attach(storage_path($this->photo), [
                    'as' => 'damage.jpg',
                    'mime' => 'application/jpg',
                ]);
            }
        );
        $this->success = 'Next we will review your damage and contact you to schedule a repair, within the next 2-3 hours!';
        $this->clearFields();
    }

    private function clearFields() {
        $this->make = '';
        $this->year = '';
        $this->name = '';
        $this->email = '';
        $this->zip = '';
        $this->phone = '';
        $this->photo = '';
    }

    public function render() {
        return view('livewire.contact-form');
    }
}

0 个答案:

没有答案