2014-04-01 12 views
5

Newbie để Laravel vì vậy hãy loại lolGửi một tập tin mẫu để gửi email với Laravel (Localhost)

cấu hình của tôi cho mail.php là đúng và email đang được nhận thành công cho đầu vào văn bản để gmail, nhưng không chắc chắn chính xác làm thế nào để hoàn thành nhiệm vụ cho các tập tin. Tôi sẽ đánh giá cao một số hỗ trợ hoặc liên kết tham chiếu.

Cảm ơn trước !!

Mã trong 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('[email protected]'); 
      $message->subject('Welcome to Laravel'); 
      $message->from('[email protected]'); 


     }); 

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

     } 
// 
}); 

Mã trong 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', '[email protected]');}}</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> 

Trả lời

13

Trước hết, hãy chắc chắn để cho phép tập tin của bạn để chấp nhận tập tin tải lên:

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

Sau đó, bạn có thể làm điều gì đó dọc theo các dòng này:

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

Tài liệu: http://laravel.com/docs/requests#fileshttp://laravel.com/docs/mail#basic-usage

Các vấn đề liên quan