2011-05-06 53 views
0

經過很多痛苦和心痛,我現在有一個工作的PHP電子郵件聯繫表格。它唯一缺少的是一封感謝信或消息。任何人都可以幫我嗎?PHP電子郵件表格謝謝注意

<form name="email" method="post"> 
    <p> 
    <label for="name">Full Name</label> 
    <input type="text" name="name" id="name"> 
    </p> 
    <p> 
    <label for="email">Email Address</label> 
    <input type="text" name="email" id="email"> 
    </p> 
    <p> 
    <label for="phone">Phone Number</label> 
    <input type="text" name="phone" id="phone"> 
    </p> 
    <p> 
    <label for="subject">Subject</label> 
    <input type="text" name="subject" id="subject"> 
    </p> 
    <p> 
    <label for="message">Message<br> 
    </label> 
    <textarea name="message" id="message" cols="45" rows="5"></textarea> 
    </p> 
    <p><input type="submit" name="send" id="send" value="Submit"><input type="reset" name="send" id="send" value="Reset"> 
    </p> 
</form> 


<?php 

// Get the Variables 
    $name = $_POST['name']; 
    $visitor_email = $_POST['email']; 
    $phone = $_POST['phone']; 
    $subject = $_POST['subject']; 
    $message = $_POST['message']; 


// Validate against spammers 
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; 
    } 
} 

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


// Compose the Email 
    $email_from = '[email protected]'; // Set a valid email address that the form can use 

    $email_subject = "New Form submission - $subject"; // Change the message subject here 

    $email_body = "You have received a new message from $name ($phone) .\n Here is the message:\n $message"; 


// Send The Email 
    $to = "[email protected]"; // Set a valid email address to send the form to 

    $headers = "From: $email_from \r\n"; 

    $headers .= "Reply-To: $visitor_email \r\n"; 

    mail($to,$email_subject,$email_body,$headers); 

?> 
+0

我會更擔心消毒'$ message',而不是檢查電子郵件地址中的怪異字符。垃圾郵件發送者仍然可以使用「合法」地址,並填寫消息的數量爲兆字節的陰莖丸和俄羅斯新娘廢話。 – 2011-05-06 17:06:30

回答

2
if(mail($to,$email_subject,$email_body,$headers)){ 
    echo "Thanks for your mail..."; 
} 
0

很簡單,mail()函數,因此使用此返回真/假:

echo mail($to, $email_subject, $email_body, $headers) ? 'Mail send' : 'Failed to send mail'; 

將結果回顯到屏幕上。但是,您可能需要在表單上添加表單操作,因爲消息放置在下面並不常見。你也可以使用這樣的:

if($_SERVER['REQUEST_METHOD'] == "POST" && !IsInjected($visitor_email)) { 
    // insert form action here 
} else { 
    if(IsInjected($visitor_email)) { 
    echo "False e-mail address supplied 
    } 
    // add form HTML code here (outside PHP tags ofcourse 
} 

在這種情況下,你首先檢查了,如果一個後請求被髮送(與E-mailadres是不是假的),如果是這樣:執行發送動作。否則,將錯誤消息發送給虛假電子郵件並顯示該表單。

當然,這不是一個傻瓜證明的例子,你必須自己做一些工作。

相關問題