如何使用联系表单PHP

3
我从主题森林购买了一个主题,打算用HTML和CSS进一步构建它。但是有一个联系表单我不知道该怎么使用。我是PHP和JavaScript的初学者。我想只需要在“//ENTER YOUR EMAIL ADDRESS HERE”这里插入我的电子邮件地址:$address = 'hello@email.com';。但是这并没有起作用。
我的问题是:我应该如何使其工作,以便当有人通过填写联系表单联系我时,我将收到发送到我的电子邮件地址的邮件。我在本地服务器上进行了测试,当我尝试填写联系表单时,我没有收到任何邮件。请问有谁能帮我指明正确的方向。

<?php

if(!$_POST) exit;

function tommus_email_validate($email) { return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email); }

$name = $_POST['name']; 
$email = $_POST['email']; 
$website = $_POST['website']; 
$comments = $_POST['comments'];

if(trim($name) == '') {
 exit('<div class="alert danger">Attention! You must enter your name.</div>');
} else if(trim($name) == 'Name') {
 exit('<div class="alert danger">Attention! You must enter your name.</div>');
} else if(trim($email) == '') {
 exit('<div class="alert danger">Attention! Please enter a valid email address.</div>');
} else if(!tommus_email_validate($email)) {
 exit('<div class="alert danger">Attention! You have entered an invalid e-mail address.</div>');
} else if(trim($website) == 'Website') {
 exit('<div class="alert danger">Attention! Please enter your website.</div>');
} else if(trim($website) == '') {
 exit('<div class="alert danger">Attention! Please enter your website.</div>');
} else if(trim($comments) == 'Message') {
 exit('<div class="alert danger">Attention! Please enter your message.</div>');
} else if(trim($comments) == '') {
 exit('<div class="alert danger">Attention! Please enter your message.</div>');
} else if( strpos($comments, 'href') !== false ) {
 exit('<div class="alert danger">Attention! Please leave links as plain text.</div>');
} else if( strpos($comments, '[url') !== false ) {
 exit('<div class="alert danger">Attention! Please leave links as plain text.</div>');
} if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

//ENTER YOUR EMAIL ADDRESS HERE
$address = 'hello@email.com';

$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name from $website from your contact form, their additional message is as follows." . "\r\n" . "\r\n";
$e_content = "\"$comments\"" . "\r\n" . "\r\n";
$e_reply = "You can contact $name via email, $email";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $address" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";

if(mail($address, $e_subject, $msg, $headers)) { 
 echo "<fieldset><div id='success_page'><h4 class='remove-bottom'>Email Sent Successfully.</h4><p>Thank you <strong>$name</strong>, your message has been submitted to us.</p></div></fieldset>"; 
}
<section id="contact" class="page-section dark-wrapper">
  <div class="container">
   <div class="row">
    
    <div class="page-header text-center">
     <!-- multiple h1's are perfectly acceptable on a page in valid HTML5, when wrapped in individual sections, they're 100% semantically correct -->
     <h1>Contact Me<small>Here's a Working Contact Form</small></h1>
     <!-- seriously -->
    </div>
    
    <div class="col-sm-8">
     <div id="message"></div>
     <form method="post" action="sendemail.php" id="contactform">
      <input type="text" name="name" id="name" placeholder="Name" />
      <input type="text" name="email" id="email" placeholder="Email" />
      <input type="text" name="website" id="website" placeholder="Website" />
      <textarea name="comments" id="comments" placeholder="Comments"></textarea>
      <input type="submit" name="submit" value="Submit" />
     </form>
    </div>
    
    <div class="col-sm-4 lp30">
     <h5>Contact Me</h5>
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar.</p>
     <ul class="fa-ul">
      <li class="bm15"><i class="fa fa-map-marker fa-fw fa-2x fa-li"></i> Denmark</li>
      <li class="bm15"><i class="fa fa-phone-square fa-fw fa-2x fa-li"></i> 08044576869</li>
      <li class="bm15"><i class="fa fa-laptop fa-fw fa-2x fa-li"></i> <a href="mailto:info@jjtranslation.com" target="_top">mail@me.com</a></li>
     </ul>
    </div>  
    
   </div>
  </div>
 </section>


问题是什么? - u_mulder
你可以选择使用 PHP Mailer。 - Rohitashv Singhal
1
@RohitashvSinghal 邮件功能在本地主机上可以工作,您只需要配置邮件服务器即可。在Web服务器上,管理员已经为您完成了这项工作。 - bansi
大家非常感谢!天哪,我不知道本地主机上的邮件功能无法工作!我尝试了在线环境,它可以工作! - Kami
显示剩余5条评论
3个回答

2
如果您不想自己编写 PHP 代码,可以看看 Formspree。该服务每月免费提供1000个请求。
使用也非常简单:只需将 action 属性添加到您的表单中,就像这样:
<form action="//formspree.io/your@email.com"
      method="POST">
    <input type="text" name="name">
    <input type="email" name="_replyto">
    <input type="submit" value="Send">
</form> 

很好的服务,感谢分享。我认为特别是如果你不期望收到太多邮件,这是完美的。 - Wavemaster
谢谢分享!我一定会试试看的! - Kami

1

您可以进行测试,但必须在php.ini中配置正确的SMTP服务器。如果您正在托管服务器上进行测试,则不应该有问题;如果您正在本地测试,则必须编辑您的php.ini文件并设置SMTP服务器。


谢谢!我是在我的本地服务器上测试它,这就是为什么它没有成功的原因。 - Kami

1

如果没有正确配置php.ini文件,则无法在本地发送电子邮件(您需要正确设置smtp服务器)。但是,当您将其上传到您的web主机位置时,它将开始工作而无需任何电子邮件配置。它将使用来自服务器的smtp设置。只需尝试上传即可看到这个神奇的效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接