2016-02-11 73 views
0

我試圖讓一個應用程序在單擊按鈕時發送電子郵件。我試圖使用isset函數來做到這一點。但是我的代碼工作不正常。我如何解決它?下面如何在點擊提交按鈕時觸發郵件功能

演示:

<!-- <button name="sendemail">send</button> --> 
<input type="submit" value="send"/> 
<?php 
if(isset($_POST['Submit'])){ 
?> 
<?php 

// multiple recipients 
// $to = '[email protected]' . ', '; 
// $to .= '[email protected]'; 
$to="[email protected]"; 
// subject 
$subject = 'test01'; 

// message 
$message = ' 
<html> 
<head> 
    <title>Birthday Reminders for August</title> 
</head> 
<body> 
    <p>Here are the birthdays upcoming in August!</p> 
</body> 
</html> 
'; 

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 



    mail($to, $subject, $message, $headers); 
} 
?> 
+0

你會得到什麼錯誤? – Fjodr

+0

提交按鈕沒有'name'屬性,並且是放置在表單中的按鈕(意思是它首先通過POST發送)?另外,這個'?> <?php'實際上只是浪費空間。您還應該檢查郵件是否實際發送,您目前沒有做。 – Qirel

+0

@Qirel和PHP結束標記?>是一個普通的錯誤 –

回答

0

你需要設置name屬性爲您input元素,以及input元素必須是這樣的形式。另外,$_POST['submit']應該小寫以保持一致。

您還應該檢查mail()是否已成功發送。 mail()發送時將返回true,如果未發送則返回false

只是注意,實際發送郵件的,將取決於你的主機/服務器上,即使它返回true,這並不一定意味着該郵件發送。

瞭解更多關於mail()http://php.net/manual/en/function.mail.php

下面的代碼應該工作:

<form method="post"> 
    <input type="submit" name="submit" value="send" /> 
</form> 

<?php 

if(isset($_POST['submit'])){ 

// NO Change from your original code 

if(mail($to, $subject, $message, $headers)) { 

    // You can change this to whatever you want PHP to do when mail is successfully sent 
    echo 'Mail successfully sent'; 

} else { 

    // You can change this to whatever you want PHP to do when mail failed to send 
    echo 'Mail failed to send'; 

} 

?> 

希望這有助於。謝謝!

0

您可以使用下面的代碼火上發送電子郵件按鈕

<form action="" method="post"> 
    <input type="submit" value="Send details to college" /> 
    <input type="hidden" name="button_pressed" value="1" /> 
</form> 

<?php 

if(isset($_POST['button_pressed'])) 
{ 
    $to  = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 

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

    echo 'Email Sent.'; 
} 

?> 
0

幾件事情來解決這裏的點擊電子郵件。首先,您的輸入按鈕不會執行任何操作,因爲它不是表單的一部分。所以你必須使用某種JavaScript或者只是一個HTML表單來提交按鈕,所以我們可以用PHP來處理它。一個簡單的表單就像下面這樣完成。請注意0​​,這允許我們稍後在PHP中收集來自$_POST -array的信息(默認情況下,表單設置爲使用GET)。

<form method="POST"> 
    <input type="submit" name="submit" value="Send!" /> 
</form> 

這種形式將在後陣送過來的數據,到它被放置在同一個頁面,所以在這裏把你的PHP也是如此。

其次,您的提交按鈕中沒有name屬性,並且由於PHP使用的是名稱而不是ID(如JavaScript),因此您需要添加該屬性。這可以在上面的代碼片段中看到。

此外,你永遠不會檢查郵件是否實際發送。你絕不應該把任何理所當然。

<?php 
if (isset($_POST['submit'])) { 
    /* add headers, messages and such here */ 

    // Check if the mail is being sent from the server 
    if (mail($to, $subject, $message, $headers)) [ 
     // Mail was sent! Great! 
    } else { 
     // It failed sending the mail 
    } 
} 
?>