2011-12-27 53 views
4

我正在學習使用SwiftMailer,並且我無法使附件從 輸入類型工作。我不應該只需要:使用SwiftMailer PHP附件

<input name="attachment" id="attachment" type="file"> 

然後

$message = Swift_Message::newInstance() 
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename,)); 

這裏是我完整的代碼

<?php 
if (!empty($_POST)) { 

$success = $error = false; 

$post = new stdClass; 

$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file. 

$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.   

foreach ($_POST as $key => $val) 
    $post->$key = trim(strip_tags($_POST[$key])); 

    if (empty($post->name) OR empty($post->email)) 
    $error = true; 

else { 

    $dir = dirname(__FILE__); 

    ob_start(); 
    require_once($dir.'/pdf.php'); 
    $pdf_html = ob_get_contents(); 
    ob_end_clean(); 

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

    $dompdf = new DOMPDF(); 
    $dompdf->load_html($pdf_html); 
    $dompdf->render(); 
    $pdf_content = $dompdf->output(); 

    ob_start(); 
    require_once($dir.'/html.php'); 
    $html_message = ob_get_contents(); 
    ob_end_clean(); 

    require_once($dir.'/swift/swift_required.php'); 

    $mailer = new Swift_Mailer(new Swift_MailTransport()); 

    $message = Swift_Message::newInstance() 
        ->setSubject('Order Entry') // Message subject 
        ->setTo(array('[email protected]' => 'Eric Hilse')) // Array of people to send to 
        ->setFrom(array('[email protected]' => 'PAi Order Entry')) 
        ->setBody($html_message, 'text/html') // Attach that HTML message from earlier 
        ->attach(Swift_Attachment::newInstance($pdf_content, 'design.pdf', 'application/pdf')); // Attach the generated PDF from earlier 
        ->attach(new Swift_Message_Attachment(new Swift_File($file), $filename)); 
    // Send the email, and show user message 
    if ($mailer->send($message)) 
     $success = true; 
    else 
     $error = true; 

} 

} 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>PAi Cap &amp; Tee Art Pack Order Form</title> 
<link href="css/style.css" rel="stylesheet" type="text/css" /> 
</head> 

<body> 

<div id="container"> 
      <div id="logo"> 
       <img src="images/pailogo.png" id="logo" /> 
      </div> 
     <h1><abbr title="Paramount Apparel International">PAi</abbr> Cap &amp; Tee Art Pack Order Form</h1> 

     <?php if ($success) { ?> 
      <div class="message success"> 
       <h4>Congratulations! It worked! Your order has been sent.</h4> 
      </div> 
     <?php } elseif ($error) { ?> 
      <div class="message error"> 
       <h4>Sorry, an error occurred. Try again!</h4> 
      </div> 
     <?php } ?> 

     <form method="post" action="" enctype="multipart/form-data"> 
      <fieldset> 
       <legend>Please fill in the following form:</legend> 
        <label for="name">Your Name:<span class="required">*</span></label> 
        <input type="text" name="name" id="name" class="input" /> 

        <label for="email">Your Email:<span class="required">*</span></label> 
        <input type="text" name="email" id="email" class="input" />  

     <br /> 

        <label for="artpacknumber">Art Pack Number:<span class="required">*</span></label> 
        <input type="text" name="artpacknumber" id="artpacknumber" class="input" />   

        <label for="language">New Or Revise?<span class="required">*</span></label> 
        <select name="language" id="language"> 
         <option value="NEW">New</option> 
         <option value="REVISE">Revise</option> 
        </select> 

     <br /> 

        <label for="duedate">Due Date:<span class="required">*</span></label> 
        <input type="text" name="duedate" id="duedate" class="input" /> 

        <label for="dateentered">Date Entered:<span class="required">*</span></label> 
        <input type="text" name="dateentered" id="dateentered" class="input" /> 

     <br /> 

        <label for="designname">Design Name:<span class="required">*</span></label> 
        <input type="text" name="designname" id="designname" class="input" /> 

     <br /> 

        <label for="customer">Customer:<span class="required">*</span></label> 
        <input type="text" name="customer" id="customer" class="input" /> 

        <label for="account">Account:<span class="required">*</span></label> 
        <input type="text" name="account" id="account" class="input" /> 

     <br /> 

        <label for="salesrep">Sales Rep:<span class="required">*</span></label> 
        <input type="text" name="salesrep" id="salesrep" class="input" /> 

        <label for="extension">Extension:<span class="required">*</span></label> 
        <input type="text" name="extension" id="extension" class="input" /> 

     <br /> 

        <label class="commentslabel" for="comments">Comments:<span class="required">*</span></label> 
        <textarea name="comments" id="comments" rows="4" cols="40"></textarea> 

     <br /> 

        <label for="attachment">File Upload<br />.</label> 
        <input name="attachment" id="attachment" type="file"> 

     <br />   

        <input type="submit" class="submit" id="submit" value="Submit" /> 

     </fieldset> 

    </form> 

</div> 

這是我的新的錯誤 [27癸 - 2011 14:51:58] PHP已棄用:函數set_magic_quotes_runtime()在/ Applications/M中被棄用AMP/htdocs/paipdf2/dompdf/lib/class.pdf.php on line 4332

[27-Dec-2011 14:51:58] PHP已棄用:函數set_magic_quotes_runtime()在/ Applications/MAMP/htdocs /paipdf2/dompdf/lib/class.pdf.php on line 4348

[27-Dec-2011 14:51:58] PHP Warning:require_once(/ Applications/MAMP/htdocs/paipdf2/dompdf/include/swift_message_attachment .cls.php)[function.require-once]:無法打開流:沒有這樣的文件或目錄在/ 194/012/2011年12月14:51:58] PHP致命錯誤:require_once()[function.require]:無法打開所需的'/ Applications/MAMP/htdocs/pai pdf/dompdf/include/swift_message_attachment.cls.php'(include_path ='。:/ Applications/MAMP/bin/php/php5.3.6/lib/php')/ Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config。 inc.php上線194

+3

的可能重複[斯威夫特梅勒附件(http://stackoverflow.com/questions/4667822/swift-mailer-attachments) - 來理解上傳的文件去,請參閱:http://php.net/manual/en/features.file-upload.php – hakre 2011-12-27 17:58:22

回答

1

我猜$file$filename不帶有文件路徑和名稱的實際值。

假設形式發佈做得正確,訪問上傳文件的路徑,你必須使用$_FILES超全局數組,像這樣:

$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file. 
$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file. 
$message = Swift_Message::newInstance() 
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename)); 

文檔:http://www.php.net/manual/en/reserved.variables.files.php

+0

我添加了我的完整代碼與你說的,由於某種原因,它徹底打破了它。你能說出我做錯了什麼嗎? – ehilse 2011-12-27 19:24:15

+0

你確定你的代碼正在被第二個if語句移過去嗎?此外,如果出現任何錯誤或警告,請分享。 – 2011-12-27 19:47:21

+0

這是我收到PHP解析錯誤的唯一錯誤:語法錯誤,第49行的/Applications/MAMP/htdocs/paipdf2/form.php中出現意外的T_OBJECT_OPERATOR – ehilse 2011-12-27 20:05:01

1

好,有在包含Swiftmailer之前包含DOMpdf時出現問題。

我還沒有找到它爲什麼是錯的,但我確實有一個解決方案。

改變這種和平的代碼在 'dompdf_config.inc.php':

function DOMPDF_autoload($class) { 
    $filename = mb_strtolower($class) . ".cls.php"; 
    require_once(DOMPDF_INC_DIR . "/$filename"); 
} 

這樣:

function DOMPDF_autoload($class) { 
    $filename = mb_strtolower($class) . ".cls.php"; 
    if (file_exists(DOMPDF_INC_DIR . "/$filename")) { 
    require_once(DOMPDF_INC_DIR . "/$filename"); 
    } 
} 

Afther這種改變它的工作原理,你可以用郵寄的swiftmailer PDF。

額外信息。我該如何開始DOMPDF:

require_once("lib/dompdf/dompdf_config.inc.php"); 
spl_autoload_register('DOMPDF_autoload'); 
$dompdf = new DOMPDF(); 
$dompdf->load_html($pdfhtml); 
$dompdf->set_paper('a4', 'portrait'); 
$dompdf->render(); 
$dompdf->stream("invoice", array('Attachment'=>0));