2012-07-10 86 views
0

以下是我的PHP腳本,它處理來自HTML表單的輸入。該腳本很好用,但經過嘗試和嘗試後,我無法獲得簡單的舊PHP代碼來處理表單中的附件。將附件添加到電子郵件表單:集成PHPmailer以處理附件

在閱讀了幾天並測試不同的代碼後,每個人都建議使用預先製作的應用程序來簡化附件。 (我選的PHPMailer。)

我試圖得到它的工作,但我能看到的是靜態文件的例子:

從PHPMailer的自述例子:

$郵件 - > AddAttachment( 「C:\ TEMP \ JS-bak.sql」); //添加附件

$ mail-> AddAttachment(「c:/temp/11-10-00.zip」);

我的問題是,它有可能使用HTML文件中的「文件」選擇器附加文件? 如果是這樣,我可以將PHPmailer附件代碼集成到我的生產腳本中,而無需重新編碼整個文件?

我需要從我的表單附加哪些代碼?

我當前的腳本:

<?php 

require("phpmailer.inc.php"); 
$email = new phpmailer; 
$uploaddir = 'uploads/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    $email->AddAttachment($uploadfile); 
} else { 
    echo "File failed to attach. Maximum size of 5mb\n";  
} 

if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "IT Request (Support)"; //Email Subject 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation: expected data already exists 
    if(!isset($_POST['name']) || 
    !isset($_POST['datereg']) || 
    !isset($_POST['message']) || 
    !isset($_POST['email'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $name = $_POST['name'];     //Required 
    $request_date = $_POST['datereg'];  //Required 
    $email_address = $_POST['email'];  //Required 
    $message = $_POST['message'];   //Not required 
    $error_message = "";  
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_address)) { 
     $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[\$A-Za-z0-9._%-]/"; 
    if(!preg_match($string_exp,$name)) { 
     $error_message .= 'The Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$request_date)) { 
     $error_message .= 'The Request Date you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$message)) { 
     $error_message .= 'You must list a description in order to submit the form.<br />'; 
    }  
    if(strlen($error_message) > 0) { 
     died($error_message); 
    } 
    $email_message = "---IT Request Form Content---\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    //These fields are required per validation so they don't need to be tested. 
    $email_message .= "Name: ".clean_string($name)."\n"; 
    $email_message .= "Email: ".clean_string($email_address)."\n"; 
    $email_message .= "Date of Request: ".clean_string($request_date)."\n"; 
    $email_message .= "Description: ".clean_string($message)."\n"; 

    // create email headers 
    $headers = 'From: '.$email_address."\r\n". 
    'Reply-To: '.$email_address."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 
?> 

    <!-- include your own success html here --> 

    Thank you for submitting an IT Support Request. Your information will be reviewed as soon as possible. 
    <META HTTP-EQUIV="refresh" CONTENT="2;URL=http://getsupport/index.html"> 

<?php 
} 
?> 
+0

文件上傳完成後,它就是磁盤上的常規文件。所以你只需要像其他文件一樣附加它。 – arnaud576875 2012-07-10 15:05:58

+0

別忘了照常做:檢查上傳的文件類型是否正確,不要太大,存檔關閉/刪除一次等附件。 – Waygood 2012-07-10 15:09:19

+1

$ email-> AddAttachment($ _ FILES ['userfile'] ['tmp_name' ],$ _FILES ['userfile'] ['name']); $ email是你的PHPmailer類的一個實例。 – Waygood 2012-07-10 15:13:50

回答

0

你會找到路徑到臨時文件,並在$_FILES變量的文件的名稱。看看the documentation

+0

爲了簡單起見,我正在跳過size \ type驗證。 工作完成後,我會重新添加它。是否需要附加功能? – CWB 2012-07-10 15:13:52