-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 に答える