2016-01-20 84 views
0

我創建了一個新頁面聯繫我們 我遇到了這個錯誤遇到麻煩。郵件不發送在PHP?

警告:電子郵件()[function.mail]:無法連接在 到郵件服務器 「localhost」 的端口25,驗證 「SMTP」 和 「SMTP_PORT」 在php.ini或 使用的ini_set設置()在上線41 contact_me.php

我的PHP文件是

<?php 
// check if fields passed are empty 

if(empty($_POST['fname']) || 
    empty($_POST['lname'])  || 
    empty($_POST['phone'])  || 
    empty($_POST['email'])  || 
    empty($_POST['dateTime'])  || 
    empty($_POST['heading'])  || 
    empty($_POST['companyName'])  || 
    empty($_POST['address'])  || 
    empty($_POST['postalCode'])  || 
    empty($_POST['city'])  || 
    empty($_POST['country'])  || 
    empty($_POST['message'])  || 
    !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) 
    { 
    echo "No arguments Provided!"; 
    return false; 
    } 

$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$phone = $_POST['phone']; 
$dateTime = $_POST['dateTime']; 
$heading = $_POST['heading']; 
$companyName = $_POST['companyName']; 
$address = $_POST['address']; 
$postalCode = $_POST['postalCode']; 
$city = $_POST['city']; 
$country = $_POST['country']; 
$message = $_POST['message']; 
$email_address = $_POST['email']; 

// create email body and send it  
$to = '[email protected]'; // PUT YOUR EMAIL ADDRESS HERE 
$email_subject = "Modern Business Contact Form: $fname"; // EDIT THE EMAIL SUBJECT LINE HERE 
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nFirst Name: $fname\n\nLast Name: $lname\n\nPhone: $phone\n\nContact Date Time:\n$dateTime\n\nHeading: $heading\n\nCompany Name: $companyName\n\nAddress: $address\n\nPostal Code: $postalCode\n\nCity: $city\n\nCountry: $country\n\nMessage: $message\n\nEmail ID: $email_address"; 
$headers = "From: [email protected]\n"; 
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers); 
return true;    
?> 

請讓我知道什麼是問題。

+0

你需要在本地主機上運行SMPT服務器以使其工作,並且應該在php,ini文件 – Pooya

+0

中提及SMTP服務器的參數。首先,您應該獲得[PHPMailer]的副本(https://github.com/PHPMailer/PHPMailer )。然後再次嘗試使用它。 – Zapp

+0

我住在這個網站不是本地的 –

回答

2

您需要在您的機器上設置郵件服務器以使郵件功能正常工作。如果你在Windows上(我猜你是使用WAMP的話),你可以設置一個Pegasus郵件服務器。

其他選項包括使用包裝類(如SwiftMailerPHPMailer),並使用它們連接到另一個SMTP服務器(如GMail帳戶)。

請參閱this Question它會解決您的問題。

0

在進行一些配置之前,不可能從本地PC發送電子郵件。我建議你上傳你的腳本到一個虛擬主機服務器(免費或付費)。我認爲它會起作用。因爲SMTP已經在虛擬主機服務器中配置。

0

我認爲你正在使用本地機器,你必須配置你的郵件功能php.ini 或加入這個代碼,我希望這將有助於你試試你的在線服務器上..

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'To: Mary <[email protected]>' . "\r\n"; 
$headers .= 'From: mysite.com <[email protected]>' . "\r\n"; 
//$headers .= 'Cc: [email protected]' . "\r\n"; 

// Mail it 
$flag = mail($to, $subject, $message, $headers); 
0

修改php.ini文件使用它(註釋掉其他線路):

[mail function] 
; For Win32 only. 
; SMTP = smtp.gmail.com 
; smtp_port = 25 

; For Win32 only. 
; sendmail_from = <e-mail username>@gmail.com 

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). 
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"