所以我想在我的网站上创建一个表单,客户端将在该表单中查询该产品。他将填写表格,表格的内容将使用PHPMailer发送到我的电子邮件地址。但是显示有关undefined的常量错误:index email和undefined:index name也有SMTP错误。 错误是这样的:
Notice: Undefined variable: errors in C:\xampp\htdocs\event.php on line 13
Notice: Undefined index: email in C:\xampp\htdocs\event.php on line 34
Notice: Undefined index: orgname in C:\xampp\htdocs\event.php on line 39
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
这是我的PHP脚本
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
require 'PHPMailerAutoload.php';
require 'credentials.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = EMAIL;
$mail->Password = PASS;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom($_POST['email']);
$mail->addAddress(EMAIL, 'Nutriberry Client');
$mail->isHTML(true);
$mail->Subject = $_POST['orgname'];
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail
clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
这是我的HTML表格代码: 实际上我在PHP脚本中定义的表单中有更多字段,但我会自己做,现在我只担心错误
<form method="POST" action="event.php" role="form">
<div class="panel-body ">
<label for="inputName " class="control-label col-lg-4 col-sm-4 ">Requirement </label>
<div class="col-lg-7 col-sm-8 ">
<div class="fav_show_wrapper ">
<select id="requirement " name="department">
<option value="Corporate Events ">Corporate Events</option>
<option value="Cafeteria Services ">Cafeteria Services</option>
<option value="Other ">Other</option>
</select>
</div>
</div>
<label for="inputName " class="control-label col-lg-4 col-sm-4 "></label>
<div class="col-lg-7 col-sm-8 " style="width: 50em">
<div>
<label>No. of Pax : </label> <select name="quantity" style="margin-left: 54px;margin-top:0.7em">
<option value="25-50 ">25 – 50</option>
<option value="51-100 ">51 – 100</option>
<option value="101-200 ">101 – 200</option>
<option value="Above 201 ">Above 201</option>
<option value="Other ">Other</option>
</select>
</div>
</div>
<div class="col-lg-7 col-sm-8 " style="width: 80%;margin-top:0.7em">
<label>Name of organisation:</label> <input type="text " class="form-control " id="orgname " name="orgname " placeholder="Name of organisation ">
</div>
<div class="col-lg-7 col-sm-8 " style="width: 80%">
<label>Contact person:</label> <input type="text " class="form-control " id="name " name="name " placeholder="Name ">
</div>
<div class="col-lg-7 col-sm-8 " style="width: 80%">
<label>Contact No.</label><input type="text " class="form-control " id="phone " name="phone " placeholder="Contact Number ">
</div>
<div class="col-lg-7 col-sm-8 " style="width: 80%">
<label> Email Id</label> <input type="email " class="form-control " id="email " name="email " placeholder="Email ">
</div>
<div class="col-lg-7 col-sm-8 " style="width: 80%">
<label>Message</label> <textarea id="message " style="width: 100%; resize: none "></textarea>
</div>
<!-- <div class="col-lg-4 col-sm-4 "></div> -->
<div class="col-lg-8 col-sm-8 " style="width: 80%">
<div class="g-recaptcha " data-sitekey="6LcOFSITAAAAAMS2wYcWaOsSnMJYAISTW5K_nZeV "></div>
</div>
<div class="col-xs-7 col-xs-offset-4 ">
<button type="submit" id="submit" value="submit" name="submit" class="btn btn-primary " style="margin-top: 1.5em;">Send</button>
</div>
</div>
</form>
答案 0 :(得分:0)
基本的PHP问题。
所有这些都将尝试访问$_POST
变量中的命名键,并且如果它们不存在则会抛出未定义的索引错误:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
请改为:
if(isset($_POST['name']) ||
isset($_POST['email']) ||
isset($_POST['message']))
isset
是一种PHP语言结构,而不是一个函数(虽然它看起来像一个),并且不会触发错误。它有一个双重功能:必须定义数组元素,如果要返回true,它也必须不是null
。
在这里,你要附加到一个尚不存在的变量:
$errors .= "\n Error: all fields are required";
通过在脚本中更早地声明它来解决此问题:
$errors = '';