简体   繁体   中英

how to send attachment files to email using laravel

Controller:

 public function sendemail(Request $request) { $data = array( 'name'=> $request->name, 'email'=> $request->email, 'text'=> $request->text, 'category'=> $request->category, 'company'=> $request->company, 'number'=> $request->number ); \\Mail::send('AltHr/Portal/supportemail', $data, function ($message) use($data){ $message->from($data['email'], $data['name']); $message->to('ra7veer@gmail.com')->subject($data['company'] . ' - ' .$data['category']); $message->attach($request->file('files')->getRealPath(), [ 'as' => $request->file('files')->getClientOriginalName(), 'mime' => $request->file('files')->getMimeType() ]); }); return view('AltHr.Portal.support'); }

Blade:

 <div class="form-group form-group-default"> <label>Attachment</label> <input type="file" name="files[]" accept="file_extension|image/*|media_type" multiple> </div>

I tried doing a simple contact form to send to my email. Currently it works im able to send that email but without attachments. So i tried doing the code for the attachments but it dont seem to be working im getting error:

Undefined variable: request

What am I doing wrong here?

Your $request variable is undefined because $request variable is not available inside the function you need to passed this below

\Mail::send('AltHr/Portal/supportemail', compact('data'), function ($message) use($data, $request){ ....   });

Add the attr inside the form tag enctype="multipart/form-data"

<form role="form" action="{{action('AltHr\Portal\PortalController@sendemail')}}" method="post" class="m-t-15" enctype="multipart/form-data">
    ....
</form>

Try this code inside your sendemail() method

$data = array(
        'name'=> $request->name,
        'email'=> $request->email,
        'text'=> $request->text,
        'category'=> $request->category,
        'company'=> $request->company,
        'number'=> $request->number
    );
    $files = $request->file('files');
    \Mail::send('AltHr/Portal/supportemail', compact('data'), function ($message) use($data, $files){    
        $message->from($data['email']);
        $message->to('nuru7495@gmail.com')->subject($data['company'] . ' - ' .$data['category']);

        if(count($files > 0)) {
            foreach($files as $file) {
                $message->attach($file->getRealPath(), array(
                    'as' => $file->getClientOriginalName(), // If you want you can chnage original name to custom name      
                    'mime' => $file->getMimeType())
                );
            }
        }
    });
$emails = ['email address 1', 'email address 2'];
$files = ['url 1','url 2'];
Mail::send('emails.welcome', [], function($message) use ($emails, $files)
{
    $message->to($emails)->subject('This is test e-mail');
    foreach ($files as $file){
        $message->attach($file);
    }
});
$file_to_attach = 'freestuff/free-file.pdf';

$email->AddAttachment( $file_to_attach , 'free-file.pdf' );

use AddAttactment

      Mail::send('mail', array('type'=>'AspirantsSelect','AspirantsSelectSms'=>$smg), function($message) use($emailA,$pic)
                        {
                            $message->to($emailA)->subject('Aspirants Selection');
                            $message->attach($pic);
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM