使用Laravel(Localhost)通过表单将文件发送到电子邮件

时间:2014-04-01 23:01:12

标签: php email file-upload laravel localhost

新手到Laravel所以要善良lol

我对mail.php的配置是正确的,并且收到的电子邮件已成功接收到gmail的文本输入,但不确定如何完成文件的任务。我将不胜感激一些帮助或参考链接。

提前致谢!!

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.');

        }
//
});

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 个答案:

答案 0 :(得分:13)

首先,请确保允许您的文件接受文件上传:

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

之后,您可以采取以下措施:

$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())
    );
});

文档:http://laravel.com/docs/requests#fileshttp://laravel.com/docs/mail#basic-usage