Merge pull request #2974 from novag/attachments

allow attaching uploaded files when sending mails
This commit is contained in:
Cüneyt Şentürk
2023-05-31 11:07:51 +03:00
committed by GitHub
5 changed files with 118 additions and 16 deletions

View File

@ -14,9 +14,10 @@ class CustomMail extends FormRequest
public function rules()
{
return [
'to' => 'required|email',
'subject' => 'required|string',
'body' => 'required|string',
'to' => 'required|email',
'subject' => 'required|string',
'body' => 'required|string',
'attachments.*' => 'nullable|boolean',
];
}
}

View File

@ -30,10 +30,15 @@ class SendDocumentAsCustomMail extends Job
$custom_mail['cc'] = user()->email;
}
$attachments = collect($this->request->get('attachments', []))
->filter(fn($value) => $value == true)
->keys()
->all();
$notification = config('type.document.' . $document->type . '.notification.class');
// Notify the contact
$document->contact->notify(new $notification($document, $this->template_alias, true, $custom_mail));
$document->contact->notify(new $notification($document, $this->template_alias, true, $custom_mail, $attachments));
event(new DocumentSent($document));
}

View File

@ -35,10 +35,17 @@ class Invoice extends Notification
*/
public $attach_pdf;
/**
* List of document attachments to attach when sending the email.
*
* @var array
*/
public $attachments;
/**
* Create a notification instance.
*/
public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = [])
public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = [], $attachments = [])
{
parent::__construct();
@ -46,6 +53,7 @@ class Invoice extends Notification
$this->template = EmailTemplate::alias($template_alias)->first();
$this->attach_pdf = $attach_pdf;
$this->custom_mail = $custom_mail;
$this->attachments = $attachments;
}
/**
@ -68,6 +76,17 @@ class Invoice extends Notification
]);
}
// Attach selected attachments
if (! empty($this->invoice->attachment)) {
foreach ($this->invoice->attachment as $attachment) {
if (in_array($attachment->id, $this->attachments)) {
$message->attach($attachment->getAbsolutePath(), [
'mime' => $attachment->mime_type,
]);
}
}
}
return $message;
}