我正在提交一個表單,提交備註並上傳文件並將其作爲附件發送。我遇到的問題是如果我不附加文件,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!
}
?>
謝謝,完美的作品!我在學習PHP時感謝大家的耐心。 –
你打賭,沒有問題! :-) – nbrogi