使用phpmailer将html表单转换为pdf和电子邮件

时间:2019-06-25 14:43:23

标签: php html

我一直在尝试对此做一些研究,但是找不到一个教程来解释如何做我想做的事情。

我想找到一种方法,能够将html表单(一旦用户填写并点击提交)转换为pdf,然后将该pdf发送给适当的人。

以这种形式为例:

<form id="contact-form" method="post" action="contact.php">

    <input name="name" type="text" id="name" class="form-control" placeholder="Your name" required><br>

    <input name="email" type="email" id="email" class="form-control" placeholder="Your email" required><br>

    <textarea name="message" id="message" class="form-control" placeholder="Message" row="4" required></textarea><br>

    <input type="submit" class="form-control submit" value="SEND MESSAGE">

</form>

这是我目前使用的php:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\PHPMailer-master\src\Exception.php';
require 'C:\PHPMailer\PHPMailer-master\src\PHPMailer.php';
require 'C:\PHPMailer\PHPMailer-master\src\SMTP.php';


$mail = new PHPMailer(TRUE);

try {

   $mail->isHTML(true);
    $mail->setFrom('example@hotmail.com', 'Contact Form');
   $mail->addAddress('example@hotmail.com', 'Lewis');
   $mail->isSMTP();
   $mail->Host = 'smtp.gmail.com';
   $mail->SMTPAuth = TRUE;
   $mail->SMTPSecure = 'tls';
  $mail->Username = '*******';
   $mail->Password = '*****';
   $mail->Port = 587;

   $name = $_POST['name'];
   $email = $_POST['email'];
   $message = $_POST['message'];

       if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'contact form'; 


        $mail->isHTML(true);

    $mail->Body = "There has been a message sent from our contact form from: $name 
    <br>This is their message: $message
    <br>Please respond to them here: $email";




   //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo

            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            header("Refresh:3; url=index.php#contact");
            echo "Message sent! Thanks for contacting us. We aim to respond within 1 working day";
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }




   /* Enable SMTP debug output. */
   $mail->SMTPDebug = 4;

  // $mail->send();


}

catch (Exception $e)
{
  echo $e->errorMessage();
}
catch (\Exception $e)
{
   echo $e->getMessage();

 } 

我需要更改什么,以便表格变成pdf格式,而不是通过电子邮件发送?

谢谢

2 个答案:

答案 0 :(得分:1)

据我了解。您不能只将html转换为pdf。 您将需要构建表单的TCPDF视图。然后将数据和表格合并为pdf,然后将其编码为base64,以将其附加到电子邮件中。

因此,第一部分使用您的TCPDF表单创建一个php文件。

 <?php
   class Contactform {
     public function generate($ctrl, $data) {
     $ctrl->pdf->SetCreator(PDF_CREATOR);}
 ;?>

然后通过组合TCPDF视图和要写入其中的数据来生成表单。

在Codeigniter中,我这样做

     $this->load->library(["pdf", "forms/contactform"]);
     $form = $this->contactform->generate($this, $data);
     $attachment = chunk_split(base64_encode($form));
     $separator = md5(time());

    $message .= "--" . $separator . PHP_EOL;
    $message .= "Content-Type: application/octet-stream; name=\"contact_form.pdf\"" . PHP_EOL;
    $message .= "Content-Transfer-Encoding: base64" . PHP_EOL;
    $message .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
    $message .= $attachment . PHP_EOL . PHP_EOL;
    $message .= "--" . $separator . "--";

包含附件时必须设置内容类型

答案 1 :(得分:0)

万一其他人遇到这个问题,我将分享我如何使它起作用的答案。

首先,我从http://www.fpdf.org/下载了fpdf文件

HTML格式显然保持不变,但是php文件如下:

    def precision_recall_auc_loss(y_true, y_pred):
        y_true = keras.backend.reshape(y_true, (BATCH_SIZE, 1)) 
        y_pred = keras.backend.reshape(y_pred, (BATCH_SIZE, 1))   
        util.get_num_labels = lambda labels : 1
        return loss_layers.precision_recall_auc_loss(y_true, y_pred)[0]

这是一个简单易懂的pdf文件,但意义不大,但要点是它可以正常工作,我认为这对于可能为此感到挣扎的人是一个很好的起点