dragon5006 2017-03-24 07:13
浏览 42
已采纳

在Laravel中将生成pdf附加到邮件中

I've some problem with email attach generate pdf, hope you can give me some advice, please kindly help.

Here is the Controller:

public function kirim(Request $request){
    $keluhan = keluhan::findOrFail($request->id);

    $tindak = DB::table('tindakans')
    ->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
    ->select(DB::raw('tindakans.id, id_keluhan, perbaikan_sementara, revisi_dokumen, target_verifikasi, ttd_tanggung1,
    ttd_tanggung2'))->get();

    $analisa = DB::table('analisas')
    ->join('tindakans','tindakans.id','=','analisas.id_tindakan')
    ->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
    ->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();

    $pdf = \PDF::loadView('laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
    //return $pdf->stream();

    $data = array(
        'email_address'=>$request->email_address,
        'cc'=>$request->cc,
        'subject'=>$request->subject,
        'keterangantambahan'=>$request->keterangantambahan
    );

    Mail::send('laporan.kirim', $data, function($message) use($data) {
        $message->from('christian7andrew@gmail.com', 'PuraBox');
        $message->to($data['email_address']);
        if($data['cc'] != null){
            $message->cc($data['cc']);
        }
        $message->subject($data['subject']);
        $message->Attach($pdf);
    });

    return redirect('/');
}

How can I attach $pdf?

  • 写回答

4条回答 默认 最新

  • douxiezha9319 2017-03-24 13:06
    关注

    You are using:

    $message->Attach($pdf);
    

    To add attachments to an e-mail, use the attach method on the $message object passed to your Closure. The attach method accepts the full path to the file as its first argument:

    Source

    You should use the attach method (without the capitalize letter), and in the parameter, you need to pass the path where the pdf is, not the generated pdf. like the docs says.

    $message->attach($pathToFile);
    

    In the end, will be something like that:

    Mail::send('laporan.kirim', $data, function($message) use($data) {
        $message->from('christian7andrew@gmail.com', 'PuraBox');
        $message->to($data['email_address']);
        if($data['cc'] != null){
            $message->cc($data['cc']);
        }
        $message->subject($data['subject']);
    
        //Full path with the pdf name
        $message->attach('foo/bar/mypdfname.pdf');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵