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

View file

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

View file

@ -7,6 +7,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ProjectReport extends Model class ProjectReport extends Model
{ {
@ -18,6 +19,17 @@ public function project()
return $this->belongsTo(Project::class); 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 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}} {{$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 ## 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/{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('/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');