Php 如何在laravel中发送带有附件(pdf)的电子邮件?
我想发送一封带有附件(pdf)的电子邮件,该附件来自一个请求。没有附件。一封电子邮件发送得很完美,但带有附件,它抛出了一个错误Php 如何在laravel中发送带有附件(pdf)的电子邮件?,php,laravel,email,Php,Laravel,Email,我想发送一封带有附件(pdf)的电子邮件,该附件来自一个请求。没有附件。一封电子邮件发送得很完美,但带有附件,它抛出了一个错误 Fatal error: Uncaught exception 'Swift_IoException' with message 'The path cannot be empty' 这是我的邮件课 class MyMail extends Mailable { use Queueable, SerializesModels; public $details
Fatal error: Uncaught exception 'Swift_IoException' with message 'The path cannot be empty'
这是我的邮件课
class MyMail extends Mailable {
use Queueable, SerializesModels;
public $details;
public function __construct($details =[]) {
$this->details = $details;
}
public function build() {
if ($this->details['file']) {
return $this->subject('New applicant')
->from("my@email.com", "name")
->view('email.mail')
->attach($this->details['file']->getRealPath(),
[
'as' => $this->details['file']->getClientOriginalName(),
'mime' => $this->details['file']->getClientMimeType(),
]);
}
}
}
这是我的控制器代码
$file = $request->file('file ');
if ($file) {
$file_name = hexdec(uniqid());
$ext = strtolower($file->getClientOriginalExtension());
$file_full_name = $file_name . '.' . $ext;
$upload_path = 'files/';
$upload_path1 = 'backend/files/';
$file_url = $upload_path . $file_full_name;
$success = $file->move($upload_path1, $file_full_name);
}
$details = [
'applicant_name' => $request->name,
'applicant_contact' => $request->contact,
'applicant_email' => $request->email,
'file' => $file,
];
$base_email = 'example@email.com';
Mail::to($base_email)->send(new MyMail($details));
有人能帮助我如何发送带有附件的电子邮件吗?您是根据请求直接附加的。但您应该将图像上载到存储或公用文件夹。然后您可以附加图像路径
$details = [
'applicant_name' => $request->name,
'applicant_contact' => $request->contact,
'applicant_email' => $request->email,
'file' => $file,
'fileUrl' => $file_url,
];
$this->subject('New applicant')
->from("my@email.com", "name")
->view('email.mail')
->attach(asset( $this->details['fileUrl']),
[
'as' => $this->details['file']->getClientOriginalName(),
'mime' => $this->details['file']->getClientMimeType(),
]);
参考:在我的控制器中,我上传了文件并将其发送到邮件类,如下所示:
if ($request->has('resume')) {
$file = $request->file('resume');
$file_name = Str::random(7) . '.'.$file->getClientOriginalExtension();
$destinationPath = "/followUp";
$file->move(public_path($destinationPath), $file_name);
$file = 'followUp/'.$file_name;
}
$subject = 'پیگیری رزومه - ایندید';
Mail::to('visanewcom@gmail.com')->send(new sendMail($username,$mobile,$subject,$file));
然后在send mail类中:
return $this->view('email.followUp')
->with('username',$this->username)
->with('mobile',$this->mobile)
->with('file',$this->file)
->subject($this->subject)
->attach(public_path($this->file), [
'as' => 'resume.pdf',
'mime' => 'application/pdf',
])
;
在从请求附加之前,我将文件存储到我的公用文件夹中。再次看到帖子,我补充道code@ihprince. updatedtry从存储中而不是从请求中获取文件