簡體   English   中英

使用Laravel(Localhost)通過表單將文件發送到電子郵件

[英]Sending a file via form to email with Laravel (Localhost)

拉拉維爾的新手很善良哈哈

我對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>

首先,請務必允許您的文件接受文件上傳:

{{ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM