Sending a file via form to email with Laravel (Loc

2019-02-17 16:36发布

Newbie to Laravel so be kind lol

My config for mail.php is correct and emails are being received successfully for text inputs to gmail, but not sure exactly how to complete the task for files. I would appreciate some assistance or reference links.

Thanks in advance!!

Code in routes.php

Route::get('/', function()
{
return View::make('form');
});

Route::post('/form', function()
{
        $data = ['firstname' => Input::get('firstname'), 'username' =>        Input::get('username'), 'email' => Input::get('email'), 'resume' => Input::get('resume') ];

        $rules = array(
            'username' => 'Required|Min:7',
            'email'     => 'Required|Email',
            'firstname' => 'Required',
            'resume' => 'Required'
        );


        $validation = Validator::make(Input::all(), $rules);

        if ($validation->fails())
        {
            // Validation has failed.
            return Redirect::to('/')->withInput()->withErrors($validation);
        }

        else {

            Mail::send('emails.welcome', $data, function($message)
        {
            $message->to('mail@domain.net');
            $message->subject('Welcome to Laravel');
            $message->from('sender@domain.net');


        });

        return Redirect::to('/')->withInput()->with('success', 'Thank you for your submission.');

        }
//
});

Code in form.blade.php

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
@if(Session::has('success'))
<div class="alert-box success">
    <h2>{{ Session::get('success') }}</h2>
</div>
@endif

    {{ Form::open(array('url' => '/form')) }}
        <p>{{ Form::label('email', 'E-Mail Address');}} <br>{{    Form::email('email', 'example@mail.com');}}</p>
        {{ $errors->first('email') }}

        <p>{{ Form::label('username', 'Username');}} <br> {{Form::text('username');}}</p>
        {{ $errors->first('username') }}

        <p>{{ Form::label('firstname', 'First Name');}} <br> {{Form::text('firstname');}}</p>
        {{ $errors->first('firstname') }}

        <p>{{ Form::file('resume'); }}</p>

        <p>{{Form::submit('Send Details');}}</p>

    {{ Form::close() }}



</body>
</html>

1条回答
可以哭但决不认输i
2楼-- · 2019-02-17 17:22

First off, be sure to allow your file to accept file uploads:

{{ Form::open(array('url' => '/form', 'files' => true)) }}

After that, you can do something along the lines of this:

$input = Input::all();
Mail::send('emails.welcome', $data, function($message) use ($input)
{
    $message->to('mail@domain.net');
    $message->subject('Welcome to Laravel');
    $message->from('sender@domain.net');
    $message->attach($input['resume']->getRealPath(), array(
        'as' => 'resume.' . $input['resume']->getClientOriginalExtension(), 
        'mime' => $input['resume']->getMimeType())
    );
});

Documentation: http://laravel.com/docs/requests#files and http://laravel.com/docs/mail#basic-usage

查看更多
登录 后发表回答