首页 > 解决方案 > 使用 laravel 发送多张图片

问题描述

我想通过电子邮件发送多张图片。我是 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

标签: phphtmllaravelemailnotifications

解决方案


推荐阅读