如何使用php发送联系表单

时间:2013-10-25 16:31:58

标签: php

我提交表格后会告诉我“我们无法发送您的信息。请联系site@sitenamexample.com它不会发送到我的网络邮件。我无法解决问题。我真的需要你的帮助人员。

$domain = "http://sitenamexample.com" . $_SERVER["HTTP_HOST"];
$siteName ="My Website name";
$siteEmail = "site@sitenamexample.com";
$er = "";

if(isset($_POST["contactEmail"])){
 global $subject, $message;

$contactName = htmlentities(substr($_POST["contactName"], 0, 100), ENT_QUOTES);
$contactEmail = htmlentities(substr($_POST["contactEmail"], 0, 100), ENT_QUOTES);
$messageSubject = htmlentities(substr($_POST["messageSubject"], 0, 100), ENT_QUOTES);
$messageContent = htmlentities(substr($_POST["messageContent"], 0, 100), ENT_QUOTES);


if (!preg_match('/' . '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$' . '/' 
 , $contactEmail))
    {
       $er .= 'Please enter a valid e-mail address.<br>';
    }

if ($contactName == "" || $contactEmail == "" || $messageSubject == "" || 
    $messageContent == ""){
       $er .= 'Your Name, E-mail, Message Subject, and Message Content cannot be left    
        blank.<br />';
     }

if($er == ''){

$subject = $messageSubject;
$message = 
'<html>
<head>
     <title>' . $siteName . ': A Contact Message </title>
 .<body>
         ' . wordwrap($messageContent, 100) . '
  </body>
 </html>';
}

$to = $siteName . ' Contact Form <' . $siteEmail . '>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=is0-8859-1' . "\r\n";
$headers .= 'From: ' . $contactName . ' <' . $contactEmail . '>' ."\r\n";
$headers .= 'Reply-To: ' . $contactName . ' <' . $contactEmail . '>' . "\r\n";
$headers .= 'Return-Path: ' . $contactName . ' <' . $contactEmail . '>' . "\r\n";
$headers .= 'X-Mailer: ' . $siteName . "\r\n";

if (mail($to, $subject, $message, $headers)){
echo '<div>Thank you for contacting ' . $siteName . '. We will read your message and 
      contact you if necessary.</div>';
}

else {
$er .= 'We weren\'t able to send your message. Please contact ' . $siteEmail . 
       '.<br />';
}

}
else {
showContactForm();
}

if ($er != '' && isset($_POST["contactEmail"])){
   showContactForm($contactName, $contactEmail, $messageSubject, $messageContent, $er);
}

else if($er != '' && !isset($_POST["contactEmail"])){
showContactForm('', '', '', $er);
}

function showContactForm($contactName = "", $contactEmail = "", $messageSubject = "", 
                         $messageContent = "", $er = ''){
echo '<div style="font-weight:bold; margin: 5px 0; "> ' . $er . '</div>

<div id="email_form">
<h1> Contact Form</h1>
<form method="post" name="contact" action="#contactus">

<label for="name">Name:</label> 
<input type="text" id="author" name="contactName" class="input_field" />
<div class="cleaner h10"></div>

<label for="email" class="adjust">Email:</label> 
<input type="text" id="email" name="contactEmail" class="input_field" />
<div class="cleaner h10"></div>

<label for="messageSubject" class="adjust">Subject:</label> 
<input type="text" id="messageSubject" name="messageSubject" class="input_field" />
<div class="cleaner h10"></div>

<label for="text" class="adjust">Comments:</label> 
<textarea id="text" name="messageContent" rows="0" cols="0"></textarea>
<div class="cleaner h10"></div>

<input type="submit" class="submit_btn float_l" name="submitButton" id="submit" 
  value="Send Message" />
<input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />
</form>   
<div class="clearfix"></div>
</div>';
}
?>
<div class="clearfix"></div>
<?php include ("includes/footer.php"); ?>

1 个答案:

答案 0 :(得分:0)

您的代码通常是正确的(但请记住 - $to应该只是没有任何<>的电子邮件地址。我认为您的问题的原因是禁用Sendmail PHP库 - 您可以在服务器设置中启用它,或者如果您无法访问它 - 请阅读phpmailer