2016-03-27 111 views
0
<?php 
//if "email" variable is filled out, send email 
if (isset($_REQUEST['email'])) { 

    //Email information 
    $admin_email = "[email protected]"; 
    $email = $_REQUEST['email']; 
    $subject = $_REQUEST['subject']; 
    $comment = $_REQUEST['comment']; 

    //send email 
    mail($admin_email, "$subject", $comment, "From:" . $email); 

    //Email response 
    echo "Thank you for contacting us!"; 
} 
//if "email" variable is not filled out, display the form 
else { 
?> 

<form method="post"> 
    <input name="email" type="text" class="form-control" placeholder="Enter your email address..."> 
    <input name="subject" type="text" class="form-control" placeholder="Subject"> 
    <br> 
    <textarea name="comment" class="form-control" rows="3"></textarea> 
    <br> 
    <div class="mesbutts"> 
     <button type="submit" class="btn btn-primary" value="Submit">Send</button> 
     <button type="reset" value="Reset" class="btn btn-default" >Clear</button> 
    </div> 

</form> 
<?php 
} 
?> 

嗨,大家好,我似乎無法弄清楚如何讓我的PHP代碼在通過使用表單發送電子郵件時工作。我已經把我的個人電子郵件,我似乎無法收到他們。你能幫我看看我的語法錯誤嗎?通過PHP中的表單類型html發送電子郵件

謝謝你隊友!

+0

您需要使用頭的功能,郵件和設置'內容type'手動http://php.net/manual/en/function.mail.php的 – Naumov

+0

可能的複製鎖定例如4號給你任務[PHP的郵件表單不完整發送電子郵件](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-email) – Qirel

+0

嗨@Naumov你怎麼可以去做?對不起,我在PHP –

回答

1

我認爲這對你有幫助。但更好的方式使用圖書館的PHP發送電子郵件,例如phpMailer,或庫。

<?php 
//if "email" variable is filled out, send email 
if (isset($_REQUEST['email'])) { 
    $headers = 'MIME-Version: 1.0' . "\r\n"; // set mime version 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // set content-type as html 
    //Email information 
    $admin_email = "[email protected]"; 
    $email = $_REQUEST['email']; 
    $subject = $_REQUEST['subject']; 
    $comment = $_REQUEST['comment']; 

    //send email 
    mail($admin_email, "$subject", $comment, "From:" . $email,$headers); // adding headers to mail 

    //Email response 
    echo "Thank you for contacting us!"; 
} 
//if "email" variable is not filled out, display the form 
else { 
?> 

<form method="post"> 
    <input name="email" type="text" class="form-control" placeholder="Enter your email address..."> 
    <input name="subject" type="text" class="form-control" placeholder="Subject"> 
    <br> 
    <textarea name="comment" class="form-control" rows="3"></textarea> 
    <br> 
    <div class="mesbutts"> 
     <button type="submit" class="btn btn-primary" value="Submit">Send</button> 
     <button type="reset" value="Reset" class="btn btn-default" >Clear</button> 
    </div> 

</form> 
<?php 
} 
?> 
+0

noob,我不能仍然收到一封電子郵件。我需要把東西放在html標題中嗎?或者我需要首先讓我的網站託管? –

+0

您的網站安裝在本地主機上?如果您的服務器(localserver)沒有設置(postfix,sendmail)或通過應用程序發送電子郵件,您可以託管您的網站。 – Naumov

相關問題