生成pdf并通过电子邮件发送

时间:2020-06-29 16:09:55

标签: php laravel

我正在尝试使用laravel中的tcpdf生成pdf并通过电子邮件发送,该pdf的内容将是boleto。但是保存pdf时出错。

生成pdf

 $pdf = new TCPDF();
                $pdf->AddPage();
                $pdf->Write(1, 'Hello world');
    
                $pdf->Output('/techbank/public/boletos/cobranca/'.$email-$datavencimento.'.pdf', 'F');

发送邮件

$mail = new PHPMailer(true);

    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();
    $mail->CharSet = 'UTF-8';                                            // Send using SMTP
    $mail->Host       = '';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '';                     // SMTP username
    $mail->Password   = '';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $local = '/opt/lampp/htdocs/techbank/public/boletos/cobranca/'.$email-$datavencimento.'pdf';
    //Recipients
    $mail->addStringAttachment(file_get_contents($local), 'cobranca.pdf');
    $mail->setFrom('', 'title');
    $mail->addAddress($email, $nome);     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Cust';
    $mail->Body    = '
    TEXT

    $mail->send();

        return redirect('cobrancas');


        }catch (\Exception  $e){
            dd($e);
        }

错误:

> The behavior of unparenthesized expressions containing both '.' and
> '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence

1 个答案:

答案 0 :(得分:1)

我相信警告与这条线有关。

$local = '/opt/lampp/htdocs/techbank/public/boletos/cobranca/'.$email-$datavencimento.'pdf';

它希望您使用括号将它认为是数学计算的内容括起来。

$local = '/opt/lampp/htdocs/techbank/public/boletos/cobranca/'.($email-$datavencimento).'pdf';

除非您真正的意思是:

$local = '/opt/lampp/htdocs/techbank/public/boletos/cobranca/'.$email.'-'.$datavencimento).'pdf';

编辑:第一个代码块中也存在相同的问题。

$pdf->Output('/techbank/public/boletos/cobranca/'.$email-$datavencimento.'.pdf', 'F');