You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

9 months ago
<?php
namespace App\Models;
use Illuminate\Support\Facades\Cache;
9 months ago
use Illuminate\Support\Facades\Mail;
9 months ago
class EmailRecordUser extends SoftDeletesModel
{
9 months ago
protected $casts = ['var_data' => 'json'];
9 months ago
public function emailRecord()
{
return $this->hasOne(EmailRecord::class, 'id', 'email_record_id');
}
9 months ago
/**
* 邮件模版内容替换
* @param $template
* @param $email_record_users
*/
9 months ago
public static function template($template, $var_data)
9 months ago
{
foreach ($var_data as $key => $var) {
$key = trim($key);
$var = trim($var);
9 months ago
$template = str_replace('{' . $key . '}', $var, $template);
}
9 months ago
return $template;
9 months ago
}
/**
* 发送邮件
3 months ago
* @param string $title 邮件标题
* @param string $template 邮件内容
* @param string $email 收件人邮箱
* @param array $attachments 附件数组,可以是 Upload 对象数组或文件路径数组
9 months ago
*/
3 months ago
public static function email($title, $template, $email, $attachments = [])
9 months ago
{
3 months ago
Mail::send('email', compact('template'), function ($message) use ($email, $title, $attachments) {
9 months ago
$message->from(env('MAIL_USERNAME'), '苏州科技商学院');
9 months ago
$message->to($email)->subject($title);
3 months ago
// 添加附件
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);
}
}
}
9 months ago
});
return true;
9 months ago
}
9 months ago
}