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

标签 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>

最佳答案

首先,请务必允许您的文件接受文件上传:

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

关于php - 使用 Laravel (Localhost) 通过表单将文件发送到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798527/

相关文章:

javascript - jQuery 文件上传 - 单次上传多次发送数据

php - 执行 PHP 钩子(Hook)的最佳方法

php - 在CodeIgniter中自动创建 View 和 Controller

php - 发送电子邮件并写入数据库不起作用

python - 来自jython中已解析的电子邮件对象的电子邮件正文

java - 导入一个文件(MultipartFile)及过程-Java/Grails

php - 使用 cronjob 更新和显示 MySQL 查询的结果

php - 如何找出 PHP 中 MySQL 错误与哪一列相关?

email - ZF2 : Using Zend\View to render emails

html - 即使在 Linux 中,从 <file> 获取值也会给出 C :\fakepath\filename,