2015-09-14 82 views
1

我想送的時候,「發送電子郵件」按鈕被按下電子郵件,但下面的代碼將不會發送任何東西到我的郵箱:發送電子郵件與PHP

<!DOCTYPE html> 
<html> 
<form action="email.php" method="post"> 
<input value="Send Email" name="email" type="submit"> 
</form> 
<?php 
    if (isset($_POST['email'])) { 
    $msg = 'hello'; 
    mail('[email protected]', 'Sample', $msg); 
           }        
?> 
</html> 

任何想法如何讓它起作用?

+0

你在本地或在您的網站的測試呢? – herriekrekel

+0

從本地主機上,我將它保存爲email.php – Jack

+0

有些測試程序可用於測試是否發送電子郵件。之後,這個問題是不是在你的代碼,並不得不問你的ISP,或使用中間件服務,如sendgrid;) – DDeme

回答

1

使用下面的PHP代碼發送電子郵件

<?php 
$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); 
?> 

郵件功能將無法在本地server.You工作需要配置您的本地服務器上的SMTP。看看這個類似的帖子,

php mail setup in xampp

+0

http://stackoverflow.com/questions/ 5335273/how-to-send-an-email-using-php –

+0

謝謝你,所以我猜這是行不通的,因爲我在本地服務器上運行它。 – Jack

+1

是的,可能是這種情況...請閱讀由娜娜或我在答案中共享的鏈接 –

0

試試這個

<?php 
     $to = "someemail.com" 
     $subject = "This is subject"; 

     $message = "<b>This is message.</b>"; 
     $message .= "<h1>This is headline.</h1>"; 

     $header = "From:[email protected] \r\n"; 

     $header .= "MIME-Version: 1.0\r\n"; 
     $header .= "Content-type: text/html\r\n"; 

     $retval = mail ($to,$subject,$message,$header); 

     if($retval == true) 
     { 
      echo "Message sent successfully..."; 
     } 
     else 
     { 
      echo "Message could not be sent..."; 
     } 
     ?>