2014-03-05 66 views
0

因此,我有一個帶有表單的網頁,我試圖設置一個php頁面,當點擊提交按鈕時,它將所有表單信息發送到我的電子郵件,而用戶不得不使用Outlook或任何類型的電子郵件軟件。我認爲我是對的,但不是運行PHP,而是顯示了所有寫出的PHP代碼的頁面,沒有任何內容到達我的電子郵件。這裏的形式:用php向我自己發送電子郵件

<form name="infoForm" method="post" action="email.php"> 
<p class="form"> 
    Are you a registered voter in the state of Washington?<br> 
    <input type="checkbox" id="yesBox"/> Yes<br><br> 

    First Name: <input type="text" id="firstName"><br> 
    Last Name:<input type="text" id="lastName"><br><br> 

    Email: <input type="text" id="email" size="30"><br><br> 
    Street Address: <input type="text" id="street1" size="30"><br> 
        <input type="text" id="street2" size="30"><br> 
    City: <input type="text" id="city" size="30"><br> 
    State: <input type="text" id="state" size="30"><br> 
    Zip Code: <input type="text" id="zip" size="30"><br><br> 
    Initials: <input type="text" id="initials" size="3"><br><br> 

    <input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/> <input type="reset" name="resetFields" class="inputButton" value="Reset"/> 
</p> 
</form> 

而且我的PHP代碼:

<?php 
    if(isset($_POST['send'])){ 
     $to_address="[email protected]"; 
     $subject="WSA Day of Action Entry"; 
     $firstname=$_POST['firstname']; 
     $lastname=$_POST['lastname']; 
     $email=$_POST['email']; 
     $street1=$_POST['street1']; 
     $street2=$_POST['street2']; 
     $city=$_POST['city']; 
     $state=$_POST['state']; 
     $zip=$_POST['zip']; 
     $initials=$_POST['initials']; 
     $message="Name: " .$firstname." ".$lastname."\n"; 
     $message .="Email: " .$email."\n"; 
     $message .="Street: " .$street1."\n"; 
     $message .="Street: " .$street2."\n"; 
     $message .="City: " .$city."\n"; 
     $message .="State: " .$state."\n"; 
     $message .="Zip Code: " .$zip."\n"; 
     $message .="Initials: " .$initials."\n"; 
     $headers = 'From: '.$email."\r\n". 
     'Replay-To: '.$email."\r\n". 
     'X-Mailer: PHP/' .phpversion(); 
     mail($to_address, $subject, $message, $headers); 
     } 
?><!DOCTYPE HTML> 

<html> 
<body> 
Thank you! 
</body> 
</html> 
+0

永遠獨自:)添加PHP解析器到Web服務器。它似乎沒有解析文件 – Robert

+0

「只顯示了所有寫出來的PHP代碼的頁面」 - php是由您的主機支持!?如果您的文件名爲'* .php'且服務器配置正確,您永遠不應該看到php代碼 – Philipp

+0

您正在服務器或本地主機上運行此腳本(在Windows或Linux上)? –

回答

3

您必須使用此內容類型爲text/html

if(isset($_POST['send'])){ 
     $to_address="[email protected]"; 
     $subject="WSA Day of Action Entry"; 
     $firstname=$_POST['firstname']; 
     $lastname=$_POST['lastname']; 
     $email=$_POST['email']; 
     $street1=$_POST['street1']; 
     $street2=$_POST['street2']; 
     $city=$_POST['city']; 
     $state=$_POST['state']; 
     $zip=$_POST['zip']; 
     $initials=$_POST['initials']; 
     $message="Name: " .$firstname." ".$lastname."\n"; 
     $message .="Email: " .$email."\n"; 
     $message .="Street: " .$street1."\n"; 
     $message .="Street: " .$street2."\n"; 
     $message .="City: " .$city."\n"; 
     $message .="State: " .$state."\n"; 
     $message .="Zip Code: " .$zip."\n"; 
     $message .="Initials: " .$initials."\n"; 


$headers=" From : ". $email."\r\n"; 
$headers.= 'MIME-Version: 1.0' . "\r\n"; 
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 


     mail($to_address, $subject, $message, $headers); 
} 
+0

Oho,解決了php代碼只是顯示而不是運行的問題,但是現在我遇到了一個新問題:提交表單時沒有任何內容到達我的電子郵件地址...... –

0
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
$to_address1="Your mail id"; 
mail($to_address1, $subject, $message, $headers); 

添加此

+0

我有這兩個語句已經在我的PHP代碼。 to_address在頂部,郵件(...)在底部 –

+0

這很好,然後添加標題 – user3004356

+0

@TonyWhite您需要添加標題,與現有的地址 – user3004356

0

發送郵件對PHP來說有點棘手。我有很多問題,直到我切換到使用此插件:http://phpmailer.worxware.com/

如果你不能使它的工作,你應該試試看。

0

我個人最近臨時創建了一個PHP郵件,然後再切換到.Net,我不確定你的代碼到底有什麼問題,但是這裏有一個PHP電子郵件論壇,我修改後很好。 (已測試)

Take a look