-1

我正在制作一个提交备注并上传文件并将其作为附件通过电子邮件发送的表单。我遇到的问题是,如果我不附加文件,PHP 不知道要附加什么。所以为了解决这个问题,我改变了

  $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);      // attachment

到:

if($file != "")
      {
      $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);      // attachment
      }

现在,即使我放入附件,它也不会提交附件,我该如何解决这个问题?

完整代码:

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

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$gender = $_POST['gender'] ;
$recipientname = $_POST['recipientname'] ;
$hobbies = $_POST['hobbies'] ;
$age = $_POST['age'] ;
$school = $_POST['school'] ;
$pet = $_POST['pet'] ;
$hair = $_POST['hair'] ;
$eye = $_POST['eye'] ;
$food = $_POST['food'] ;
$drink = $_POST['drink'] ;
$sport = $_POST['sport'] ;
$schoolsubject = $_POST['schoolsubject'] ;
$teacher = $_POST['teacher'] ;
$strengths = $_POST['strengths'] ;
$animal = $_POST['animal'] ;
$superpower = $_POST['superpower'] ;
$fears = $_POST['fears'] ;
$skills = $_POST['skills'] ;
$dedication = $_POST['dedication'] ;
$friend = $_POST['friend'] ;
$otherinfo = $_POST['otherinfo'] ;
$file = $_POST['file'] ;

$body = "Name: $name
Email: $email
Gender: $gender
Recipient's name: $recipientname
Hobbies: $hobbies
Age: $age
School: $school
Pet Info: $pet
Hair Color: $hair
Eye Color: $eye
Favorite Food: $food
Favorite Drink $drink
Favorite Sport: $sport
Favorite School Subject: $schoolsubject
Favorite Teacher: $teacher
Strengths: $strengths
Favorite Animal: $animal
Favorite Superpower/Why: $superpower
Fears/Challenged: $fears
Skills: $skills
Dedication: $dedication
Best Friend Info: $friend
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('***@****.com', 'Michael');
      $mail->SetFrom($email, $name);
      $mail->AddReplyTo($email, $name);
      $mail->Subject = "Message From Legendmaker Customer: $name";
      $mail->Body = $body;

      $mail->Send();
      echo "Story Request Sent Successfully</p>\n";
      echo "<img src='/images/knight-success.png' alt='success' width='429' height='791' />"; 
      echo "Your request will be processed soon. Once your request has been processed you will receive an email. </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

2 回答 2

1

$_POST['file'] 似乎与它没有任何关系。

我会删除它并放入:

if ($_FILES['file']['size']) {
    ...
}

那行得通吗?

于 2013-02-03T23:36:37.733 回答
0

检查是否$_POST['file']等于""不是检查用户是否上传文件的有效方法。你想要类似is_uploaded_file的东西。

于 2013-02-03T22:06:11.197 回答