【问题标题】:Send pdf file as email attachment in laravel 5.7在 laravel 5.7 中将 pdf 文件作为电子邮件附件发送
【发布时间】:2019-12-03 19:00:22
【问题描述】:

我想以 pdf 格式发送发票刀片文件的电子邮件,但出现以下错误

(1/1) Swift_IoException 无法打开文件进行读取 [/storage/D:\xampp\htdocs\clientreq\public/mypdf.pdf]

控制器:

 public function insert(Request $req)
    {
      ini_set('max_execution_time', 300000);
    $mytime = Carbon::now();
    $project_type=implode(',',$req->input('project_type'));
    //$qty=implode(',',$req->input('qty'));
    $amount=implode(',',$req->input('amount'));
    $id=$req->input('id');
    $bill_date=$req->input('bill_date');
    $updated_at=$mytime->toDateTimeString();
      $created_at=$mytime->toDateTimeString();
      $data = array('client_id'=>$id,'date_of_bill'=>$bill_date,'description'=>$project_type,'amount'=>$amount,'created_at' => $created_at,'updated_at' => $updated_at);
    DB::table('bill')->insert($data);

    $client = DB::table('requirement')
                  ->join('bill','requirement.id','=','bill.client_id')
                  ->select('requirement.*','bill.*')
                  ->where('requirement.id',$id)
                  ->where('bill.date_of_bill',$bill_date)
                  ->first();

    $data = array(
            'client_name' => $client->client_name,
            'company_name' => $client->company_name,

        );

    $pdf = \PDF::loadView('pages.invoice',$data);
    //return $pdf->stream();

    $data = array(
        'email_address'=>'seemashelar01@gmail.com',

    );

    Mail::send('pages.invoice', $data, function($message) use($data) {
    $message->from('seemashelar01@gmail.com', 'PuraBox');
    $message->to($data['email_address']);
    $message->subject('hello');
    $filename = \Storage::url(public_path() . '/' . 'mypdf.pdf');
    $message->attach($filename);
    //Full path with the pdf name

});  

    }

【问题讨论】:

  • 您的文件路径不正确,您的pdf在公共路径或存储中

标签: laravel


【解决方案1】:

如果你想要来自Storage 的地址文件,你应该调用attachFromStorage,像这样:

    $message->attachFromStorage(
           /path/to/file',
           'name.pdf', [
               'mime' => 'application/pdf'
           ]);

更多信息:https://laravel.com/docs/5.7/mail#attachments

【讨论】:

  • (1/1) ErrorException call_user_func_array() 期望参数 1 是一个有效的回调,类 'Swift_Message' 没有方法 'attachFromStorage' 这个错误会发生
【解决方案2】:

试试下面这个例子

$data = array(
        'name' => Input::get('name'),   
        'detail'=>Input::get('detail'),
        'sender' => Input::get('sender')
    );

$pdf = PDF::loadView('letters.test', $data);

Mail::send('emails.invoice', $data, function($message) use($pdf)
{
    $message->from('us@example.com', 'Your Name');

    $message->to('foo@example.com')->subject('Invoice');

    $message->attachData($pdf->output(), "invoice.pdf");
});

【讨论】:

  • (1/1) 异常框属性计算需要包含块宽度出现此错误
  • 我想使用电子邮件将创建的发票发送给用户
  • $file = Config::get('invoice.path') 。 '/39/company/1/invoice/' 。 '发票 27' 。 '.pdf'; $message->attach($file, array('as' => 'invoice', 'mime' => 'application/pdf'));
  • 试试上面的例子
猜你喜欢
  • 2013-03-19
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多