我想通过电子邮件发送多张图像。我是laravel的新手,我不知道如何添加多个附件。但是请采取以下方法。
发送电子邮件控制器:
foreach (json_decode($img_files) as $img_file) {
$imgName = $img_file->name;
$pathToFile[] = ['path' => public_path() . "\\uploads\\" . $imgName];
}
//send the email to the relevant customer email
Mail::to($customer_email)->send(new SendMail($customer_firstname, $pathToFile), function ($message) {
// $message->attach($pathToFile);
foreach ($pathToFile as $path) {
$pathToFile = $path->path;
$message->attach($pathToFile);
}
});
dd($ pathToFile)的输出
array:2 [
0 => array:1 [
"path" => "C:\xampp\htdocs\bit-project\public\uploads\photo_2019-11-08_23-11-50.jpg"
]
1 => array:1 [
"path" => "C:\xampp\htdocs\bit-project\public\uploads\photo_2019-11-08_23-11-55.jpg"
]
]
在电子邮件模板中,这是我添加图像的方式:
<img src="{{ $message->embed($pathToFile) }}" style="width:600px;height:335px">
如上所述,我有一个数组,其中包含上传的图像路径。我如何遍历此数组以将其传递给SendMail
。感谢您的帮助!
(编辑):这是SendMail类的代码
public function __construct($customer_firstname, $pathToFile)
{
$this->customer_firstname = $customer_firstname;
$this->pathToFile = $pathToFile;
}
public function build()
{
return $this->subject('Thank you! - Advance Car Diagnostics (Pvt.)Ltd')
->view('admin.email_template')
->with('data', $this->customer_firstname);
}
我这样编辑了构建方法:
public function build()
{
$email = $this->subject('Thank you! - Advance Car Diagnostics (Pvt.)Ltd')
->view('admin.email_template')
->with('data', $this->customer_firstname);
foreach($this->pathToFile as $filePath){
$email->attach($filePath);
}
return $email;
}
但是它给出了这个错误:basename () expects parameter 1 to be string array
出现basename...
错误的原因是在现在传递数组而不是文件名数组之前。
以下内容将帮助您实现所追求的目标:
$files = collect(json_decode($img_files))->map(function ($file) {
return public_path('uploads' . DIRECTORY_SEPARATOR . $file->name);
})->toArray();
Mail::to($customer_email)->send(new SendMail($customer_firstname, $files));
SendMail类:
public function __construct($customer_firstname, $files)
{
$this->customer_firstname = $customer_firstname;
$this->files = $files;
}
public function build()
{
return $this->subject('Thank you! - Advance Car Diagnostics (Pvt.)Ltd')
->view('admin.email_template')
->with('data', $this->customer_firstname); //I'm not sure if this line is correct but it's what you had in your question.
->with('files', $this->files);
}
admin.email_template:
@foreach($files as $file)
<img src="{{ $message->embed($file) }}" style="width:600px;height:335px">
@endforeach