'json']; public function emailRecord() { return $this->hasOne(EmailRecord::class, 'id', 'email_record_id'); } /** * 邮件模版内容替换 * @param $template * @param $email_record_users */ public static function template($template, $var_data) { foreach ($var_data as $key => $var) { $key = trim($key); $var = trim($var); $template = str_replace('{' . $key . '}', $var, $template); } return $template; } /** * 发送邮件 * @param string $title 邮件标题 * @param string $template 邮件内容 * @param string $email 收件人邮箱 * @param array $attachments 附件数组,可以是 Upload 对象数组或文件路径数组 */ public static function email($title, $template, $email, $attachments = []) { Mail::send('email', compact('template'), function ($message) use ($email, $title, $attachments) { $message->from(env('MAIL_USERNAME'), '苏州科技商学院'); $message->to($email)->subject($title); // 添加附件 if (!empty($attachments)) { foreach ($attachments as $attachment) { if ($attachment instanceof Upload) { // 如果是 Upload 对象,获取文件路径 // folder 格式为 'storage/files',实际文件存储在 storage/app/public/files $folderPath = str_replace('storage/', '', $attachment->folder); $filePath = storage_path('app/public/' . $folderPath . '/' . $attachment->name); if (file_exists($filePath)) { $message->attach($filePath, [ 'as' => $attachment->original_name, ]); } } elseif (is_string($attachment) && file_exists($attachment)) { // 如果是文件路径字符串 $message->attach($attachment); } } } }); return true; } }