2013-09-25 167 views
1

我想知道是否有可能從本地主機發送電子郵件?我試過一些東西,但沒有奏效,我想也許它不會在本地主機上工作。下面的代碼:電子郵件發送本地主機

  // Send the email: 
      $message = " To activate your account, please click on this link:\n\n"; 
      $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation"; 
      mail($Email, 'Registration Confirmation', $message, 'From: [email protected]'); 

      // Flush the buffered output. 


      // Finish the page: 
      echo '<div class="success">Thank you forregistering! A confirmation email has been sent to '.$Email.' Please click on the Activation Link to Activate your account </div>'; 

而這裏的連接:

 /*Define constant to connect to database */ 
     DEFINE('DATABASE_USER', 'root'); 
     DEFINE('DATABASE_PASSWORD', 'buena'); 
     DEFINE('DATABASE_HOST', 'localhost'); 
     DEFINE('DATABASE_NAME', 'forum'); 
     /*Default time zone ,to be able to send mail */ 
     date_default_timezone_set('UTC'); 

     /*You might not need this */ 
     ini_set('SMTP', "mail.myt.mu"); // Overide The Default Php.ini settings for sending mail 


     //This is the address that will appear coming from (Sender) 
     define('EMAIL', '[email protected]'); 

     /*Define the root url where the script will be found such as http://website.com or http://website.com/Folder/ */ 
     DEFINE('WEBSITE_URL', 'http://localhost'); 


     // Make the connection: 
     $dbc = @mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, 
DATABASE_NAME); 

     if (!$dbc) { 
trigger_error('Could not connect to MySQL: ' . mysqli_connect_error()); 
     } 

這有什麼錯?還是失蹤?

+4

這將在localhost上正常工作,但您需要在其上設置一個smtp服務器。 – andrewsi

+0

哦,就是這樣。我對此並不熟悉。但我會做一些研究。感謝您對我發現一個問題,關於我的答案提示:) –

回答

1

您的代碼很好,問題是PHP通過其發送電子郵件的機制。在Windows中,PHP的mail()函數沒有開箱即用的解決方案,用於將電子郵件對比發送到主要由sendmail附帶的* NIX系統。

在任何情況下,都可以在Windows中實現這樣一種機制,以允許mail()函數無縫工作。儘快做到這一點,我不會在這裏介紹如何去做。請參考this clear step-by-step post,您將立即開始運行。

0

您需要一個SMTP服務器才能使用php郵件。 爲了使郵件功能可用,PHP需要安裝和運行的電子郵件系統。要使用的程序由php.ini文件中的配置設置定義。 PHP通常與PEAR郵件包打包在一起。在本網站搜索PEAR Mail。大量的信息。

+0

,它說編輯sendmail.ini本: '[sendmail的] smtp_server = smtp.gmail.com SMTP_PORT = 587 error_logfile = error.log中 debug_logfile = 的debug.log [email protected] AUTH_PASSWORD =我的Gmail郵箱密碼 force_sender =我的Gmail郵箱-ID @ gmail.com' 將這項工作如果我更改了我的首選電子郵件地址的價值,那麼請問 –

+0

@AnneBuena PEAR郵件可能是更好的選擇。它與PHP打包在一起,我不知道gmail是否允許你轉發,如果它不是一個Gmail帳戶。 – user2162192

1

要從本地主機發送郵件,您需要本地機器上的smtp服務器。我認爲這不是必須的,因爲有一個小程序監聽本地主機發送的電子郵件,捕獲它們並在默認的電子郵件客戶端中打開它們。

Here is a link

我強烈recomand這個節目。我也使用它,它是免費的:)

+1

謝謝你貝尼明,你真的救了我的一天。 :) –

相關問題