gpt4 book ai didi

php - 在 Laravel 5.4 中通过电子邮件发送 PDF 文档作为附件

转载 作者:行者123 更新时间:2023-12-02 17:11:16 25 4
gpt4 key购买 nike

我正在使用 Laravel 开发一个网络应用程序。我已经编程,当用户在表单中输入数据时,数据会发送到我的电子邮件地址用户然后会收到一条反馈消息。我想在反馈消息中附加一个 PDF 文档。寻求有关如何将用户详细信息从 Controller 解析为 PDF 以便我可以使用它然后附加到反馈消息的帮助..

~问候

Controller

public function postContact(Request $request){

$this->validate($request, [
'name' => 'required',
'phone' => 'required',
'email' => 'required|email',
'message' => 'required|min:20',
'checkbox' => 'required']);

$data = array(
'name' => $request->name,
'phone' => $request->phone,
'email' => $request ->email,
'checkbox' => $request ->checkbox,
'bodyMessage' => $request->message
);

//code to send email to my inbox
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('info@************');
});

//Feedback mail to client
Mail::send('emails.feedback', $data, function($message) use ($data){
$message->from('info@**********');
$message->to($data['email']);
$message->subject('Thank you message');
//Attach PDF doc
$message->attachData($data, 'pdf.customer']);
});

Session::flash('success', 'Hello  '.$data['name'].' Thank You for choosing us. Will reply to your query as soon as possible');

return redirect()->back();

}

最佳答案

我建议您使用这个包来创建一个 pdf 文件,稍后可以附加该文件以将其发送到邮件。 Laravel-PDF generator

设置好上述包后,使用下面的示例代码生成pdf

公共(public)函数 postContact(请求 $request){

    $this->validate($request, [
'name' => 'required',
'phone' => 'required',
'email' => 'required|email',
'message' => 'required|min:20',
'checkbox' => 'required']);

$data = array(
'name' => $request->name,
'phone' => $request->phone,
'email' => $request ->email,
'checkbox' => $request ->checkbox,
'bodyMessage' => $request->message
);

//code to send email to my inbox
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('info@************');
});

//Feedback mail to client
$pdf = PDF::loadView('your_view_name', $data)->setPaper('a4');
Mail::send('emails.feedback', $data, function($message) use ($data,$pdf){
$message->from('info@**********');
$message->to($data['email']);
$message->subject('Thank you message');
//Attach PDF doc
$message->attachData($pdf->output(),'customer.pdf');
});

Session::flash('success', 'Hello  '.$data['name'].' Thank You for choosing us. Will reply to your query as soon as possible');

return redirect()->back();

}

关于php - 在 Laravel 5.4 中通过电子邮件发送 PDF 文档作为附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49125123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com