PHP表单验证和电子邮件

时间:2014-02-12 12:30:07

标签: php

好的,所以我正在尝试组织一个简单但安全的PHP验证并形成电子邮件。到目前为止,验证似乎有效,但我现在如何实现以下字段发送到test@gmail.com?谢谢。 :)

     <?php
  // define variables and set to empty values
     $nameErr = $emailErr = $messageErr = "";
       $name = $email = $message = "";

   if ($_SERVER["REQUEST_METHOD"] == "POST")
      {
     if (empty($_POST["name"]))
      {$nameErr = "Name required*";}
    else
      {
     $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
   if (!preg_match("/^[a-zA-Z ]*$/",$name))
   {
   $nameErr = "Only letters and white space allowed"; 
   }
}

 if (empty($_POST["email"]))
 {$emailErr = "Email required*";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
  {
  $emailErr = "Invalid email format"; 
  }
}

 if (empty($_POST["message"]))
   {$messageErr = "Message required*";}
  else
     {$message = test_input($_POST["message"]);}
 if (!preg_match("/^[a-zA-Z ]*$/",$name))
       {
  $nameErr = "Only letters and white space allowed"; 
    }
 }

   ?>

这也是HTML代码:

        <form method="post" action="<?php echo              
            htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <div class="form-headers">Full Name* </div>
            <input name="name" type="text" /> <span class="error"><?php echo $nameErr;?> 
           </span>
            <div class="form-headers">Email Address* </div>
            <input name="email" type="text" /> <span class="error"><?php echo 
          $emailErr;?></span>
            <div class="form-headers">Cellphone No. </div>
            <input name="cellphone" type="text" /> 
            <div class="form-headers">Message* </div>
            <textarea name="message"></textarea> <span class="error2"><?php echo 
          $messageErr;?></span>
            <div class="form-headers"></div>
            Support Query <input name="cf_query" class="checkbox" type="checkbox" 
         value="Support" /> 
            Information Query <input name="cf_query" class="checkbox" type="checkbox" 
        value="Information" /> 
            <!-- HIDDEN FIELD - HONEYPOT ANTI_SPAM -->
            <input id="website" class="using" name="cf_website" type="text"  
             />
            <!-- END -->
            <div class="form-headers"> </div>
            <input name="" class="button" type="submit" value="Send" />


        </form> 

2 个答案:

答案 0 :(得分:1)

您应该使用PHP邮件功能(以下是链接)。您可以创建所有这些字段的字符串,并将其添加到电子邮件正文中。

http://in2.php.net/manual/en/function.mail.php

还有一个选项pear Mail包。

http://pear.php.net/package/Mail/redirected

两者都适合你。

答案 1 :(得分:0)

基本PHP邮件功能: -

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

参考: - http://in1.php.net/manual/en/function.mail.php

您也可以使用Swift Mailer: - http://swiftmailer.org/

require_once 'lib/swift_required.php';

// Create the message
$message = Swift_Message::newInstance()

  // Give the message a subject
  ->setSubject('Your subject')

  // Set the From address with an associative array
  ->setFrom(array('john@doe.com' => 'John Doe'))

  // Set the To addresses with an associative array
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

  // Give it a body
  ->setBody('Here is the message itself')

  // And optionally an alternative body
  ->addPart('<q>Here is the message itself</q>', 'text/html')

  // Optionally add any attachments
  ->attach(Swift_Attachment::fromPath('my-document.pdf'));
相关问题