Skip to content

Instantly share code, notes, and snippets.

@xnekv03
Last active September 5, 2023 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnekv03/5489fccf588583b103d2a53645ab19da to your computer and use it in GitHub Desktop.
Save xnekv03/5489fccf588583b103d2a53645ab19da to your computer and use it in GitHub Desktop.
implementace posílání šablon přes mailtrap v Laravelu
public function sendTemplate(string $templateUuid, string $toEmail, array $variables = []): bool
{
if (!filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {
Log::error('invalid e-mail address provided ' . $toEmail);
return false;
}
$data = [
"from" => [
"email" => config('mail.from.address'),
"name" => config('mail.from.name')
],
"to" => [
[
"email" => $toEmail
]
],
"template_uuid" => $templateUuid,
"template_variables" => $variables
];
$response = Http::withToken(config('custom.mailtrap_token'))->withHeader('Content-Type', 'application/json')->post('https://send.api.mailtrap.io/api/send',
empty($variables) ? Arr::except($data, 'template_variables') : $data
);
$success = $response->json('success') === true;
$success ? Log::info('Template sent ' . $response->body()) : Log::error('Template sending failed ' . $response->body());
return $success;
}
@xnekv03
Copy link
Author

xnekv03 commented Sep 5, 2023

řádek 23-25 je Laravel HTTP klient, v nette máš určitě něco podobného. Zbytek je přenositelný kamkoliv

@xnekv03
Copy link
Author

xnekv03 commented Sep 5, 2023

Arr::except - helper laravelu, pokud šablona nemá žádné proměnné, vůbec je v requestu neposílám

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment