PHP联系人表单失败
我写了一个php联系人表单,不明白为什么它不发送电子邮件。表单可以提交,但不发送实际电子邮件 下面是我的PHP代码PHP联系人表单失败,php,html,forms,Php,Html,Forms,我写了一个php联系人表单,不明白为什么它不发送电子邮件。表单可以提交,但不发送实际电子邮件 下面是我的PHP代码 <?php //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the name field is not empty if(trim($_POST['contactname']) == '') {
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'xyz@xyz.com';
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
下面的HTML代码是联系人表单HTML:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactform">
<fieldset class="contact-fieldset">
<h2>Send us a message</h2>
<ul>
<li>
<label for="contactname">Your Name:</label>
<div class="ctinput-bg"><input type="text" name="contactname" id="contactname" value="" class="contact-input required" /></div>
</li>
<li>
<label for="email">Email:</label>
<div class="ctinput-bg"><input type="text" id="email" name="email" class="contact-input required email" /></div>
</li>
<li>
<label for="subject">Subject:</label>
<div class="ctinput-bg"><input type="text" name="subject" id="subject" class="contact-input required" /></div>
</li>
<li>
<label for="message">Your message:</label>
<div class="cttxtarea-bg"><textarea rows="6" cols="40" id="message" name="message" class="contact-textarea required"></textarea></div>
</li>
<li>
<div class="form-button contact-submit">
<span><input type="submit" value=" send message " name="submit" /></span>
</div>
</li>
</ul>
</fieldset>
</form>
听起来您的邮件功能设置不正确。检查php.ini
文件,确保您具有正确的SMTP服务器设置
另外,与您最初的问题无关,但我不确定使用
标记表单在语义上是否正确。一组字段实际上并不是一个未排序的列表,我更喜欢使用语义正确的
,甚至是一系列
。我看到两件事:
1-代码顶部的$HASRERROR=false声明(使舒尔it成为激进分子)
2-生成标题,如下所示:
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
希望这能起到作用,您可以尝试检查邮件是否已实际发送:
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
这些东西不会解决问题,但肯定会有助于调试它。除了其他人提供的解决方案之外,我强烈建议使用SwiftMailer库
我必须创建一个自动电子邮件功能,手动设置邮件头之类的东西简直是一场噩梦。SwiftMailer为我节省了大量时间,请参见示例。服务器错误日志中是否有任何内容?您是否正在本地服务器中尝试mail()
功能?您是否安装了可以实际发送邮件的邮件服务器?这对每个人来说都不是那么明显:-)我想我没有安装邮件服务器。我没有意识到我需要安装一个???
var_dump($headers);