56 lines
2.5 KiB
PHP
56 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\MailFlow;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class ConventionalTransport
|
|
{
|
|
public function send(array $payload): array
|
|
{
|
|
$config = config('mail');
|
|
foreach (['host', 'port', 'username', 'password', 'encryption'] as $key) {
|
|
$config[$key] = admin_setting('email_' . $key, $config[$key] ?? null);
|
|
}
|
|
$driver = $config['driver'] ?? 'smtp';
|
|
$config['transport'] = $driver;
|
|
$config['timeout'] = 8;
|
|
if (($config['encryption'] ?? null) === 'ssl') {
|
|
$config['scheme'] = 'smtps';
|
|
}
|
|
if (($config['encryption'] ?? null) === 'tls') {
|
|
$config['require_tls'] = true;
|
|
}
|
|
$from = admin_setting('email_from_address', $config['from']['address']);
|
|
$name = admin_setting('app_name', $config['from']['name']);
|
|
// Build an isolated mailer: cached worker mailers never inherit another message's credentials.
|
|
$sent = Mail::build($config)->html($payload['html'], function ($message) use ($payload, $from, $name) {
|
|
$message->from($from, $name)->to($payload['to'])->subject($payload['subject']);
|
|
if (!empty($payload['reply_to'])) {
|
|
$message->replyTo($payload['reply_to']);
|
|
}
|
|
foreach ($payload['attachments'] ?? [] as $attachment) {
|
|
$bytes = base64_decode($attachment['content'], true);
|
|
if ($bytes === false) { throw new \RuntimeException('mail_attachment_invalid'); }
|
|
$part = new \Symfony\Component\Mime\Part\DataPart($bytes, $attachment['filename'], $attachment['content_type'] ?? null);
|
|
if (!empty($attachment['content_id'])) { $part->asInline()->setContentId($attachment['content_id']); }
|
|
$message->getSymfonyMessage()->addPart($part);
|
|
}
|
|
foreach ($payload['headers'] ?? [] as $key => $value) {
|
|
if (in_array(strtolower($key), ['in-reply-to', 'references'], true)) {
|
|
$message->getSymfonyMessage()->getHeaders()->addIdHeader($key, trim($value, '<>'));
|
|
} else {
|
|
$message->getSymfonyMessage()->getHeaders()->addTextHeader($key, $value);
|
|
}
|
|
}
|
|
});
|
|
if (!$sent) {
|
|
throw new \RuntimeException('mail_send_cancelled');
|
|
}
|
|
return [
|
|
'status' => in_array($driver, ['log', 'array'], true) ? 'simulated' : 'submitted',
|
|
'provider_id' => $sent->getMessageId(),
|
|
];
|
|
}
|
|
}
|