Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/resend mails #15

Merged
merged 9 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Commands/ResendMailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
namespace Vormkracht10\Mails\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Vormkracht10\Mails\Jobs\ResendMailJob;
use Vormkracht10\Mails\Models\Mail;

class ResendMailCommand extends Command
class ResendMailCommand extends Command implements PromptsForMissingInput
{
public $signature = 'mail:resend {uuid} {to} {cc} {bcc}';
public $signature = 'mail:resend {uuid} {to?} {cc?} {bcc?}';

public $description = 'Resend mail';

public function handle(): int
{
// TODO: resend mail
$mail = Mail::where('uuid', $this->argument('uuid'))->first();

ResendMailJob::dispatch($mail,
...collect($this->argument())->only(['to', 'cc', 'bcc'])->map(fn ($n) => $n ?? []),
david-d-h marked this conversation as resolved.
Show resolved Hide resolved
);

$this->comment('All done');

return self::SUCCESS;
}

protected function promptForMissingArgumentsUsing()
{
return ['uuid' => 'What is the UUID of the email you want to re-send?'];
david-d-h marked this conversation as resolved.
Show resolved Hide resolved
}
}
51 changes: 51 additions & 0 deletions src/Jobs/ResendMailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Vormkracht10\Mails\Jobs;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Mail\Message;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Vormkracht10\Mails\Models\Mail;

class ResendMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, InteractsWithSockets, Queueable, SerializesModels;

public function __construct(
private readonly Mail $mail,
private array $to = [],
private array $cc = [],
private array $bcc = [],
) {
$this->checkFields($this->mail);
}

public function handle(): void
{
\Illuminate\Support\Facades\Mail::send([], callback: fn (Message $mail) => $mail
david-d-h marked this conversation as resolved.
Show resolved Hide resolved
->replyTo($this->mail->reply_to ?? [])
->subject($this->mail->subject ?? '')
->to($this->to ?? [])
->cc($this->cc ?? [])
->bcc($this->bcc ?? [])
&& is_null($this->mail->html)
david-d-h marked this conversation as resolved.
Show resolved Hide resolved
? $mail->text($this->mail->text ?? '')
: $mail->html($this->mail->html)
);
}

protected function checkFields(Mail $mail)
{
if (! empty($this->to)) {
return;
david-d-h marked this conversation as resolved.
Show resolved Hide resolved
}

[$this->to, $this->cc, $this->bcc] = array_values(
collect($mail->only(['to', 'cc', 'bcc']))->map(fn ($n) => $n ?? [])->toArray(),
);
}
}
1 change: 0 additions & 1 deletion src/Traits/HasMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Vormkracht10\Mails\Contracts\HasAssociatedMails;
use Vormkracht10\Mails\Models\Mail;

/**
Expand Down