将表单详细信息发送到电子邮件

时间:2015-01-04 08:58:32

标签: php html forms email

我是第一次尝试这个,当我点击提交时我已经给了$to:my gmail它没有显示任何错误,但我没有收到任何邮件到我的Gmail。怎么了?我做了:

<html>
<body>
<form method="post" name="myemailform" action="form-to-email.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>

</body>
</html>

PHP: - 形式对email.php

<?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 = 'tom@amazing-designs.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 = "kasani.prabha@GMAIL.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;
  }
}

?> 

那么$email_from呢?它应该是有效的电子邮件;我的意思是如果我在提交时输入abc@abc.ac一些错误的电子邮件,我将不会收到邮件吗?

1 个答案:

答案 0 :(得分:0)

来自SMTP服务器的电子邮件未经过验证,但问题将在最终用户尝试回复时启动,它将被传递或退回到email_from中添加的电子邮件地址。 SMTP提供商还验证您应该是电子邮件域的所有者,如

                      abc@abc.com

你应该是

的所有者
                     abc.com

为此,他们提供了某种需要在域DNS条目中添加的密钥。原因是避免使用垃圾邮件。