Doubt with pdf generation and sending via mpdf

1

I have a question. I'm generating a .pdf file from the mpdf library. After generating I saved on the server. Well, I'd like to generate the pdf file and not save on the server and yes, send them to the client's email. Even because the necessary data I save them in the database. I took an example on the web, however, it does not.

The shipping code.

<?php>

//recuperando os dados do cliente

$cliente = $_POST['cli_nome'];
$email_cli = $_POST['cli_email'];

$mpdf=new mPDF();

$mpdf->WriteHTML($pagina);  <<<<

$content = $mpdf->Output('', 'S');

$content = chunck_split(base64_encode($content));
$mailto = $email_cli;
$from_name = 'Comercial SC';
$from_mail = '[email protected]';
//$replyto = '[email protected]';
$uid = md5(uniqid(time()));
$subject = 'Seu Boleto';
$message = 'Olá '. $cliente . '! Obrigado pela preferência.';
$filename = $arquivo;

$header = "From: ".$from_name." <" .$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed: boundary=\"".$uid. "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid. "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .=  "--" .$uid. "\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" .$filename."\"\r\n\r\n";
$header .= $content. "\r\n\r\n";
$header .= "--".$uid."--";
$is_sent = @mail($mailto, $subject, "", $header);

$mpdf->Output();
exit;

?>

You're giving an error here in this line:

$content = chunck_split(base64_encode($content));

In fact, here I create the body of the file .pdf :

//Criando arquivo pdf...
include('pdf/mpdf.php');

$pagina = '  <<<<<
 <html> 
    (...)
    
asked by anonymous 29.05.2018 / 19:40

2 answers

1

GENERATE PDF, ATTACH AND SEND BY EMAIL: // Creating pdf file ... // Instantiating the class ...     include ('pdf / mpdf.php');

$pagina = '
 <html> 
  <!DOCTYPE html>
  <head>
  <style type="text/css">
  .cab_boleto {
   font-size: 20px;
   font-family: Georgia, "Times New Roman", Times, serif;
   }
   table.bordasimples {border-collapse: collapse;
   }
   table.bordasimples tr td {border:1px solid #FF0000;
   }
  </style>
  <title></title>
  </head>

        - PAY ORDER -                FEDERAL ECONOMIC BOX | 104-0      RECEIPT OF THE OUTPUT           Place for deposit: Caixa Econômica e Lotéricas branches.      Date of maturity: '. $ Maturity.'           

Benefited: Shalom Criartes - MEI - CNPJ: XX.XXX.XXX/000X-XX

    

Ag./Op / Account: 00XX-0XX-000XXXX-X

          Address of beneficiary: Street XXXX of XXX, 000 - District XXXXX XXX - Bhte - MG - Cep. XX.XXX-XXX           Date of document: '. $ Today.'     Doc Number: '. $ Ndoc.'     Processing date: '. $ Today.'     Document type: > > > > > R < < <     Document value: R $ '. $ Tg.'        

* * * MR. Cash, not receive after expiration. * * *

    

* * * Deposit referring to order payment in virtual store. * * *

          Depositor: '. $ Client.'           Address: '. $ Address.', '. $ Number.' - '. $ comple.' neighborhood city - '. $ state.' - Cep: '. $ Cep.'                 CPF .: '. $ Cpf.'     Order #: '. $ Numped.'           Note: Kindly, after making deposit, send print receipt with no., Date and time for Whatsapp 9-xxxxxxxx   
  </body>
</html>
';

$ numped="SC". $ order; $ file = $ numped. ".pdf"; $ mpdf = new mPDF (); $ mpdf-> WriteHTML ($ page); $ mpdf-> Output ('pdf / tmp /'. $ file, 'F');

// I - Open in browser // F - Save the file to the server // D - Save the file to the user's computer

// Attaching the .pdf file and sending it to the email informed by the client ...

// recovering client data ... $ client = $ _POST ['cli_name']; $ email_cli = $ _POST ['cliemail'];

// Include the class in your file require_once 'phpmailer / class.phpmailer.php'; require_once 'phpmailer / class.smtp.php';

// require_once ('phpmailer / class.phpmailer.php'); //require_once('phpmailer/class.smtp.php ');

// Instantiate class for sending email $ mail = new PHPMailer (); $ mail-> IsSMTP (); $ mail- > IsHTML (true); $ mail-> CharSet = 'utf-8'; // Set the message charset $ mail-> SMTPAuth = true; // Enable SMTP Authentication $ mail-> Host = 'smtp.gmail.com'; // Define the SMTP server $ mail- > Username = '[email protected]'; // SMTP user account $ mail-> Password = '*********'; // SMTP password account
$ mail-> SMTPSecure = 'tls'; // Enable TLS encryption, ssl accepted $ mail-> Port = 587; // TCP port to connect to

// Realizando o envio do email

try {     // Sender     $ mail- > AddReplyTo ('[email protected] ',' My Name ');     $ mail-> SetFrom ('[email protected] ',' My Name ');

// Destinatário
$mail->AddAddress($email_cli, $cliente);

// Assunto
$mail->Subject = 'Boleto de Pagamento';

// Mensagem para clientes de email sem suporte a HTML
$mail->AltBody = 'Olá '.$cliente . " segue em anexo o boleto para o pagamento do seu pedido";

// Mensagem para clientes de email com suporte a HTML
$mail->MsgHTML('<p>Olá ' . $cliente . ' ! Obrigado pela preferência! <br> Segue em anexo o boleto para o pagamento.</p>');

// Adicionar anexo
$caminho = 'pdf/tmp/';
$ficheiro = $arquivo;

$mail->AddAttachment($caminho.$ficheiro);

// Enviar email
$mail->Send();

//echo "E-mail enviado com sucesso!";
echo('<script type="text/javascript">alert("O boleto foi enviado para o e-mail informado!")</script>');
session_destroy();
exit ($refresh);
}
catch (phpmailerException $e) {
// Mensagens de erro do PHPMailer
echo $e->errorMessage();
}
catch (Exception $e) {
// Outras mensagens de erro
echo $e->getMessage();

}

? >

// Note: I took this code on the web and adapted the same for my case.

    
01.06.2018 / 21:16
0

You can do it this way:

  • Save to server in temporary folder
  • Email attached with your own email service
  • Finally, delete the temporary folder file on the server
29.05.2018 / 19:44