Last active
September 5, 2023 06:23
-
-
Save xnekv03/5489fccf588583b103d2a53645ab19da to your computer and use it in GitHub Desktop.
implementace posílání šablon přes mailtrap v Laravelu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
řádek 23-25 je Laravel HTTP klient, v nette máš určitě něco podobného. Zbytek je přenositelný kamkoliv