2012-02-02 85 views
5

嗨,我是一個新手,PHP和發送PHP郵件我下面這個教程從接觸形式

http://tutorialpot.com/2011/06/fancy-contact-form-with-inline-validation/#comment-1771

我想知道你我把我的電子郵件地址,因此用戶可以發送電子郵件給我

在此先感謝

<?php 
function checkLen($str,$len=2) //&len definens the minimun length of the input fields 
{ 
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len; 
} 
function checkEmail($str) 
{ 
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str); 
} 
foreach($_POST as $k=>$v) 
{ 
$_POST[$k]=stripslashes($_POST[$k]); 

$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k])); 
} 
//session names must be same with that in contact form  
session_name("tpot_contact"); 
@session_start(); 
if (isset($_POST['send'])){ 
$err = array(); 
if(!checkLen('name')) 
    $err[]='The name field is too short or empty!'; 
if(!checkLen('email')) 
    $err[]='The email field is too short or empty!'; 
else if(!checkEmail($_POST['email'])) 
    $err[]='Your email is not valid!'; 
if(!checkLen('subject')) 
    $err[]='You have not selected a subject!'; 
if(!checkLen('message')) 
    $err[]='The message field is too short or empty!'; 
if((int)$_POST['captcha'] != $_SESSION['expected']) 
    $err[]='Wrong security code!'; 
if(count($err)) 
{ 
     $_SESSION['errStr'] = implode('<br />',$err); 
     header('Location: '.$_SERVER['HTTP_REFERER']); 
     exit(); 
    } 
    //submission data 
     $IP=$_SERVER['REMOTE_ADDR']; 
     $name=$_POST['name']; 
     $email=$_POST['email']; 
     $date=(gmdate(" Y/m/d ")); 
     $time = date('H:i:s'); 
     $message=$_POST['message']; 
      $from="[email protected]"; 
      $subject = " from ".$_POST['name']." | contact form"; 
      $headers = "From: ".$from."\r\n"; 
      $headers .= "Reply-to: ".$from."\r\n"; 
      $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
      //checks whether send to my email address is set 
      if ($cc == 1) { 
      $headers .= 'Cc:'. $_POST['email']."\r\n"; 
          } 
     $msg = 
      "<p><strong>Name: </strong>" .$name. "</p> 
      <p><strong>Email Address: </strong>" .$email. "</p> 
      <p><strong>Enquiry: </strong>" .$_POST['subject']. "</p> 
      <p><strong>Message: </strong>" .$message. "</p> 
      <br/> <br/> 
      <p>This message was sent from the IP Address:" .$ipaddress." on".$date. "at".$time."</p>"; 
      if(@mail($email, $subject, $msg, $headers)) 
      { 
     $success=array(); 
     $success[]='Your message has been sent! | Thank you'; 
     $_SESSION['sent'] = implode('<br />',$success); 
     header('Location: '.$_SERVER['HTTP_REFERER']); 
     exit(); 
      } 
    else{ 
    $err[]='your message could not be sent due to a network problem please try again.!'; 
    $_SESSION['errStr'] = implode('<br />',$err); 
    header('Location: '.$_SERVER['HTTP_REFERER']); 
    exit(); 
    } 
} 
?> 

    <div class="fieldContainer"> 
    <label for="name" >*Name: </label> 
    <input class="validate[required,minSize[3]] input1" id="name" name="name" type="text" autofocus="autofocus" placeholder="NAME"/><br /><br /> 
    <label for="email">*Email</label> 
    <input class="validate[required,custom[email]] input1" id="email" name="email" type="text" placeholder="EMAIL" /><br /><br /> 
     <label for="subect" >*Subject</label> 
     <select id="dropdown4" name="subject" class="validate[required] input1"> 
     <option selected="selected" value="">--Choose--</option> 
     <option value="Quote">Quote</option> 
     <option value="Suggestion">Suggestion</option> 
     <option value="Question">Question</option> 
     <option value="Business Proposal">Business Proposal </option> 
     <option value="Advertising">Advertising</option> 
     <option value="Complaint">Complaint</option> 
     <option value="Other">Other</option> 
     </select><br /><br /> 
    <label for="message" >*Message</label> 
    <textarea rows="10" cols="15" name="message" class="validate[required,minSize[3],maxSize[300]] input1" id="message" placeholder=" MESSAGE CONTENTS"></textarea><br /><br /> 

     <legend>*Human Verification (HELP US FIGHT SPAM)</legend> 
     <label for="captcha">25+9=</label> 
    <input type="text" class="validate[required,custom[integer]] input1 " name="captcha" id="captcha" maxlength="2" placeholder="DO A LITTLE MATH" /> 
<p> 
     <input type='checkbox' id='cc' name='cc' value='1' /> 
     Send a copy to your email address 
     </p> 
    </div> 
    <div class="signupButton"> 
<input name="send" type="submit" class="btnsubmit" id="btnsubmit" /> 
    <!--<input class="blackb" type="submit" name="send" id="submit"/>--> 
    </div> 

</form> 
+0

對於郵件的實際發送,我建議你看看http://swiftmailer.org。 – Svish 2012-02-02 15:43:08

回答

2

正是這一部分作爲郵件返回布爾發送消息。第一paramters是解決(見鏈接)

if(@mail($email, $subject, $msg, $headers)) 

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

在本例中的電子郵件將進入用戶輸入如所見到的$email填充了貼值

$email=$_POST['email']; 
地址

但你可以硬編碼到任何你想要的。

if(@mail('[email protected]', $subject, $msg, $headers)) 
+1

此外,你在這一行中聲明變量:'$ email = $ _ POST ['email'];'所以你可以在那裏添加它。 – 2012-02-02 15:44:30

+0

我是否只是將電子郵件更改爲我的電子郵件地址? $ email = $ _ POST ['myemailaddress ???????????']; – 2012-02-02 15:49:50

+0

@JeremyMiller他不應該這樣做,因爲郵件正文中使用'$ email'來告訴他有關發件人的電子郵件地址。他應該替換第62行的'mail'函數中的變量。 – MMM 2012-02-02 15:53:06

3

本教程似乎有一些錯誤(至少乍一看)。

它使用$cc但是這個變量沒有在任何地方定義。

它將消息發送到$email$email = $_POST['email'](第42行),因此它將該電子郵件發送到以(?)形式提供的電子郵件地址。您想要修復第62行:

if(@mail('[email protected]', $subject, $msg, $headers)) 

它也不會消毒輸入,因此您可以將標頭注入電子郵件。更多解釋here

最後,如果CC的功能被正確執行(即$cc定義),你需要發送電子郵件的副本發送者(CC),從而揭示你的e-mail地址(如果你想不好避免垃圾郵件)。您應該發送一封單獨​​的電子郵件給發件人。