我正在制作一个表单,提交备注并上传文件并将其作为附件发送。我遇到的问题是如果我没有附加文件,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!
}
?>
答案 0 :(得分:1)
$ _ POST ['file']似乎没有任何关系。
我会将其移除并放置:
if ($_FILES['file']['size']) {
...
}
那会有用吗?
答案 1 :(得分:0)
检查$_POST['file']
是否等于""
不是检查用户是否上传文件的有效方法。你需要像is_uploaded_file这样的东西。