Changeset View
Changeset View
Standalone View
Standalone View
src/app/Mail/Helper.php
<?php | <?php | ||||
namespace App\Mail; | namespace App\Mail; | ||||
use App\Tenant; | |||||
use Illuminate\Mail\Mailable; | use Illuminate\Mail\Mailable; | ||||
use Illuminate\Support\Facades\Mail; | |||||
class Helper | class Helper | ||||
{ | { | ||||
/** | /** | ||||
* Render the mail template. | * Render the mail template. | ||||
* | * | ||||
* @param \Illuminate\Mail\Mailable $mail The mailable object | * @param \Illuminate\Mail\Mailable $mail The mailable object | ||||
* @param string $type Output format ('html' or 'text') | * @param string $type Output format ('html' or 'text') | ||||
Show All 13 Lines | public static function render(Mailable $mail, string $type = 'html'): string | ||||
throw new \Exception("Unsupported output format"); | throw new \Exception("Unsupported output format"); | ||||
} | } | ||||
// HTML output | // HTML output | ||||
return $mail->build()->render(); // @phpstan-ignore-line | return $mail->build()->render(); // @phpstan-ignore-line | ||||
} | } | ||||
/** | /** | ||||
* Sends an email | |||||
* | |||||
* @param Mailable $mail Email content generator | |||||
* @param int|null $tenantId Tenant identifier | |||||
* @param array $params Email parameters: to, cc | |||||
* | |||||
* @throws \Exception | |||||
*/ | |||||
public static function sendMail(Mailable $mail, $tenantId = null, array $params = []): void | |||||
{ | |||||
$class = explode("\\", get_class($mail)); | |||||
$class = end($class); | |||||
$getRecipients = function () use ($params) { | |||||
$recipients = []; | |||||
// For now we do not support addresses + names, only addresses | |||||
foreach (['to', 'cc'] as $idx) { | |||||
if (!empty($params[$idx])) { | |||||
if (is_array($params[$idx])) { | |||||
$recipients = array_merge($recipients, $params[$idx]); | |||||
} else { | |||||
$recipients[] = $params[$idx]; | |||||
} | |||||
} | |||||
} | |||||
return implode(', ', $recipients); | |||||
}; | |||||
try { | |||||
if (!empty($params['to'])) { | |||||
$mail->to($params['to']); | |||||
} | |||||
if (!empty($params['cc'])) { | |||||
$mail->cc($params['cc']); | |||||
} | |||||
$fromAddress = Tenant::getConfig($tenantId, 'mail.from.address'); | |||||
$fromName = Tenant::getConfig($tenantId, 'mail.from.name'); | |||||
$replytoAddress = Tenant::getConfig($tenantId, 'mail.reply_to.address'); | |||||
$replytoName = Tenant::getConfig($tenantId, 'mail.reply_to.name'); | |||||
if ($fromAddress) { | |||||
$mail->from($fromAddress, $fromName); | |||||
} | |||||
if ($replytoAddress) { | |||||
$mail->replyTo($replytoAddress, $replytoName); | |||||
} | |||||
Mail::send($mail); | |||||
$msg = sprintf("[%s] Sent mail to %s%s", $class, $getRecipients(), $params['add'] ?? ''); | |||||
\Log::info($msg); | |||||
} catch (\Exception $e) { | |||||
$format = "[%s] Failed to send mail to %s%s: %s"; | |||||
$msg = sprintf($format, $class, $getRecipients(), $params['add'] ?? '', $e->getMessage()); | |||||
\Log::error($msg); | |||||
throw $e; | |||||
} | |||||
} | |||||
/** | |||||
* Return user's email addresses, separately for use in To and Cc. | * Return user's email addresses, separately for use in To and Cc. | ||||
* | * | ||||
* @param \App\User $user The user | * @param \App\User $user The user | ||||
* @param bool $external Include users's external email | * @param bool $external Include users's external email | ||||
* | * | ||||
* @return array To address as the first element, Cc address(es) as the second. | * @return array To address as the first element, Cc address(es) as the second. | ||||
*/ | */ | ||||
public static function userEmails(\App\User $user, bool $external = false): array | public static function userEmails(\App\User $user, bool $external = false): array | ||||
Show All 19 Lines |