added attachment as link

This commit is contained in:
Martin Folkerts 2024-09-09 10:52:39 +02:00
parent 77633af429
commit 3e773b1386
8 changed files with 125 additions and 36 deletions

View file

@ -44,6 +44,7 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien
$mailing = $report->project->mailings()->create([
'subject' => $subject,
'message' => $message,
'report_id' => $report->id,
]);
$mailing->attachments()->create([
@ -53,7 +54,7 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien
$mailing->recipients()->createMany($recipients);
Mail::to($mailing->recipients()->pluck('email')->toArray())
->send(new \App\Mail\ReportMailer($mailing));
->send(new \App\Mail\ReportMailer($mailing, $report));
}
}

View file

@ -5,6 +5,7 @@
use App\Models\ProjectMailing;
use App\Models\ProjectMailingAttachment;
use App\Models\ProjectMosaic;
use App\Models\ProjectReport;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
@ -52,40 +53,44 @@ public function content(): Content
'logoPath'=> resource_path('images/smartcane.png'),
'subject' => $this->mailing->subject,
'mailing' => $this->mailing,
'reportUrl' => route(
'project.report.download',
$this->mailing->report->token
),
],
//htmlString: $this->mailing->message
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) {
$attachment_path = Storage::path($attachment->mailing->project->download_path."/".$attachment->path);
if (!File::exists($attachment_path)) {
logger('Could not find attachment: ' . $attachment_path);
return null;
}
logger('Attachment found: ' . $attachment_path);
$mime = 'application/pdf'; // default MIME type
$extension = pathinfo($attachment->path, PATHINFO_EXTENSION);
if ($extension === 'docx') {
$mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
return Attachment::fromPath(
$attachment_path
)
->as($attachment->path)
->withMime($mime);
})
->filter()
->toArray();
}
// /**
// * Get the attachments for the message.
// *
// * @return array<int, \Illuminate\Mail\Mailables\Attachment>
// */
// public function attachments(): array
// {
// return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) {
// $attachment_path = Storage::path($attachment->mailing->project->download_path."/".$attachment->path);
//
// if (!File::exists($attachment_path)) {
// logger('Could not find attachment: ' . $attachment_path);
// return null;
// }
// logger('Attachment found: ' . $attachment_path);
//
// $mime = 'application/pdf'; // default MIME type
// $extension = pathinfo($attachment->path, PATHINFO_EXTENSION);
// if ($extension === 'docx') {
// $mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
// }
//
// return Attachment::fromPath(
// $attachment_path
// )
// ->as($attachment->path)
// ->withMime($mime);
// })
// ->filter()
// ->toArray();
// }
}

View file

@ -19,10 +19,10 @@ class ProjectMailing extends Model
protected $fillable = [
'subject',
'message',
'status'
'status',
'report_id',
];
public function addAttachment($name, UploadedFile $file)
{
$prefix = Str::random(10);
@ -39,7 +39,10 @@ public function addAttachment($name, UploadedFile $file)
'path' => $path
]));
}
public function report()
{
return $this->belongsTo(ProjectReport::class, 'report_id', 'id');
}
public function project()
{

View file

@ -7,6 +7,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ProjectReport extends Model
{
@ -18,6 +19,17 @@ public function project()
return $this->belongsTo(Project::class);
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->token)) {
$model->token = Str::random(32); // Generates a random string of 60 characters
}
});
}
public function getFileNameAttribute(): string
{

View file

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('project_reports', function (Blueprint $table) {
$table->string('token')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('project_reports', function (Blueprint $table) {
$table->dropColumn('token');
});
}
};

View file

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('project_mailings', function (Blueprint $table) {
$table->bigInteger('report_id')->unsigned()->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('project_mailings', function (Blueprint $table) {
$table->dropColumn('report_id');
});
}
};

View file

@ -16,7 +16,10 @@
{{$mailingContent}}
Thanks.
<div style="text-align: center"> <a href="{{ $reportUrl }}" target="_blank" style="margin: 0 auto;display: inline-block; padding: 10px 20px; font-size: 16px; color: #ffffff; background-color: #2aaf99; border: none; border-radius: 5px; text-align: center; text-decoration: none; cursor: pointer;"> View report</a></div>
## Thanks.
## Contact

View file

@ -33,3 +33,12 @@
Route::get('/projects/{projectReport}/download', [\App\Http\Controllers\ProjectReportController::class, 'download'])->name('project.report.download');
Route::get('/projects', [\App\Http\Controllers\ProjectController::class, 'index'])->name('project');
});
Route::get('download/{token}', function ($token) {
$report = App\Models\ProjectReport::where('token', $token)->first();
if ($report) {
$path = $report->project->download_path . '/' . $report->path;
return response()->download(Storage::path($path));
}
return abort(404);
})->name('project.report.download');