我遇到了一些麻烦,我正在尝试使用php和phpmailer提交表单。
所以我所做的是尝试在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>
答案 0 :(得分:0)
首先,不会调用您使用if($contactsend== 1)
检查的操作,因为您的脚本强制$contactsend = 0
检查&#34; contactsubject&#34;,&#34; contactphone&#34;,& #34; contactmessage&#34; ..
然后你必须设置这些
$mail->SetFrom('email1', 'FromEmail');
$mail->AddAddress('email1', 'ToEmail');
$mail->AddAddress('email2', 'ToEmail1');
$mail->AddAddress('email3', 'ToEmail2');
使用有效的电子邮件地址.. 以
为例 $mail->AddAddress($contactemail, 'ToEmail');
从POST
获取值对于任何其他信息,请阅读此内容 https://github.com/PHPMailer/PHPMailer