2012-12-26 27 views
0

我有一個簡單的電子郵件形式的PHP文件,我嘗試發送電子郵件,但似乎我的變量爲空。我的代碼後面有一個回顯,用於測試我的變量是否有任何值,並且它們不是打印的。唯一打印的是'完成'和'email $ to'。我做錯了什麼?我在YouTube上發佈了一個帖子,他採用了這種方法,他做了完全相同的事情,併爲他工作。我也嘗試了更多的電子郵件PHP文件,但仍然沒有。 這是我的html和php代碼。在PHP電子郵件形式的空變量

PHP代碼:

<?php 

$name = $_REQUEST['author']; 
$from = $_REQUEST['email']; 
$subj = $_REQUEST['sub']; 
$message = $_REQUEST['msg']; 

$headers .= "From: ".$from; 

$to = "[email protected]"; 

$subject = $subj; 
$body = $message; 

if(mail($to,$subject,$body,$headers)) { 
echo "An e-mail was sent to ".$to." with the subject: ".$subject; 
} else { 
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid"; 
} 

?> 

的html代碼:

<form action="formtoemail.php" method="post" enctype="text/plain"> 
          <p> 
           <label for="author">Name:</label> 
           <input type="text" id="author" name="author" class="required input_field" /> 
          </p> 
          <p> 
           <label for="email">Email:</label> 
           <input type="text" id="email" name="email" class="validate-email required input_field" /> 
          </p> 
          <p class="no_margin_right"> 
           <label for="subject">Subject:</label> 
           <input type="text" name="sub" id="sub" class="input_field" /> 
          </p> 
          <div class="cleaner h20"></div> 

          <label for="text">Message:</label> 
          <textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea> 
          <div class="cleaner h20"></div> 

          <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" /> 
          <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" /> 
         </form> 
+0

問題是否解決? – Joddy

+0

現在我得到的價值。但我在if語句中出錯。它發展我的if語句的其他部分:/ – lantonis

+0

嘗試PEAR郵件包發送郵件 – Joddy

回答

2

您通過POST發送,並通過GET retriving嘗試$_POST,而不是$_GET在PHP

或HTML -

Chen GE從行動GET而不是POST

+0

哦該死的我發佈代碼與get。我沒有注意到。我用郵局初始化了我的代碼。仍然沒有工作。 – lantonis

+0

@lantonis然後用最新的代碼更新。 – Joddy

+0

只是。現在我從變量中獲取值。但我在if語句中遇到錯誤。我從我的if語句中得到了其他的東西。 – lantonis

0

在你的HTML代碼更改 「POST」 方法 「GET」

<form action="formtoemail.php" method="get" enctype="text/plain"> 
         <p> 
          <label for="author">Name:</label> 
          <input type="text" id="author" name="author" class="required input_field" /> 
         </p> 
         <p> 
          <label for="email">Email:</label> 
          <input type="text" id="email" name="email" class="validate-email required input_field" /> 
         </p> 
         <p class="no_margin_right"> 
          <label for="subject">Subject:</label> 
          <input type="text" name="sub" id="sub" class="input_field" /> 
         </p> 
         <div class="cleaner h20"></div> 

         <label for="text">Message:</label> 
         <textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea> 
         <div class="cleaner h20"></div> 

         <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" /> 
         <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" /> 
        </form> 
+0

我改變了我的html格式的帖子,現在變量都沒問題。但我在if語句中得到else語句。爲什麼不驗證?幫助了m8謝謝 – lantonis

0

使用 「get」 方法:

<form action="formtoemail.php" method="get" enctype="text/plain"> 
0

您需要更改這個:

$headers .= "From: $email"; 

對此:

$headers = "From: ".$from; 

,改變這一點:

$subject = "$subj"; 
$body = "$message"; 

mail($to, $subject, $body, $headers); 

if(mail($to, $subject, $body, $headers)) { 
    echo "An e-mail was sent to $to with the subject: $subject"; 
} else { 
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid"; 
} 

要這樣:

$subject = $subj; 
$body = $message; 

if(mail($to, $subject, $body, $headers)) { 
    echo "An e-mail was sent to ".$to." with the subject: ".$subject; 
} else { 
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid"; 
} 
+0

。但是現在我在我的if語句中出現錯誤。它在我的if中生成else語句。任何想法爲什麼? – lantonis

+0

@lantonis我沒有測試過上面的代碼,如果錯誤告訴我:) –

+0

以及我在我的if語句中得到錯誤。我收到「發送郵件時出現問題,請檢查您的代碼並確保電子郵件地址」。$ to。「有效」;信息。顯然,我的收件箱中沒有任何新郵件。它在最後幾天真的讓我瘋狂:P它一定是一件非常簡單但仍未完成的事情。 – lantonis