无法使用PHP发送带有表单数据的pdf附件

时间:2013-07-30 18:10:32

标签: php

我有一个.html表单,需要填写数据并上传.pdf附件。 .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 = "aaaa.aaaa@aaa.com"; // 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>

1 个答案:

答案 0 :(得分:0)

您没有从$ _POST正确获取值。

错:

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

右:

$requestor=$_POST["requestor"];

您还应该清理和/或验证所有输入。