63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Option;
|
|
use App\Mail\InvoiceMail;
|
|
use App\Models\Invoice;
|
|
use App\TenantMail;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Mail\Mailer;
|
|
|
|
class MailController extends Controller
|
|
{
|
|
/**
|
|
* The Mailer used for sending Tenant Mails defined by options.
|
|
* @var Mailer
|
|
*/
|
|
protected Mailer $mailer;
|
|
|
|
/**
|
|
* Set the TenantMail::class as mailer for further usage
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->mailer = TenantMail::get();
|
|
}
|
|
|
|
/**
|
|
* Send the invoice mail for the given request.
|
|
*/
|
|
public function sendInvoice(Request $request): JsonResponse
|
|
{
|
|
$invoice = Invoice::find($request->id);
|
|
$invoiceMail = new InvoiceMail($invoice);
|
|
$invoiceMail->subject($request->Subject);
|
|
$invoiceMail->body = $request->Body;
|
|
|
|
$options = Option::optionsAsObject();
|
|
if (property_exists($options, 'bcc_copy')) {
|
|
if (is_null($request->Bcc)) {
|
|
$request->Bcc = $options->email;
|
|
} else {
|
|
$request->Bcc .= "," . $options->email;
|
|
}
|
|
}
|
|
|
|
try {
|
|
$this->mailer->to($request->To)
|
|
->cc($request->Cc)
|
|
->Bcc($request->Bcc)
|
|
->send($invoiceMail);
|
|
} catch (\Exception $exception) {
|
|
return response()->json(['status' => 'error', 'message' => $exception->getMessage()], 500);
|
|
}
|
|
|
|
$invoice->update(['status' => 'sent']);
|
|
|
|
return response()->json($invoice);
|
|
}
|
|
}
|