私はいくつかの問題を抱えています。私はphpとphpmailerでフォームを送信しようとしています。フォームが送信されないPHP
私がしたことは、w3schoolの完全なフォームの例の後にフォームを作成しようとしていて、メールで送信できるかどうか疑問に思っていました。私はphpmailerを見つけ、それを使用しようとしましたが、送信していません。
フォームを送信できるかどうかを確認するために$ contactsendを使用しています(検証が完了した後に)。したがって、$ contactsendが0の場合は送信できません。それが1ならば、フォームを送信できます。
しかし、私はほとんどそれに問題があると確信していますが、私は理由を知らない。 どうやって間違っているのか理解できますか?
<?php
$contactnameErr = $emailErr = $phoneErr = $subjectErr = $messageErr = "";
$contactname = $email = $phone = $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$contactsend= 1;
if (empty($_POST["contactname"])) {
$contactnameErr = "* Name is required";
$contactsend= 0;
} else {
$contactname = test_input($_POST["contactname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$contactname)) {
$contactnameErr = "* Only letters and white space allowed";
$contactsend= 0;
}
}
if (empty($_POST["contactemail"])) {
$contactemailErr = "* Email is required";
$contactsend= 0;
} else {
$contactemail = test_input($_POST["contactemail"]);
// check if e-mail address is well-formed
if (!filter_var($contactemail, FILTER_VALIDATE_EMAIL)) {
$contactemailErr = "* Invalid email format";
$contactsend= 0;
}
}
if (empty($_POST["contactphone"])) {
$contactphoneErr = "* Phone is required";
$contactsend= 0;
} else {
$contactphone = test_input($_POST["contactphone"]);
$contactsend= 0;
}
if (empty($_POST["contactsubject"])) {
$contactsubjectErr = "* Subject is required";
$contactsend= 0;
} else {
$contactsubject = test_input($_POST["contactsubject"]);
$contactsend= 0;
}
if (empty($_POST["contactmessage"])) {
$contactmessageErr = "* Message is required";
$contactsend= 0;
} else {
$contactmessage = test_input($_POST["contactmessage"]);
$contactsend= 0;
}
echo $contactsend;
if($contactsend== 1)
{
//send mail
$message = "\nSpecial Events , Contact Us . \nName : " . $contactname . "\nEmail : " . $contactemail . "\nPhone : " . $contactphone
. "\nSubject : " . $contactsubject ."\nMessage : " . $contactmessage;
require("/home/specialeventsleb/public_html/phpmailer/PHPMailer/class.phpmailer.php");
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'email1';
$mail->Password = 'password1';
$mail->SetFrom('email1', 'FromEmail');
$mail->AddAddress('email1', 'ToEmail');
$mail->AddAddress('email2', 'ToEmail1');
$mail->AddAddress('email3', 'ToEmail2');
$mail->SMTPDebug = true;
$mail->Timeout = 2000;
$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
$mail->Debugoutput = 'echo';
$mail->Subject = 'Message from Special Events Website';
$mail->Body = $message;
$mail->send();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="ContactUs">
<!--<img class="ComputerBanner" src="Pictures/map/ContactUsBanner.png" />-->
<img class="MobileBanner" src="Pictures/map/ContactUsBannerMobile.png" />
<div class="ContactBox">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2>SEND US A MESSAGE!</h2>
<span>We'd be happy to hear from you.</span>
<input name="contactname" placeholder="Name" type="text" value="<?php echo $contactname;?>"/> <span class="error"> <?php echo $contactnameErr;?></span>
<input name="contactemail" placeholder="Email" type="text" value="<?php echo $contactemail;?>" /><span class="error"> <?php echo $contactemailErr;?></span>
<input name="contactphone" placeholder="Phone #" type="text" value="<?php echo $contactphone;?>" /><span class="error"> <?php echo $contactphoneErr;?></span>
<input name="contactsubject" placeholder="Subject" type="text" value="<?php echo $contactsubject;?>" /><span class="error"> <?php echo $contactsubjectErr;?></span>
<textarea name="contactmessage" placeholder="Message"><?php echo $contactmessage;?></textarea><span class="error"> <?php echo $contactmessageErr;?></span>
<input type="submit" name="submit" value="Send" class="contact-button" />
</form>
</div>
</div>
これを読んではいあなたは正しいです、私はそれが動作しますが、テスト・データの後に、これらの3つのフィールドから削除contactsendを強制されました! –