如何使用 Laravel 将附件文件发送到电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47051151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:53:37  来源:igfitidea点击:

how to send attachment files to email using laravel

phphtmllaravelemailemail-attachments

提问by anonymous

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('[email protected]')->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>

Hey guys 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?

嘿伙计们,我尝试做一个简单的联系表格来发送到我的电子邮件.. 目前它可以发送该电子邮件但没有附件。所以我尝试为附件编写代码,但它似乎不起作用,我收到错误:未定义的变量:请求我在这里做错了什么?

回答by Rimon Khan

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

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

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

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

在表单标签中添加 attr 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

在您的内部尝试此代码 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('[email protected]')->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())
                );
            }
        }
    });

回答by Ollobergan Karimov

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

回答by Vikas Kumar

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

回答by Mohammad hayajneh

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

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

use AddAttactment

使用 AddAttactment