2015-12-02 138 views
1

我想用發送電子郵件,但無法做到,而我正在將數據存入MySQL數據庫。PHP使用郵件功能發送電子郵件

我發佈了我的整個代碼,請檢查並讓我知道錯誤在哪裏。

$objConnect = mysql_connect("localhost","username","pwd"); 
$objDB = mysql_select_db("database"); 

$strName = $_POST["name"]; 
$strEmail = $_POST["email"]; 

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

$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')"; 
$objQuery = mysql_query($strSQL); 

if(!$objQuery) 
    { 
     $arr['StatusID'] = "0"; 
     $arr['Message'] = "Cannot save data!"; 
    } 
    else 
    { 
     mail($to, $subject, $message, $headers); 
     $arr['StatusID'] = "1"; 
     $arr['Message'] = "Data saved successfully";  
    } 

    mysql_close($objConnect); 

    echo json_encode($arr); 
+0

執行'var_dump(mail($ to,$ subject,$ message,$ headers))'並查看它返回的內容。 –

+0

你是否在本地進行測試? –

+0

@Sougata變得無法上傳 – Sun

回答

0

此代碼是真正的工作......

<?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(); 
    $errLevel = error_reporting(E_ALL^E_NOTICE); // suppress NOTICEs 
    if(!mail($to, $subject, $message, $headers)){ 
     error_reporting($errLevel); 
    } 
?> 
+0

你能解釋一下OP做了什麼錯誤,以及你的代碼如何幫助解決它?只是代碼是不夠的。 –

0

你有沒有在你的代碼中加入mail()函數,那麼它將如何發送郵件,並從localhost發送電子郵件也應適當配置。下面的代碼將在服務器上運行。

$objConnect = mysql_connect("localhost","username","pwd"); 
$objDB = mysql_select_db("database"); 
$strName = $_POST["name"]; 
$strEmail = $_POST["email"]; 
$to  = $strEmail; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
      'Reply-To: [email protected]' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 
if(!mail($to, $subject, $message, $headers)){ 
    echo 'Mail cant be send'; 
} 
else 
{ 
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')"; 
$objQuery = mysql_query($strSQL); 
if(!$objQuery) 
    { 
    $arr['StatusID'] = "0"; 
    $arr['Message'] = "Cannot save data!"; 
} 
else 
{ 
    mail($to, $subject, $message, $headers); 
    $arr['StatusID'] = "1"; 
    $arr['Message'] = "Data saved successfully";  
} 
echo json_encode($arr); 
    } 
    mysql_close($objConnect); 
+0

我已經試過你的方式還沒有完成 – Sun

+0

你是從服務器還是從本地主機?首先通過發送一個測試郵件來檢查郵件服務器是否正常工作。 –

+0

@孫先生簡單地檢查郵件服務器是否工作 –

相關問題