在laravel 5.7中将PDF文件作为电子邮件附件发送

时间:2019-12-03 08:12:45

标签: laravel

我想以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

});  

    }

2 个答案:

答案 0 :(得分:0)

如果您要从Storage发送地址文件,则应像这样调用attachFromStorage

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

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

答案 1 :(得分:0)

尝试下面的示例

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