0

当我使用我制作的这个表格时,它会完美地发送文件,除了......它没有扩展名。我知道我发送的扩展名,所以我能够看到文件没有损坏,但我的客户不知道他们得到了什么扩展名(有 5 种可能性),这将是不专业的。我怎样才能做到这一点,以便向客户发送具有正确扩展名的文件。正确的名称也很好,但不是强制性的。

HTML:

</p>
<title>The Legend Maker - submit a story (test server)</title>
<link href="/CSS/CSS.css" rel="stylesheet" type="text/css">

<div align="center">
  <p><span class="linkText"><a href="/index.html">Home</a> <a href="/contact-us.php">Contact Us</a> <a href="/payments.html">Payments</a></span> </p>
  <p>&nbsp;</p>
  <h2 class="headingText">&nbsp;</h2>
  <h2 class="headingText">&nbsp;</h2>
  <h2 class="headingText">&nbsp;</h2>
  <h2 class="headingText">(for testing purposes)</h2>
  <h2 class="headingText">Submit a story test server</h2>
</div>
<form method="post" action="scripts/email.php" enctype="multipart/form-data">
<table width="476" height="468" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="23%" height="25" class="bodytext"><p align="center">Your Name:</p></td>
<td width="77%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td height="25" class="bodytext"><p align="center">Email Address:</p></td>
<td><input name="email" type="text" id="email" value="you@example.com" size="32"></td>
</tr>
<tr>
<td height="44" class="bodytext"><p align="center">Recipient's Gender:</p></td>
<td><label>
  <select name="gender" id="gender">
    <option value="male" selected="selected">Male</option>
    <option value="female">Female</option>
    <option value="other">Other</option>
        </select>
</label></td>
</tr>
<tr>
<td height="44" class="bodytext"><p align="center">Recipient's Name:</p>  </td>
<td align="left" valign="top"><input name="recipientname" type="text" id="recipientname" value="ex: Jonny" size="32" /></td>
</tr>
<tr>
  <td height="44" class="bodytext"><p align="center">Recipient's Interests or Hobbies:</p></td>
  <td align="left" valign="top"><textarea name="hobbies" cols="50" rows="6" id="hobbies">ex: horseback riding, biking...</textarea></td>
</tr>
<tr>
  <td class="bodytext"><p align="center">Recipient's Age:</p></td>
  <td align="left" valign="top"><input name="age" type="text" id="age" size="5" maxlength="3" /></td>
</tr>
<tr>
  <td class="bodytext"><p align="center">Other Important Information:</p></td>
  <td align="left" valign="top"><textarea name="otherinfo" cols="50" rows="7" id="otherinfo">ex: other things the recipient may enjoy or their favorite superhero
Please specify information you are giving us also:
don't do this: superman
submit it like this: favorite superhero: superman</textarea></td>
</tr>
<tr>
  <td class="bodytext"><p align="center">Images You Would Like To Include In Story:</p></td>
  <td align="left" valign="top"><label>
    <br />
    <input type="file" name="file" id="file" />
  </label></td>
</tr>
<tr>
  <td class="bodytext"><p align="center">&nbsp;</p></td>
  <td align="left" valign="top"><input type="submit" name="Submit" value="Send" /></td>
</tr>
</table>
</form> 

PHP:

 <?php
    require_once '../PHPMailer_5.2.2/class.phpmailer.php';

    $name = $_POST['name'] ;
    $email = $_POST['email'] ;
    $gender = $_POST['gender'] ;
    $recipientname = $_POST['recipientname'] ;
    $hobbies = $_POST['hobbies'] ;
    $age = $_POST['age'] ;
    $otherinfo = $_POST['otherinfo'] ;
    $file = $_POST['file'] ;
    $body = "Name: $name
    Email: $email
    Gender: $gender
    Recipient's name: $recipientname
    Hobbies: $hobbies
    Age: $age
    Other Information: $otherinfo";

    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

    try {
      $mail->AddAddress('***erna@yahoo.com', 'Michael ***');
      $mail->SetFrom($email, $name);
      $mail->AddReplyTo($email, $name);
      $mail->Subject = "Message From Legendmaker Customer: $name";
      $mail->Body = $body;
      $mail->AddAttachment($_FILES['file']['tmp_name']);      // attachment
      $mail->Send();
      echo "Story Request Sent Successfully</p>\n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }
    ?>
4

1 回答 1

0

我检查了 PHPMailer 的源代码,这就是我为函数签名找到的。

AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') 

要解决您的问题,您应该做的就是:

$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
于 2013-01-20T03:53:57.313 回答