2014-02-12 4 views
-1

그래, 간단하지만 안전한 PHP 유효성 검사 및 양식 전자 메일을 구성하려고합니다. 지금까지 유효성 검사가 작동하는 것 같지만 어떻게 [email protected]에 보낼 다음 필드를 구현합니까? 고맙습니다. :)PHP 폼 유효성 검사 및 전자 메일

 <?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

당신이 ['mail']를 사용하여 시도 되세요 (http://nl1.php.net/manual/en/function.mail.php) 기능? – Overv

답변

0

기본 PHP 메일 기능 : -

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

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

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

또한 사용할 수 있습니다 스위프트 메일러 - 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('[email protected]' => 'John Doe')) 

    // Set the To addresses with an associative array 
    ->setTo(array('[email protected]', '[email protected]' => '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'));