HTML-PHP联系人表格电子邮件 [英] HTML - PHP contact form email

查看:87
本文介绍了HTML-PHP联系人表格电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里和Google中搜索了如何制作联系表格,而我却没有能力。

I have searched here and in google how to make a contact form to work and I havent been able.

我下载了 php联系人表格,但我不知道如何使它起作用。我知道对此有很多询问,但请帮帮我。我被困在这里:/

I downloaded a "php contact form" but I dont know how to make it work. I know there have been a lot asked question about this but please help me out. I am stuck here :/

这是HTML代码:

<form action="form-to-email.php" name="myemailform" method="post">
    <div>
        <span>Name</span>
        <input type="text" name="name" value="" placeholder="Your Name">
    </div>

    <div>
        <span>Email</span>
        <input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="example@domain.com">
    </div>

    <div><textarea name="message" placeholder="Message"></textarea></div>

    <div class="code">
        <button><input type="submit" name='submit' value="Send"></button>
    </div>
    <i class="clear" style="display: block"></i>
    </div>
</form>

这是我从网站上下载的免费PHP表单代码:

And here is the "free PHP form code" I downloaded from a website:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'myEmail@domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "myEmail@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

我是否需要在mysql中做某事或做更多事情?
感谢您的帮助!

Do I need to do something in mysql or do something more at all? Thanks for the help!

推荐答案

此处这是消息:\n $ message。< =看到点了吗?

Here "Here is the message:\n $message". <= see the dot? That's supposed to be a semi-colon.

那是OP的代码无法正常工作的原因。

That, is the reason why OP's code is not working.

错误报告 添加到您的文件将有助于发现错误。

Add error reporting to the top of your file(s) which will help find errors, which it would have.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

侧注:错误报告仅应在登台中进行,而绝不能在生产中进行。

Sidenote: Error reporting should only be done in staging, and never production.

这篇关于HTML-PHP联系人表格电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆