2011-12-27 5 views
4

SwiftMailer를 사용하는 법을 배우고 있으며 첨부 파일이 입력 유형에서 작동하는 데 문제가 있습니다. 난 단지 필요가 없습니다 : 여기SwiftMailer를 사용하는 PHP 첨부 파일

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

다음

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

것은 [27-Dec- 다음

<?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> 
나의 새로운 오류 내 전체 코드입니다 2011 14:51:58] PHP Deprecated :/apps/M에서 set_magic_quotes_runtime() 함수는 더 이상 사용되지 않습니다. AMP/htdocs/paipdf2/dompdf/lib/class.pdf.php 온라인 4332

[27-12 월 2011 14:51:58] PHP deprecated :/application/MAMP/htdocs에서 set_magic_quotes_runtime() 함수는 더 이상 사용되지 않습니다. require_once를 (/ 응용/MAMP/htdocs에/paipdf2/DOMPDF/포함/swift_message_attachment : 4,348 라인

[27 ~ 12 2011 14시 51분 58초]가 PHP의 경고에 /paipdf2/dompdf/lib/class.pdf.php .cls.php) [function.require-once] : 스트림을 열지 못했습니다 : /Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config.inc.php 온라인에없는 파일이나 디렉토리가 없습니다. 194

[27- 2011 년 12 월 14 일 14시 51 분 58 초] PHP 치명적인 오류 : require_once() [function.require] : 열 필요 없음 '/ Applications/MAMP/htdocs/pai/Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config에 pdf2/dompdf/include/swift_message_attachment.cls.php '(include_path ='. :/응용 프로그램/MAMP/bin/php/php5.3.6/lib/php ' inc.php on line 194

+3

의 중복 가능성 [스위프트 메일러 첨부] (http://stackoverflow.com/questions/4667822/swift-mailer-attachments) - 업로드 된 파일을 참조하시기 바랍니다 간다 어디 이해하기 : http://php.net/manual/en/features.file-upload.php – hakre

답변

1

나는 $file$filename이 파일 경로와 이름의 실제 값을 가지고 있지 않다고 추측하고 있습니다.

$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

+0

코드가 두 번째'if' 문에 의해 지나가고 있습니까? 또한 오류나 경고가 표시되는 경우 공유하십시오. –

+0

이것은 PHP 오류를받은 유일한 오류입니다. 구문 오류, 예기치 않은 T_OBJECT_OPERATOR/49 행의 /Applications/MAMP/htdocs/paipdf2/form.php – ehilse

1

아니라,이 양식 게시물을 가정

그렇게처럼 $_FILES 자동 전역 배열을 사용할 필요가 업로드 된 파일의 경로에 액세스하려면 제대로 수행 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));