我已经购买了托管并创建了网络邮件。但联系表单的邮件功能在我的托管中无效。它打印“错误”。我不明白我错在哪里。我在谷歌搜索,看了例子,但我没有解决我的问题。请帮助我。谢谢。 修改信息:当我进行托管时,它是Windows托管。 maik功能正在运行。但是,我通过了linux托管,然后再次在cpanel中创建了邮件。但是,现在邮件功能无法正常工作。再次感谢。
Edit2:您好这个代码在Windows托管中工作。我认为问题源于linux托管...... 这是我的代码:
contact.html
<html>
<form action="contact_form.php" method="POST" enctype="multipart/form-data" id="contactform">
<fieldset class="row">
<legend>Contact me :)</legend>
<p>
<label for="your-name">Your Name</label>
<input type="text" name="name" id="your-name" class="input-xlarge">
</p>
<p>
<label for="your-email">Your Email <span class="required">(required)</span></label>
<input type="email" name="email" id="your-email" class="input-xlarge" required>
</p>
<p>
<label for="your-subject">Subject</label>
<input type="text" name="subject" id="your-subject" class="input-xlarge">
</p>
<p>
<label for="your-message">Your message <span class="required">(required)</span></label>
<textarea name="message" cols="50" rows="10" id="your-message" class="input-xxlarge" required placeholder="What do you want to say?"></textarea>
</p>
<!-- This is hidden for normal users -->
<div class="hidden">
<label>
Do not fill out this field
<input name="s_check">
</label>
</div>
<p>
<input type="submit" id="submit" name="submit" class="primary" value="Send Message">
</p>
<p hidden id="response"></p>
</fieldset>
</form>
</html>
contact_form.php
<?php
if(isset($_POST['submit'])) {
$to = 'info@xyz.com';
$name = stripslashes($_POST['name']); //sender's name
$email = stripslashes($_POST['email']); //sender's email
$subject = stripslashes($_POST['subject']); // the subject
echo $name."<br/>";
echo $email."<br/>";
echo $subject."<br/>";
//The message you will receive in your mailbox
$msg = "From : $name \r\n"; //add sender's name to the message
$msg .= "e-Mail : $email \r\n"; //add sender's email to the message
$msg .= "Subject : $subject \r\n\n";
$msg .= "---Message--- \r\n".stripslashes($_POST['message'])."\r\n\n"; $msg .= "---User information--- \r\n"; //Title
$msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
$msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
$msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer
if (mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")){
//Message sent!
echo nl2br("
<div class=\"MsgSent\">
<h1>Congratulations!!</h1>
<p>Thank you <b><?=$name;?></b>, your message is sent!<br /> I will get back to you as soon as possible.</p>
</div>
");
exit;
}
else{
// Display error message if the message failed to send
echo "
<div class=\"MsgError\">
<h1>Error!!</h1>
<p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
</div>";
exit;
}
}
?>
答案 0 :(得分:1)
<p>Thank you <b><?=$name;?></b>, your message is sent!<br />
<p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
应该是
<p>Thank you <b>{$name}</b>, your message is sent!<br />
<p>Sorry <b>{$name}</b>, your message failed to send. Try later!</p>
也不确定你为什么使用nl2br一个简单的回声就足够了。
答案 1 :(得分:0)
正如您在其他地方所说的那样,mail()
函数存在问题 - 它的级别非常低且不易使用。
你最好不要使用它 - 有几个非常好的PHP邮件程序类,它们更容易使用,效果更好,而且功能更强大。
我的建议是使用phpMailer;它非常容易使用 - 正如您从this example page所看到的那样。但是也有其他几种替代库。