Send . pdf, in Base64, by e-mail

Asked

Viewed 1,144 times

1

I get a string through a API external, which comes directly from the client (I only take care of the website, making it impossible to change the type of information I receive). This string is a .pdf which was encrypted in base64 and I need to send it in email to the customer, when he requests, and should be sent the link .pdf, and not the attached file.

For the record, I work with PHP, using Laravel.

  1. If I try to give Decode in this string, it generates a giant stream, but how would I generate the link with that stream?

  2. If I use the command window.open("data:application/pdf," + codigo_base64); in the Javascript, I can open the .pdf perfectly in another browser tab, but, as I said, I need to send the same by email, thus losing any functionality to use Javascript. Or am I wrong?

  3. If I try to use <a href='data:application/pdf," + codigo_base64)'>, the email client simply ignores the existence of the link, some client even display the link. (and to tell the truth, I don’t even know if such a command is possible)

Is there any way to send that .pdf by e-mail?

Editing: Unfortunately, the client wants the download link sent, not the attached file.

  • If you turn again into pdf file stores in a temporary folder, makes an email attaches that item and send then erases the time? how about ?

  • What is the version of your Laravel?

  • 1

    The problem is that my client wants the download link sent, not the attached file (I forgot to mention this). I am currently using version 5.2.

1 answer

0


To send an e-mail with attachment, where the .pdf is in format base64 is easy, precise reverse that base64 for a physical file, record in a temporary folder and normally send via an attachment with class Mail of

I’m doing as if you’ve already gotten the die of 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 am waiting for some user message to improve the reply!


Editing:

Client wants a link sent:

In the template email put something like this, use the template helper Asset:

<!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 by sending to View the link created and as this file cannot be deleted I 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 sends the attachment and a link to the site, would not recommend the link, but, reported by you customer thing.

  • It’s not exactly what I need (because the client wants a link, not an attachment), but the function works perfectly to fire an attachment via email. In the worst case, I’ll try to convince you to send the attachment!

  • @lmartim but even so is easy to solve also with a link I will do the editing and put with link too.

  • 1

    Then you managed to solve my problem, I did not have the knowledge of this library Assets! Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.