使用 Laravel(本地主机)通过表单将文件发送到电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22798527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending a file via form to email with Laravel (Localhost)
提问by Neil Kearney
Newbie to Laravel so be kind lol
Laravel 的新手,所以要友善,哈哈
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.
我的 mail.php 配置是正确的,并且已成功接收电子邮件以将文本输入到 gmail,但不确定如何完成文件任务。我将不胜感激一些帮助或参考链接。
Thanks in advance!!
提前致谢!!
Code in routes.php
route.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.');
}
//
});
Code in form.blade.php
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>
回答by nvisser
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('[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())
);
});
Documentation: http://laravel.com/docs/requests#filesand http://laravel.com/docs/mail#basic-usage
文档:http: //laravel.com/docs/requests#files和http://laravel.com/docs/mail#basic-usage