2013-07-30 55 views
0

我有一個.html表單,它需要填寫數據和.pdf附件才能上傳。 .pdf和數據需要通過電子郵件發送。無法使用PHP發送帶有表單數據的pdf附件

代碼我已經成功檢查.pdf,但是當我收到電子郵件時,電子郵件來自我們的一個Apache郵件服務器而不是發件人,電子郵件爲空..沒有表單數據和附件被剝離並替換爲空白.txt

有人可以看看我的代碼,讓我知道我在做什麼錯了嗎?我沒有看到它。

PHP代碼:

<?php 

$fileInfo = new finfo(FILEINFO_MIME_TYPE); 
$fileMime = $fileInfo->file(@$_FILES["file"]["tmp_name"]); 
if($fileMime == "application/pdf") 
{ 
//Requestor 
$requestor=$_POST["$requestor"]; 

//Requestor Phone 
$telephone=$_POST["$telephone"]; 

//Requestor Email 
$mail_from=$_POST["$req_mail"]; 

//Document Subject 
$subject=$_POST["$subject"]; 

// Services Date 
$req_date=$_POST["req_date"]; 

//Addintional Info 
$info=$_POST["body"]; 

$email_to = "[email protected]"; // Who the email is too 

$headers = "From: ".$mail_from; 

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    $headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$message . "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$message .= "--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" . 
"--{$mime_boundary}--\n"; 

$message .="Request From: ".$requestor."\n"."Contact Phone: ".$telephone."\n"."Contact Email: ".$mail_from."\n"."Project/Mission Name: ".$subject."\n"."Need by Date: ".$req_date."\n"."Details: ".$info; 

$ok = @mail($email_to, $subject, $message, $headers); 

if($ok) 
{ 
    header("Form & attachment sent successfully"); 
} 
    else 
    { 
    die("Sorry but the email could not be sent. Please go back and try again!"); 
    } 
} 
else 
{ 
header("Your attachment must be a .pdf"); 
}  
?> 

,這是我的.html:

<form action="sendreview.php" method="post" enctype="multipart/form-data"> 
<table width="99%" border="0" cellspacing="1" cellpadding="3"> 

<tr> 
    <td width="40%">Requestor Name</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield1"><input name="requestor" type="text" id="requestor" size="50" /><span class="textfieldRequiredMsg">A name is required.</span><span class="textfieldInvalidFormatMsg">A name is required.</span></span></td> 
</tr> 

<tr> 
    <td width="40%">Requestor Number</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield2"> 
    <input name="telephone" type="text" id="telephone" size="50" /> 
    <span class="textfieldRequiredMsg">A phone number is required.</span> 
    <span class="textfieldInvalidFormatMsg">Invalid format 000-000-0000.</span></span> </td> 
</tr> 

<tr> 
    <td width="40%">Requestor Email</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="sprytextfield3"> 
<input name="req_mail" type="text" id="req_mail" size="50" /> 
<span class="textfieldRequiredMsg">An email address is required.</span><span class="textfieldInvalidFormatMsg">A valid email address is required.</span></span> 
    </td> 
</tr> 

<tr> 
    <td width="40%">Project/Mission:</td> 
    <td width="2%">:</td> 
    <td width="58%"><span id="spryselect1"> 
    <select name="subject" id="subject"> 
    <optgroup label="TEST"> 
     <option value="1">1</option> 
     <option value="2">2</option> 
    </optgroup> 
    </select> 
    <span class="selectRequiredMsg">Please select an item.</span></span></td> 
</tr> 

<tr> 
    <td width="36%">Need by Date</td> 
    <td width="2%">:</td> 
    <td width="62%"> 
    <div id="OAWidget"></div> 
    <script type="text/javascript"> 
    // BeginOAWidget_Instance_2443523: #OAWidget 

    document.body.className += " yui-skin-sam"; 
    YAHOO.namespace("widget.Calendar"); 

    YAHOO.init_OAWidget = function() { 
    var oCalendar_OAWidget = new YAHOO.widget.Calendar("oCalendar_OAWidget","OAWidget", 
    {selected: "", 
    mindate: null, 
     maxdate: null, 
     title: "", 
    close: false, 
     iframe: true, 
     multi_select: false, 
     navigator: false, 
     show_weekdays: true, 
    locale_weekdays: "short", 
     start_weekday: 0, 
     show_week_header: false, 
     show_week_footer: false, 
     hide_blank_weeks: false } 
    ); 
    oCalendar_OAWidget.render(); 
    } 
    YAHOO.util.Event.onDOMReady(YAHOO.init_OAWidget); 

    // EndOAWidget_Instance_2443523 
     </script> 
</tr> 

<tr> 
    <td>Message:</td> 
    <td>:</td> 
    <td><span id="sprytextarea1"> 
    <textarea name="body" cols="50" rows="4" id="body"></textarea> 
    <span class="textareaRequiredMsg">A value is required.</span></span></td> 
</tr> 

<tr> 
    <td>Attach File</td> 
    <td>:</td> 
    <td><input type="file" name="file" id="file"> File type must be a PDF</td> 
</tr> 

<tr> 
    <td></td> 
    <td></td> 
    <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> 
</tr> 
</table> 
</form> 
+0

不要自己構建mime電子郵件。該郵件構建代碼的ALl可以用4或5行swiftmailer或phpmailer替換。另外,你的'header()'調用是完全錯誤的... –

+0

謝謝,我回到使用SwiftMailer,並能夠讓所有的東西都能正常工作。 – Kim

回答

0

您沒有正確地從$ _ POST獲取值。

錯誤:

$requestor=$_POST["$requestor"]; 

右:

$requestor=$_POST["requestor"]; 

你也應該消毒和/或驗證所有輸入。

+0

謝謝,我注意到我發佈後(D'oh!),我想回到使用SwiftMailer,並能夠讓所有的東西都起作用。 – Kim

相關問題