简体   繁体   English

使用dompdf将文件附加到php电子邮件中

[英]Attach file in php email using dompdf

I am generating dpf and sending it in email and its working fine for me but I just want to attach the files which uploaded to my html form. 我正在生成dpf并将其发送到电子邮件中,并且对我来说工作正常,但我只想附加上载到我的html表单中的文件。 I am using dompdf. 我正在使用dompdf。

Here is my code: 这是我的代码:

$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];

$target = "uploads/"; 
$target1 = $target . basename($_FILES['pass_doc']['name']); 
//Here we check that $ok was not set to 0 by an error 
move_uploaded_file($_FILES['pass_doc']['tmp_name'], $target1);
$pass_doc = $_FILES['pass_doc']['name'];

$dir = dirname(__FILE__);

$pdf_html = "Name: $title $fname";

require_once($dir.'/dompdf/dompdf_config.inc.php');

        $dompdf = new DOMPDF(); // Create new instance of dompdf
        $dompdf->load_html($pdf_html); // Load the html
        $dompdf->render(); // Parse the html, convert to PDF
        $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

        // Get the contents of the HTML email into a variable for later
        ob_start();
        require_once($dir.'/html.php');
        $html_message = ob_get_contents();
        ob_end_clean();

        // Load the SwiftMailer files
        require_once($dir.'/swift/swift_required.php');

        $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer

        $message = Swift_Message::newInstance()
                       ->setSubject('Student Inquiry from lsbuk.com') // Message subject
                       ->setTo(array('myemail@example.com')) // Array of people to send to
                       ->setFrom(array('no-reply@example.com' => 'LSBUK')) // From:
                       ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                       ->attach(Swift_Attachment::newInstance($pdf_content, 'studentadmission.pdf', 'newstudent/pdf')); // Attach the generated PDF from earlier

        // Send the email, and show user message
        if ($mailer->send($message))
            $success = true;
        else
            $error = true;

Just want to know how can I attach $pass_doc also ? 只想知道我还可以附加$ pass_doc吗? generated pdf working fine and coming in the email but not $pass_doc 生成的pdf可以正常工作并且可以通过电子邮件发送,但不能通过$ pass_doc

Thanks 谢谢

I fixed it already by just putting the code bellow: 我已经把下面的代码解决了:

->attach(Swift_Attachment::fromPath($pass_doc_path))

Anyway Thanks 还是谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM