如何使用laravel

时间:2017-11-01 08:50:46

标签: php html laravel email email-attachments

控制器:

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

刀片:

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

嘿伙计们我尝试用一​​个简单的联系表单发送到我的电子邮件..目前它的工作原理我能够发送该电子邮件,但没有附件。所以我尝试为附件做代码但它似乎没有工作我得到错误: 未定义的变量:请求 我在这做错了什么?

4 个答案:

答案 0 :(得分:3)

您的$request变量未定义,因为$request变量在您需要传递的函数中不可用

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

在表单标记enctype="multipart/form-data"

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

$file_to_attach = 'freestuff/free-file.pdf';

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

使用AddAttactment

答案 3 :(得分:1)

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