[英]How to send email with attachment (pdf) in laravel?
我想发送一个带有来自请求的附件 (pdf) 的 email。 无执着。 一个 email 已完美发送,但带有附件,它抛出了一个错误
Fatal error: Uncaught exception 'Swift_IoException' with message 'The path cannot be empty'
这是我的邮件 class
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(),
]);
}
}
}
这是我的 controller 代码
$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));
谁能帮助我如何发送带有附件的 email?
您是直接从请求中附加的。但是您应该将您的 iamge 上传到存储或公共文件夹。然后您可以附加图像的路径
$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(),
]);
在我的 controller 中,我上传了文件并将其发送到邮件 class,如下所示:
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));
然后在发送邮件 class 中:
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',
])
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.