How to upload files by sending the attachment as email in laravel 5.3?

1

I'm making a page of work with us with uploaded files (resumes), I would send this file by email attachment, but I'm kind of aimless. Can anyone help me?

Controller

    public function trabalheConosco(RequestNewsletter $request)
{

    $data['nomeDaEmpresa'] = "Arantes Nutrição Animal";
    $data['nomeTrabalhe'] = $request->input("nomeTrabalhe");
    $data['sobrenome'] = $request->input("sobrenome");
    // $data['anexo'] = $request->input("anexo");
    $data['emailTrabalhe'] = $request->input("emailTrabalhe");
    $data['dataDeNascimento'] = $request->input("dataDeNascimento");
    $data['area'] = $request->input("area");
    $data['mensagemTrabalhe'] = $request->input("mensagemTrabalhe");
    date_default_timezone_set('America/Sao_Paulo');
    $date = date('Y-m-d H:i');

    $data['dataDeEnvio'] = $date;
    Mail::send('emails.trabalhe', $data, function($message){
        $message->to('[email protected]', 'Teste')
        ->subject('Currículo');
        $message->attach($anexo);
    });
    $request->session()->flash('alert-success', 'Currículo cadastrado com sucesso.');
    return redirect('trabalhe-conosco');
}

View

            <form action="{{ url('trabalhe-conosco') }}" method="POST" class="row">
            {{ csrf_field() }}
                <div class="form-group col-sm-12">
                    <p><strong>Dados Pessoais</strong></p>
                </div>
                <div class="form-group col-sm-4" {{ $errors->has('nomeTrabalhe') ? ' has-error' : '' }}>
                    <input type="text" name="nomeTrabalhe" class="form-control" placeholder="Nome: ">
                    @if ($errors->has('nomeTrabalhe'))
                        <span class="help-block">
                            <strong>{{ $errors->first('nomeTrabalhe') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group col-sm-5">
                    <input type="text" name="sobrenome" class="form-control" placeholder="Sobrenome: ">
                </div>
                 <div class="form-group col-sm-3">
                    <input id="anexo" type="file" name="anexo" style="display: none"><label for='anexo' class="form-control" style="height: 48px;line-height: 35px;background-color: #a8cf45;">Anexar Currículo<i class="fa fa-paperclip" aria-hidden="true"></i></label></input>
                </div>
                <div class="form-group col-sm-3">
                    <input type="text" name="email" class="form-control" placeholder="E-mail: ">
                </div>
                <div class="form-group col-sm-4">
                    <input type="text" name="data_de_nascimento" class="form-control" placeholder="Data de Nascimento: ">
                </div>
                <div class="form-group col-sm-5">
                    <input type="text" name="area_de_interesse" class="form-control" placeholder="Área de Interesse: ">
                </div>
                <div class="form-group col-sm-12">
                    <textarea name="mensagem" class="animated form-control" rows="6" placeholder="Mensagem: "></textarea>
                </div>
                <div class="form-group col-sm-12">
                    <button class="btn btn-default">Enviar</button>
                </div>
            </form>
            </div>

RequestNewsletter

public function messages()
{
    return [
        'emailnewsletter.required' => 'O campo Email é obrigatório.',
        'emailnewsletter.email' => 'O campo Email deve ser um email válido',
        'emailnewsletter.min' => 'O campo Email deve ter pelo menos :min caracteres.',
    ];
}

public function rules()
{
    return [
        'emailnewsletter' =>  'required|email|min:7',
    ];
}

Route

Route::post('trabalhe-conosco', 'Front\MailController@trabalheConosco');
    
asked by anonymous 11.10.2016 / 14:41

1 answer

1

First, you have to set the enctype="multipart/form-data" attribute to its <form> . Staying like this:

<form action="{{ url('trabalhe-conosco') }}" method="POST" class="row" enctype="multipart/form-data">

Then in the Mail function:

Add this:

Mail::send('emails.trabalhe', $data, function($message) use ($data){
    $message->to('[email protected]', 'Teste')
    ->subject('Currículo')
    ->attach($data['anexo']->getRealPath(), array(
        'as'   => 'file-.' . $data['anexo']->getClientOriginalExtension(), 
        'mime' => $data['anexo']->getMimeType());
    );
});

Request of the file. Uncomment and change input by file .

$data['anexo'] = $request->file("anexo");
    
11.10.2016 / 15:23