How do I upload files by sending the attachment as an email in Windows 5.3?

Asked

Viewed 1,682 times

1

I’m making a page to work with us with uploading files(resumes), I would like to send this file by email attachment, but I’m kind of aimless. Can someone 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');
  • Make a tour by stackoverflow, learn a little about the rules and good practices. Then edit your question and enter the code of what you’ve tried...

  • How are you doing ? Do you have any code ? Mail of Laravel.

  • I’m trying like this @Gumball

  • Your form is without the attribute enctype="multipart/form-data" for sending files.

  • Thanks @Gumball I’ll try

1 answer

2


First, you have to put the attribute enctype="multipart/form-data" in his <form>. Thus remaining:

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

Then in function Mail:

Add to that:

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

File request. Decrypt and swap input for file.

$data['anexo'] = $request->file("anexo");
  • I tried this way but this error appears: Fatalthrowableerror in Mailcontroller.php line 74: Parse error: syntax error, Unexpected '->' (T_OBJECT_OPERATOR)

  • Corrected code.

  • now gives this Fatalthrowableerror in Mailcontroller.php line 74: Call to a Member Function getRealPath() on null

  • Did you deconstruct the $data['anexo'] ?

  • I edited my post, see the latest update.

  • 1

    Yes @Gumball thanks, helped me a lot!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.