PHP表单邮件附件

时间:2014-06-11 13:29:39

标签: php email attachment email-attachments

我正在尝试修复我正在处理的其中一种形式。我不是开发人员所以其他问题,因为我的代码与那里的代码看起来很不一样。

我正在看的基本功能是我有一个包含多个文件上传字段的表单。

这是基本的前端代码:

 <form action="mailer.php" method="post" name="mainform" enctype="multipart/form-data">
    <table width="500" border="0" cellpadding="5" cellspacing="5">
       <tr>
        <th>Name:* </th>
        <td><input name="FName" type="text"></td>
    </tr>
    <tr>
    <tr>
        <th>Email:* </th>
        <td><input name="YEmail" type="text"></td>
    </tr>
    <tr>
        <th>Side 1 Text:* </th>
        <td><input name="S1Text" type="text"></td>
    </tr>
 <tr>
        <th>Side 2 Text:* </th>
        <td><input name="S2Text" type="text"></td>
    </tr>
    <tr>
        <th>Poker Chip:* </th>
        <td><select name="PokerChip" id="PokerChip">
        <option value="8 Stripe Inlay">8 Stripe Inlay</option>
        <option value="Hot Stamp Chip">Hot Stamp Chip</option>
        <option value="6 Stripe Direct Print">6 Stripe Direct Print</option>
        <option value="Clean Slate Ceramic">Clean Slate Ceramic</option>
        <option value="6 Stripe Deluxe Print">6 Stripe Deluxe Print</option>
        <option value="Dice Full Color Print">Dice Full Color Print</option>
        <option value="High Roller Rectangle">High Roller Rectangle</option>
        <option value="Tri-Color Clay">Tri-Color Clay</option>
        <option value="Pro Clay Hot Stamp">Pro Clay Hot Stamp</option>
            </select>
        </td>
    </tr>
    <tr>
        <th>Comments: </th>
        <td><textarea name="SpecialInstructions" cols="20" rows="4" id="SpecialInstructions"></textarea></td>
    </tr>
    <tr>
      <th>Upload File 1:</th>
      <td><input name="attachment" type="file">(File must be 10 MB or less)</td>
    </tr>
     <tr>
      <th>Upload File 2:</th>
      <td><input name="attachment2" type="file">(File must be 10 MB or less)</td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"></td>
    </tr>
    </table>
    </form>

现在我的mailer.php文件中的代码是:

 <?php

$to_Email = "xxx"; //Replace with recipient email address
$subject = 'Virtual Proof Request Email';
$fromEmail = $_POST['YEmail']; 
$fromName = $_POST['FName']; 
$sidetext1 = $_POST['S1Text']; 
$sidetext2 = $_POST['S2Text']; 
$pokerchip = $_POST['PokerChip']; 
$specialinstructions = $_POST['SpecialInstructions'];
$message = "Name: $fromName\n\nEmail: $fromEmail\n\nSide Text 1: $sidetext1\n\nSide Text 2: $sidetext2\n\nPoker Chip: $pokerchip\n\nSpecial Instructions: $specialinstructions"; 



/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

$tmpName2 = $_FILES['attachment2']['tmp_name']; 
$fileType2 = $_FILES['attachment2']['type']; 
$fileName2 = $_FILES['attachment2']['name']; 

/* Start of headers */ 
$headers = "From: $fromName $fromEmail"; 


if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 


  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n"; 
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\""; 

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


  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" . 
  "Content-Type: {$fileType};\n" . 
  " name=\"{$fileName}\"\n" . 
  "Content-Transfer-Encoding: base64\n\n" . 
  $data . "\n\n" . 
  "--{$mimeBoundary}--\n"; 


} 

$flgchk = mail ("$to_Email", "$subject", "$message", "$headers"); 

if($flgchk){
  header ('Location: index.php?route=information/information&information_id=8');
 }
else{
  header ('Location: index.php?route=information/information&information_id=9');
}
?>

问题是我只收到1个附件而不是2个。

我不是程序员..有人可以请最早指导我吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

永远不会处理文件2.

if只是在这里检查文件1: if(file($ tmpName))

您需要将$ tmpName更改为$ tmpName2的整个块复制。 标题中的更改只需要执行一次,因此您可以在复制的块中删除它们。

希望能让你走上正轨。