To send an email with an attachment, where .pdf
is in base64
format is easy, you need reverse this base64
to a file and send it normally via the attachment with the class Mail from laravel
I'm doing as if you've already gotten the data from this API
:
Route::get('emailpdf', function()
{
$data = "_dados_da_string_base64_arquivo_tipo_pdf";
//gerando um nome para o arquivo
$pathToFile = 'temp/'.uniqid().'.pdf';
//salvar a string em uma pasta temporária para servir de anexo
//com extensão e tipo PDF
file_put_contents($pathToFile, base64_decode($data));
//classe que envia o email
\Illuminate\Support\Facades\Mail::send('email', [],
function($message) use ($pathToFile)
{
$message->to('[email protected]');
//anexando o arquivo criado na pasta temporária
$message->attach($pathToFile);
});
//excluindo o arquivo da pasta temporária
unlink($pathToFile);
return "Email enviado com sucesso";
});
I'm waiting for some user message to further improve the answer !
Edition:
Customer wants a link to be sent:
In the template email
put something like this, use the helper
asset / a>:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
<div class="flex-center position-ref full-height">
<h3>Email enviado com sucesso</h3>
<p>
<a href="{{asset($link_pdf)}}">PDF Link</a>
</p>
</div>
</body>
</html>
and in the code make these changes, sending to View
the link created and how this file can not be deleted removed the last line:
Route::get('emailpdf', function()
{
$data = "_dados_da_string_base64_arquivo_tipo_pdf";
//gerando um nome para o arquivo
$pathToFile = 'temp/'.uniqid().'.pdf';
//salvar a string em uma pasta temporária para servir de anexo
//com extensão e tipo PDF
file_put_contents($pathToFile, base64_decode($data));
//classe que envia o email
\Illuminate\Support\Facades\Mail::send('email',['link_pdf' => $pathToFile],
function($message) use ($pathToFile)
{
$message->to('[email protected]');
//anexando o arquivo criado na pasta temporária
$message->attach($pathToFile);
});
return "Email enviado com sucesso";
});
In this case send the attachment and a link to the site, would not recommend the link, but, reported by you client thing.