Send .pdf, in base64, by email

1

I get a string through an 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 API that was encrypted in .pdf and I need to send it in e-mail to the client when it requests, and the base64 link should be sent instead of the attached file. / p>

Just to note, I work with .pdf , using PHP .

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

  • If I try to use Javascript , the email client simply ignores the existence of the link, some clients even display the link. ( and to tell you the truth, I do not even know if such a command is possible)

  • Is there any way I can send this <a href='data:application/pdf," + codigo_base64)'> by email?

    Edition: Unfortunately, the client wants the download link to be sent, not the attached file.

        
    asked by anonymous 01.11.2016 / 19:58

    1 answer

    0

    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

    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.

        
    01.11.2016 / 20:31