简体   繁体   English

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

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

Newbie to Laravel so be kind lol 拉拉维尔的新手很善良哈哈

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. 我对mail.php的配置是正确的,并且正在成功接收电子邮件以便将文本输入到gmail,但不确定如何完成文件的任务。 I would appreciate some assistance or reference links. 我将不胜感激一些帮助或参考链接。

Thanks in advance!! 提前致谢!!

Code in routes.php 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.');

        }
//
});

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', '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>

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

Documentation: http://laravel.com/docs/requests#files and http://laravel.com/docs/mail#basic-usage 文档: 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