2012-07-31 19 views
0

爲什麼下面的代碼不發送郵件給我?什麼是錯誤?爲什麼mail()不發送電子郵件?

<?php 
if(isset($_POST['name'])){ 
    $msg="Name: ".$_POST['name']."\n Email: ".$_POST['email']."\n Address: ".$_POST['city']."\n Phone: ".$_POST['phone']; 
    mail('[email protected]', 'New Trial Request', $msg); 
    echo '<h2 align="center" style="color:green">Thank you for your message.</h2>'; 

} ?> 

沒有錯誤我得到了。只是我沒有在收件箱收到我的電子郵件。這在IIS服務器上運行。

+1

你得到了什麼錯誤..? – 2012-07-31 05:26:38

+0

檢查您的垃圾郵件文件夾。檢查PHP配置。 – 2012-07-31 05:27:52

+0

檢查'var_dump(mail(...))'的值; – diEcho 2012-07-31 05:28:42

回答

0

試試這個

$headers = 'From: [email protected]' . "\r\n"; 
$validate = mail('[email protected]', 'New Trial Request', $msg, $headers); 

if($validate) 
{ 
    echo '<h2 align="center" style="color:green">Thank you for your message.</h2>'; 
} 
else 
{ 
    echo '<h2 align="center" style="color:red">Something went wrong.</h2>'; 
} 

如果你'Something went wrong'這意味着問題出在你的郵件服務器,而不是在PHP代碼。

+1

至少有禮貌提及downvote的原因。這是你至少可以做的 – asprin 2012-07-31 05:40:32

0

您可能需要發送許多郵件服務器所需的使用SMTP驗證的郵件。 檢查this link瞭解更多詳情。

0

可能是你的PHP配置是不完整的,看到C://xampp/php/php.ini在:

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" 

這是激活電子郵件。可能是你的設置是:

;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" 
+0

她沒有使用xampp – 2012-07-31 05:55:33

+0

對不起,我使用xampp,但在每個PHP配置中,我刪除了sendmail_path中的分號並且成功工作 – 2012-07-31 06:36:12

-1

雖然我沒有改變你的代碼中的任何東西。請試試這個

<?php 
if(isset($_POST['name'])){ 

    $to = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 

    $msg='Name:'.$_POST{name}. "\r\n". 
     'Email: '.$_POST{email}. "\r\n". 
     'Address: '.$_POST{city}. "\r\n". 
     'Phone: '.$_POST{phone}. "\r\n"; 

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

    $sent = mail($to, $subject, $message, $headers); 
    var_dump($sent) // just to debug 
    echo '<h2 align="center" style="color:green">Thank you for your message.</h2>'; 

} ?> 
+0

如果有人downvote請提及原因並建議更正。 – diEcho 2012-07-31 05:51:37

0

我建議你使用PEAR::Mail包。您可以通過SMTP發送電子郵件。

require_once "Mail.php"; 

$from = "[email protected]"; 
$to = "[email protected]"; 
$subject = "New Trial Request"; 
$body = "Name $name, Address $address ..."; 
$host = "ssl://smtp.gmail.com"; 
$port = 465; 
$username = "[email protected]"; 
$password = "password"; 

$headers = array(
    'From' => $from, 
    'To' => $to, 
    'Subject' => $subject 
); 
$smtp = Mail::factory(
    'smtp', 
    array(
     'host' => $host, 
     'port' => $port, 
     'auth' => true, 
     'username' => $username, 
     'password' => $password 
    ) 
); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo("<p>" . $mail->getMessage() . "</p>"); 
} else { 
    echo("<p>Message has been sent!</p>"); 
} 
相關問題