dongzhenbi8919 2014-02-12 12:30
浏览 61
已采纳

PHP表单验证和电子邮件发送[关闭]

Okay so i'm trying to organize a simple yet safe php validation and form emailing. So far the validation seems to work but how do i now implement the following fields to send to test@gmail.com? Thank you. :)

     <?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"; 
    }
 }

   ?>

Here's the HTML code too:

        <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条回答 默认 最新

  • doqpm82240 2014-02-12 12:39
    关注

    Basic PHP mail function :-

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

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

    You can also use 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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料